make-all / tuya-local

Local support for Tuya devices in Home Assistant
MIT License
1.01k stars 405 forks source link

Ha 2024.5.0 issue fixes #1876

Closed craibo closed 4 weeks ago

craibo commented 1 month ago
make-all commented 4 weeks ago

Does this not cause performance problems? [Edit: after review of async_receive, the blocking functions are all using an executor job, so should be safe to use on the event loop]

It seems you have changed from starting a task to handle receiving, to scheduling receives on the HA event loop. Since the tinytuya library is not async, this will block the HA event loop for up to 5 seconds every time we check for updates.

The other change to use schedule_update_ha_state instead of async_write_ha_state should not be necessary if the receive loop is running on the HA event loop, but without a change to async_refresh, will result in extra polling of the device, which may be what is making the difference. [Edit: the necessary change to async_refresh was made in my previous attempt to fix the issue, and was not reverted with the rest of the change when that attempt failed to fix the issue]

make-all commented 4 weeks ago

I am not sure why the test failure is occuring, maybe coroutines need to be top level functions outside of a class.

It will also be necessary to save the result of the call that creates the task so it can be awaited when the device is stopped (after ensuring that the receive loop will be terminated). [Edit: self._refresh_task is already awaited in the stop method, I am not sure why some users were reporting "...was not awaited" messages, possibly it is due to HA killing the tasks without going through the stop method, and avoided by using the new method]