Can't achieve to use hass.async_add_executor_job

I’m creating the config flow for worldtidesinfocustom https://github.com/jugla/worldtidesinfocustom

I’m trying to implement config_flow as a 2nd entry for the sensor.

I fall on issue that I/O are implemented in sync metod (via package requests).
From function point of view , the sensor entity call several function toward this I/O request in sync mode.

I only redirect async_update() to update() method.

To protect from the event loop , I have added hass.async_add_executor_job.
Issue : I have always a message that say the hass.async_add_executor_job shall be put just in front of of I/O whereas at this step I’m in sync … (i.e. I cannot put await …)

Any idea ?

My code is https://github.com/jugla/worldtidesinfocustom/blob/ef19c623d7a7a075295c419a1b2a54b2ef1caf12/custom_components/worldtidesinfocustom/sensor.py around line 486 for async_update() method

You should just pass the method and not call it:

await self._hass.async_add_executor_job(self.update)

When calling it like you do now, you do exactly the opposite of what you are trying to achieve (i.e. calling sync code from async context).

2 Likes

Thank you very much !
It works :slight_smile:

NB : perhaps in https://developers.home-assistant.io/docs/asyncio_working_with_async/ , the absence of () shall be highlighted.

1 Like

What if the function has multiple parameters, but I only want to pass the last parameter. How can I do that?

Please can you start a new topic, please.
I will answer.