Skip to content

Commit 284f575

Browse files
authored
Replace asyncio.sleep with custom sleep function (#117)
1 parent 77190b0 commit 284f575

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

custom_components/ta_cmi/__init__.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@
2929
PLATFORMS: list[str] = [Platform.SENSOR, Platform.BINARY_SENSOR]
3030

3131

32+
async def custom_sleep(delay: int) -> None:
33+
"""Custom sleep function to prevent Home Assistant from canceling."""
34+
start = time.time()
35+
try:
36+
await asyncio.sleep(delay)
37+
except asyncio.CancelledError:
38+
elapsed = time.time() - start
39+
_LOGGER.debug(
40+
"Sleep cancelled after %s. Sleep remaining time: %s",
41+
elapsed,
42+
delay - elapsed,
43+
)
44+
await asyncio.sleep(delay - elapsed)
45+
46+
3247
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3348
"""Set up platform from a ConfigEntry."""
3449
host: str = entry.data[CONF_HOST]
@@ -106,21 +121,6 @@ def __init__(
106121

107122
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval)
108123

109-
@staticmethod
110-
async def custom_sleep(delay: int) -> None:
111-
"""Custom sleep function to prevent Home Assistant from canceling."""
112-
start = time.time()
113-
try:
114-
await asyncio.sleep(delay)
115-
except asyncio.CancelledError:
116-
elapsed = time.time() - start
117-
_LOGGER.debug(
118-
"Sleep cancelled after %s. Sleep remaining time: %s",
119-
elapsed,
120-
delay - elapsed,
121-
)
122-
await asyncio.sleep(delay - elapsed)
123-
124124
async def _async_update_data(self) -> dict[str, Any]:
125125
"""Update data."""
126126
try:
@@ -141,7 +141,7 @@ async def _async_update_data(self) -> dict[str, Any]:
141141
_LOGGER.debug(
142142
f"Sleep mode for {DEVICE_DELAY} seconds to prevent rate limiting"
143143
)
144-
await self.custom_sleep(DEVICE_DELAY)
144+
await custom_sleep(DEVICE_DELAY)
145145

146146
return return_data
147147
except (InvalidCredentialsError, RateLimitError, ApiError) as err:

custom_components/ta_cmi/config_flow.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Config flow for Technische Alternative C.M.I. integration."""
22
from __future__ import annotations
33

4-
import asyncio
54
from copy import deepcopy
65
from datetime import timedelta
76
import time
@@ -15,16 +14,10 @@
1514
from homeassistant.exceptions import HomeAssistantError
1615
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1716
import homeassistant.helpers.config_validation as cv
18-
from ta_cmi import (
19-
CMI,
20-
ApiError,
21-
Device,
22-
InvalidCredentialsError,
23-
InvalidDeviceError,
24-
RateLimitError,
25-
)
17+
from ta_cmi import CMI, ApiError, Device, InvalidCredentialsError, RateLimitError
2618
import voluptuous as vol
2719

20+
from . import custom_sleep
2821
from .const import (
2922
_LOGGER,
3023
CONF_CHANNELS,
@@ -69,13 +62,13 @@ async def fetch_device(device: Device, retry=False) -> None:
6962
_LOGGER.debug(
7063
f"Sleep mode for {DEVICE_DELAY} seconds to prevent rate limiting"
7164
)
72-
await asyncio.sleep(DEVICE_DELAY)
65+
await custom_sleep(DEVICE_DELAY)
7366
device.set_device_type("DUMMY-NO-IO")
7467

7568
_LOGGER.debug("Try to fetch device type: %s", device.id)
7669
await device.fetch_type()
7770
_LOGGER.debug(f"Sleep mode for {DEVICE_DELAY} seconds to prevent rate limiting")
78-
await asyncio.sleep(DEVICE_DELAY)
71+
await custom_sleep(DEVICE_DELAY)
7972

8073
_LOGGER.debug("Try to fetch available device channels: %s", device.id)
8174
await device.update()
@@ -189,7 +182,7 @@ async def async_step_devices(
189182
_LOGGER.debug(
190183
f"Sleep mode for {DEVICE_DELAY} seconds to prevent rate limiting"
191184
)
192-
await asyncio.sleep(DEVICE_DELAY)
185+
await custom_sleep(DEVICE_DELAY)
193186

194187
try:
195188
await fetch_device(dev)
@@ -291,7 +284,7 @@ async def async_step_finish(self) -> FlowResult:
291284
end_time = time.time()
292285
time_lapsed = end_time - self.start_time
293286
if time_lapsed <= DEVICE_DELAY:
294-
await asyncio.sleep(DEVICE_DELAY - time_lapsed)
287+
await custom_sleep(int(DEVICE_DELAY - time_lapsed))
295288
return self.async_create_entry(title="C.M.I", data=self.config)
296289

297290
@staticmethod

0 commit comments

Comments
 (0)