tuya-local icon indicating copy to clipboard operation
tuya-local copied to clipboard

When unloading the integration, runtime errors occur

Open AdrianGarside opened this issue 1 year ago • 11 comments

2024-05-05 11:36:06.537 DEBUG (MainThread) [custom_components.tuya_local] Unloading entry for device: REDACTED 2024-05-05 11:36:06.538 DEBUG (MainThread) [custom_components.tuya_local.helpers.device_config] Loaded device config simple_dual_switch.yaml 2024-05-05 11:36:06.538 DEBUG (MainThread) [custom_components.tuya_local.device] Stopping monitor loop for Dual switch 2024-05-05 11:36:06.538 ERROR (MainThread) [homeassistant.components.switch] Error while removing entity switch.dual_switch_switch_2 Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1353, in async_remove await self.__async_remove_impl(force_remove) File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1369, in __async_remove_impl await self.async_will_remove_from_hass() File "/config/custom_components/tuya_local/helpers/mixin.py", line 111, in async_will_remove_from_hass await self._device.async_unregister_entity(self) File "/config/custom_components/tuya_local/device.py", line 195, in async_unregister_entity await self.async_stop() File "/config/custom_components/tuya_local/device.py", line 170, in async_stop await self._refresh_task TypeError: object Future can't be used in 'await' expression

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 908, in async_reset await entity.async_remove() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1358, in async_remove self.__remove_future.set_result(None) asyncio.exceptions.InvalidStateError: invalid state 2024-05-05 11:36:06.540 INFO (MainThread) [custom_components.tuya_local.device] Deleting device: REDACTED 2024-05-05 11:36:06.540 DEBUG (MainThread) [custom_components.tuya_local.device] Stopping monitor loop for Dual switch 2024-05-05 11:36:06.540 ERROR (MainThread) [homeassistant.config_entries] Error unloading entry Dual switch for tuya_local Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/config_entries.py", line 724, in async_unload result = await component.async_unload_entry(hass, self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/tuya_local/init.py", line 517, in async_unload_entry await async_delete_device(hass, config) File "/config/custom_components/tuya_local/device.py", line 655, in async_delete_device await hass.data[DOMAIN][device_id]["device"].async_stop() File "/config/custom_components/tuya_local/device.py", line 170, in async_stop await self._refresh_task TypeError: object Future can't be used in 'await' expression 2024-05-05 11:36:06.921 WARNING (MainThread) [custom_components.tuya_local.device] Dual switch receive loop has terminated

AdrianGarside avatar May 05 '24 18:05 AdrianGarside

I'm seeing the same after upgrade to 2024.5.2 today. Here are my logs

2024-05-06 18:30:05.727 ERROR (MainThread) [homeassistant.components.lock] Error while removing entity lock.kantoor_1_child_lock Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1378, in async_remove await self.__async_remove_impl(force_remove) File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1394, in __async_remove_impl await self.async_will_remove_from_hass() File "/config/custom_components/tuya_local/helpers/mixin.py", line 111, in async_will_remove_from_hass await self._device.async_unregister_entity(self) File "/config/custom_components/tuya_local/device.py", line 195, in async_unregister_entity await self.async_stop() File "/config/custom_components/tuya_local/device.py", line 170, in async_stop await self._refresh_task TypeError: object Future can't be used in 'await' expression

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 910, in async_reset await entity.async_remove() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1383, in async_remove self.__remove_future.set_result(None) asyncio.exceptions.InvalidStateError: invalid state 2024-05-06 18:30:05.734 INFO (MainThread) [custom_components.tuya_local.device] Deleting device: 2024-05-06 18:30:05.734 DEBUG (MainThread) [custom_components.tuya_local.device] Stopping monitor loop for Kantoor 1 2024-05-06 18:30:05.735 ERROR (MainThread) [homeassistant.config_entries] Error unloading entry Kantoor 1 for tuya_local Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/config_entries.py", line 772, in async_unload result = await component.async_unload_entry(hass, self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/tuya_local/init.py", line 517, in async_unload_entry await async_delete_device(hass, config) File "/config/custom_components/tuya_local/device.py", line 655, in async_delete_device await hass.data[DOMAIN][device_id]["device"].async_stop() File "/config/custom_components/tuya_local/device.py", line 170, in async_stop await self._refresh_task TypeError: object Future can't be used in 'await' expression

