Skip to content

Hold a strong ref to the timed disconnect task to avoid GC #189

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

Merged
merged 2 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions switchbot/adv_parsers/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# data: 650000cd802b6300
# data: 658000c9802b6300


# Low: 658000c5222b6300
# Med: 658000c5432b6300
# High: 658000c5642b6300
Expand Down
1 change: 0 additions & 1 deletion switchbot/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class SwitchbotAccountConnectionError(RuntimeError):


class SwitchbotModel(StrEnum):

BOT = "WoHand"
CURTAIN = "WoCurtain"
HUMIDIFIER = "WoHumi"
Expand Down
6 changes: 3 additions & 3 deletions switchbot/devices/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ..const import DEFAULT_RETRY_COUNT, DEFAULT_SCAN_TIMEOUT
from ..discovery import GetSwitchbotDevices
from ..models import SwitchBotAdvertisement
from ..util import execute_task

_LOGGER = logging.getLogger(__name__)

Expand All @@ -48,7 +49,6 @@


class ColorMode(Enum):

OFF = 0
COLOR_TEMP = 1
RGB = 2
Expand Down Expand Up @@ -331,7 +331,7 @@ def _disconnect_from_timer(self):
self._reset_disconnect_timer()
return
self._cancel_disconnect_timer()
asyncio.create_task(self._execute_timed_disconnect())
execute_task(self._execute_timed_disconnect())

def _cancel_disconnect_timer(self):
"""Cancel disconnect timer."""
Expand Down Expand Up @@ -375,7 +375,7 @@ async def _execute_disconnect_with_lock(self) -> None:
self._client = None
self._read_char = None
self._write_char = None
if client and client.is_connected:
if client:
_LOGGER.debug("%s: Disconnecting", self.name)
await client.disconnect()
_LOGGER.debug("%s: Disconnect completed", self.name)
Expand Down
17 changes: 17 additions & 0 deletions switchbot/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Library to handle connection with Switchbot."""

import asyncio
from collections.abc import Awaitable
from typing import Any


def execute_task(fut: Awaitable[Any]) -> None:
"""Execute task."""
task = asyncio.create_task(fut)
tasks = [task]

def _cleanup_task(task: asyncio.Task[Any]) -> None:
"""Cleanup task."""
tasks.remove(task)

task.add_done_callback(_cleanup_task)