From 0b691a70a68becc5fc55ae098d31d2742dfb944a Mon Sep 17 00:00:00 2001 From: Jason Rumney Date: Tue, 4 Jun 2024 21:04:26 +0900 Subject: [PATCH] Handle CancelledError when stopping When stopping the integration, we expect that a CancelledError will be thrown, so catch it and continue as normal. Issue #1974 --- custom_components/tuya_local/device.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/custom_components/tuya_local/device.py b/custom_components/tuya_local/device.py index 1d333c58a0..79a3b30d43 100644 --- a/custom_components/tuya_local/device.py +++ b/custom_components/tuya_local/device.py @@ -3,6 +3,7 @@ """ import asyncio +from asyncio.exceptions import CancelledError import logging from threading import Lock from time import time @@ -191,7 +192,10 @@ def register_entity(self, entity): async def async_unregister_entity(self, entity): self._children.remove(entity) if not self._children: - await self.async_stop() + try: + await self.async_stop() + except CancelledError: + pass async def receive_loop(self): """Coroutine wrapper for async_receive generator.""" @@ -320,7 +324,7 @@ async def async_receive(self): await asyncio.sleep(0.1 if self.has_returned_state else 5) - except asyncio.CancelledError: + except CancelledError: self._running = False # Close the persistent connection when exiting the loop self._api.set_socketPersistent(False)