The integration itself says: "Failed to unload"

sweetpants avatar May 06 '24 16:05 sweetpants

Hmmm, this seems to be related to the reload workaround using "Home Assistant Core Integration: Reload config entry -> entity-ID". When disabled there are no errors in the log anymore.

sweetpants avatar May 06 '24 16:05 sweetpants

That is a trigger, not a cause. The bug will still show up whenever the integration is stopped.

make-all avatar May 07 '24 02:05 make-all

I am seeing this too after upgrade to 2024.5.2 today. The integration itself says ‘Failed to unload’. Anyone has figured out any workaround ?

muzammilk avatar May 09 '24 04:05 muzammilk

This fixes it but I don't know if it's a correct and complete fix: Chang:

        if self._refresh_task:
            await self._refresh_task

to

        if self._refresh_task:
            await asyncio.wrap_future(self._refresh_task)

AdrianGarside avatar May 09 '24 05:05 AdrianGarside

This fixes it but I don't know if it's a correct and complete fix: Chang:

        if self._refresh_task:
            await self._refresh_task

to

        if self._refresh_task:
            await asyncio.wrap_future(self._refresh_task)

Hey mate, I tried this but then my integration won’t reload devices at all

haroldboom avatar May 10 '24 00:05 haroldboom

For me the reload is slower - sometimes quite a bit slower but it does reload the device seemingly correctly in the logs: 2024-05-09 21:48:56.796 INFO (MainThread) [custom_components.tuya_local.device] Deleting device: 24142854d8bfc0c9dcd0 2024-05-09 21:48:56.797 INFO (MainThread) [custom_components.tuya_local.device] Creating device: 24142854d8bfc0c9dcd0

And if I made local edits to the device yaml it is reflected post-reload.

AdrianGarside avatar May 10 '24 04:05 AdrianGarside

For me the reload is slower - sometimes quite a bit slower but it does reload the device seemingly correctly in the logs: 2024-05-09 21:48:56.796 INFO (MainThread) [custom_components.tuya_local.device] Deleting device: 24142854d8bfc0c9dcd0 2024-05-09 21:48:56.797 INFO (MainThread) [custom_components.tuya_local.device] Creating device: 24142854d8bfc0c9dcd0

And if I made local edits to the device yaml it is reflected post-reload.

I just tried it again and it worked ok. I’ll try it for a few days and report back. Earlier when it wasn’t working was on a device that was unavailable in HA but was available in the Tuya app and it wouldn’t reload, just hung.

haroldboom avatar May 10 '24 05:05 haroldboom

I suspect without that change, there may be a conflict causing a failure, and the delay waiting for the device to finish unloading should avoid that

make-all avatar May 10 '24 05:05 make-all

ut that change, there may be a conflict causing a failure, and the delay waiting for the device to finish unloading should avoid that

I think with that fix it's going to actually wait on the device so if the device is hung the reload will hang too?

AdrianGarside avatar May 10 '24 05:05 AdrianGarside

The integration itself says: "Failed to unload"

+1 to also having this error

This fixes it but I don't know if it's a correct and complete fix: Chang:

        if self._refresh_task:
            await self._refresh_task

to

        if self._refresh_task:
            await asyncio.wrap_future(self._refresh_task)

This fixed worked for me: image

poldim avatar May 10 '24 15:05 poldim

Fix works for me also

haroldboom avatar May 14 '24 11:05 haroldboom

Using the suggested fix from HA dev on #1917 instead of the previous PR #1876 fix for #1871 also seems to fix this.

make-all avatar May 15 '24 13:05 make-all