xZetsubou / hass-localtuya

🔹 A Home Assistant integration to handle Tuya devices locally "fork from localtuya"
https://xzetsubou.github.io/hass-localtuya/
GNU General Public License v3.0
291 stars 30 forks source link

Integration tries to connect to devices disabled by user #160

Closed andrus2049 closed 3 months ago

andrus2049 commented 4 months ago

Devices disabled in HA by user because no longer powered or no longer connected to the wifi network or temporarily unused (seasonal device like heaters in summer and irrigation valves in winter) are in any case searched for connection repeatedly.

At HA startup and at any localtuya integration manual reload or reconfiguration this cause a very very long delay showing the integration as "initializing" and slow to react,, and eventually it might be shown as failed to setup.

If possible please exclude disabled devices to be contacted.

Example of error logged (repeated many times in the log)

Unloading V40 (localtuya) config entry. Task <Task cancelling name='reconnect_172.16.110.132' coro=<TuyaDevice.async_connect() running at /config/custom_components/localtuya/common.py:232> wait_for=<Task cancelling name='Task-40789' coro=<TuyaDevice._make_connection() running at /config/custom_components/localtuya/common.py:251> wait_for=<Future pending cb=[BaseSelectorEventLoop._sock_write_done(71, handle=<Handle BaseS....132', 6668))>)(), Task.task_wakeup()]> cb=[set.remove(), Task.task_wakeup()]> cb=[set.remove(), <4 more>, _wait.._on_completion() at /usr/local/lib/python3.12/asyncio/tasks.py:534]> did not complete in time

common.py

    async def async_connect(self, _now=None) -> None:
        """Connect to device if not already connected."""
        if not self._hass_entry:
            self._hass_entry = self._hass.data[DOMAIN][self._config_entry.entry_id]

        if not self._is_closing and not self.is_connecting and not self.connected:
            try:
                self._connect_task = self._hass.async_create_task(
                    self._make_connection()
                )
                await self._connect_task
            except (TimeoutError, asyncio.CancelledError):
xZetsubou commented 4 months ago

I think there are two things,

  1. there is a timeout "5 seconds" on connecting task so it shouldn't takes long however this fix might be applied on master version not released one.

  2. How did you disabled the the devices did you disable all the entities in that devices?

andrus2049 commented 4 months ago

I disabled the device in HA, so all the entities pertaining to that device appear marked as disabled too. Time probably depends on how many times connection is retried.

Cause of this only device I currently disabled, the integration "inizialing" status lasts for more than 10-15 minutes, and this is a little inconvenient when adding new tuya devices, or testing/modifying their configuration, or reloading the integration itself or even rebooting HA (also the message localtuya still loading etc.... is displayed for all this time after HA reboots) After that period the following integration status is marked as Failed to set-up. After some more time finally the status returns normal ("CONFIGURE" is shown), and stay so until a configuration of any of my local connected devices is reloaded/modified, and this behavior starts over. No cloud API account configured, only ID and keys used (true local).

If possible disabled devices in HA shouldn't taken into consideration when attempting the connection (filtered/excluded in common.py?).

xZetsubou commented 4 months ago

Ok, I got confused on different thing, I'll fix this however the fix shouldn't happen in common.py.

andrus2049 commented 4 months ago

Many thanks, yours is the best localtuya HA integration, and I tested almost all available... Particularly I appreciated the device template export that is only available in your integration. And you keep up with the updates, which is fundamental!

andrus2049 commented 4 months ago

Great! I manually downloaded your updated repository and copied the files onto the HA localtuya folder, then rebooted HA. Now HA boots quickly and localtuya integration startup time lasts only few seconds (for 14 devices and 53 entities). No errors!! Many thanks, problem solved!!

2024-03-06 01 29 32 v40 local f4bdfb7abad1

xZetsubou commented 4 months ago

There is an issue in the commit https://github.com/xZetsubou/hass-localtuya/commit/34a9d9aa6161ca1f1fdd4af8d1cae1e382f644cd that if you added new device integration won't load up any more. https://github.com/xZetsubou/hass-localtuya/commit/d08e1f3068b0a5d49c89caec97b8f792e147d29d should fix it.

andrus2049 commented 4 months ago

Ok, updated files, rebooted, all ok. I had no new device to add in this meanwhile, so I didn't get into that problem. Thanks alot again.