Skip to content

Commit 979cdc9

Browse files
authored
Hold a strong ref to the timed disconnect task to avoid GC (#189)
1 parent 34968e6 commit 979cdc9

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

switchbot/adv_parsers/humidifier.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# data: 650000cd802b6300
1111
# data: 658000c9802b6300
1212

13+
1314
# Low: 658000c5222b6300
1415
# Med: 658000c5432b6300
1516
# High: 658000c5642b6300

switchbot/const.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class SwitchbotAccountConnectionError(RuntimeError):
2727

2828

2929
class SwitchbotModel(StrEnum):
30-
3130
BOT = "WoHand"
3231
CURTAIN = "WoCurtain"
3332
HUMIDIFIER = "WoHumi"

switchbot/devices/device.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from ..const import DEFAULT_RETRY_COUNT, DEFAULT_SCAN_TIMEOUT
2727
from ..discovery import GetSwitchbotDevices
2828
from ..models import SwitchBotAdvertisement
29+
from ..util import execute_task
2930

3031
_LOGGER = logging.getLogger(__name__)
3132

@@ -48,7 +49,6 @@
4849

4950

5051
class ColorMode(Enum):
51-
5252
OFF = 0
5353
COLOR_TEMP = 1
5454
RGB = 2
@@ -331,7 +331,7 @@ def _disconnect_from_timer(self):
331331
self._reset_disconnect_timer()
332332
return
333333
self._cancel_disconnect_timer()
334-
asyncio.create_task(self._execute_timed_disconnect())
334+
execute_task(self._execute_timed_disconnect())
335335

336336
def _cancel_disconnect_timer(self):
337337
"""Cancel disconnect timer."""
@@ -375,7 +375,7 @@ async def _execute_disconnect_with_lock(self) -> None:
375375
self._client = None
376376
self._read_char = None
377377
self._write_char = None
378-
if client and client.is_connected:
378+
if client:
379379
_LOGGER.debug("%s: Disconnecting", self.name)
380380
await client.disconnect()
381381
_LOGGER.debug("%s: Disconnect completed", self.name)

switchbot/util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Library to handle connection with Switchbot."""
2+
3+
import asyncio
4+
from collections.abc import Awaitable
5+
from typing import Any
6+
7+
8+
def execute_task(fut: Awaitable[Any]) -> None:
9+
"""Execute task."""
10+
task = asyncio.create_task(fut)
11+
tasks = [task]
12+
13+
def _cleanup_task(task: asyncio.Task[Any]) -> None:
14+
"""Cleanup task."""
15+
tasks.remove(task)
16+
17+
task.add_done_callback(_cleanup_task)

0 commit comments

Comments
 (0)