Pyscript make api call to Wolfram Alpha

On HAOS, I have pyscript installed, and I am trying to test making an api call to Wolfram Alpha in python. The code works fine in a normal environment:

import wolframalpha

@service
def WolframAlphaTest():
    wolframalpha_client = wolframalpha.Client('API-KEY')
    response = wolframalpha_client.query("What is the melting point of iron?")
    log.info(next (response.results).text)

However, in HA, it gives me an error:
Exception in <file.wolframalpha_test.WolframAlphaTest> line 6: response = await wolframalpha_client.query(“What is the melting point of iron?”) ^ RuntimeError: asyncio.run() cannot be called from a running event loop

I’ve tried using async and await with asyncio, but haven’t had any luck there either, assuming I’m using those correctly.

import asyncio
import wolframalpha

@service
async def WolframAlphaTest():
  wolframalpha_client = wolframalpha.Client('API_KEY')
  response = await wolframalpha_client.query("What is the melting point of iron?")
  answer = next(response.results).text
  log.info(answer)

Any help would be appreciated!

If anyone else has this question, here is what I got to work:

import wolframalpha

@service
def WolframAlphaTest():
    log.info(SendQuery())
    
@pyscript_executor
def SendQuery():
    wolframalpha_client = wolframalpha.Client('API-KEY')
    response = wolframalpha_client.query("What is the melting point of iron?")
    return next (response.results).text
1 Like