I am trying to build an integration and for that i need to do an API call. This API call is done with a library which does not support async programming, therefore I tried using async_add_executor_job
to do this api call.
However, by running the api call function inside async_add_executor_job
, it changes the input to the API call function and keeps executing the function multiple times. I have been trying a lot of different stuff, but i can’t seem to find what the problem is.
Here is my code:
async def _async_update_data(self) -> dict:
"""Get the latest data from ENTSO-e"""
self.logger.debug("Fetching ENTSO-e data")
today = pd.Timestamp(dt.as_local(dt.start_of_local_day()))
tomorrow = pd.Timestamp(dt.as_local(dt.start_of_local_day()+timedelta(days=1)))
data_today = await self.fetchprices(today, tomorrow)
return {
"marketPricesElectricity": data_today,
}
async def fetchprices(self, start_date, end_date):
try:
resp = await self.hass.async_add_executor_job(
self.api_update, start_date, end_date
)
data = resp.to_dict()
return data
except (asyncio.TimeoutError, KeyError) as error:
raise UpdateFailed(f"Fetching energy price data failed: {error}") from error
def api_update(self, start_date, end_date):
client = EntsoePandasClient(api_key=self.api_key)
return client.query_day_ahead_prices("NL", start=start_date, end=end_date)
I hope you can help me!
Thanks in advance!