-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid multithreading and other fixes #359
Conversation
Forgot to add: There also can be a problem with periodical calling of |
Can't we find better way to avoid using async def _shutdown_entities(self, exc=""):
"""Shutdown device entities"""
# Delay shutdown.
if not self._is_closing:
await asyncio.sleep(3 + self._device_config.sleep_time)
if self.connected:
return
signal = f"localtuya_{self._device_config.id}"
dispatcher_send(self._hass, signal, None)
if self._is_closing:
return
if self.is_subdevice:
self.info(f"Sub-device disconnected due to: {exc}")
elif hasattr(self, "low_power"):
m, s = divmod((int(time.time()) - self._last_update_time), 60)
h, m = divmod(m, 60)
self.info(f"The device is still out of reach since: {h}h:{m}m:{s}s")
else:
self.info(f"Disconnected due to: {exc}") Now whenever we want to call it w/ delay self._call_on_close.append(
asyncio.create_task(self._shutdown_entities(exc=exc)).cancel
) ^ as an example it will needs a checks / cancelation on connected |
Agree. I've added |
The There is another related problem, with |
FYI, after merging in this set of changes, I'll PR another set, for better handling of unload/shutdown events. It is ready and is running quite well. |
If we reload the integration the shutdown task will be canceled however if the connection fails after reload we need to shutdown the entities in that case as well. |
If the connection fails after reload, the entities should stay in their initial states, i.e. disabled and Unknown. At least this is what I've experienced when LocalTuya failed to start after a restart due to my bugs. Also, I saw entities disabled in the past, when connections started in Anyway, this PR changes nothing for the case you mentioned, compared to the existing behavior: it sould continue working, or not working, exactly as before 😄 So I suggest to finish here, with loops from dirfferent threads, then go to other possible problems, step by step. |
forgot about that thanks 😄 you are correct it should be disabled. |
8304b61:
_shutdown_entities
checks several properties and members that could be changed during connection process. So, to avoid possible race conditions, it is better to call it from the main thread loop of LocalTuya, rather than from HASS thread loop. Instead, a device's entities could be disabled right after successful re-connect. Frankly, I never experienced such a misbehavior.70970c9:
After several unsuccessful re-connect attempts, it is good to decide that the device is offline and increase delay between further attempts. Just like for sub-devices that were reported as offline by their gateways.
d0a9539:
On close event,
_shutdown_entities
, actually, did nothing for low power and currently connected devices. Actually, this call has rather cosmetic effect, because the entities are removed from HASS in a moment. But, while it is there, better to make it right way.