Skip to content

Commit 2175754

Browse files
hahn-thbdraco
authored andcommitted
Fix throttling issue in HomematicIP Cloud (home-assistant#146683)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent df5f253 commit 2175754

File tree

5 files changed

+42
-42
lines changed

5 files changed

+42
-42
lines changed

homeassistant/components/homematicip_cloud/hap.py

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ async def async_setup(self, tries: int = 0) -> bool:
128128
self.config_entry.data.get(HMIPC_AUTHTOKEN),
129129
self.config_entry.data.get(HMIPC_NAME),
130130
)
131+
131132
except HmipcConnectionError as err:
132133
raise ConfigEntryNotReady from err
133134
except Exception as err: # noqa: BLE001
@@ -210,41 +211,13 @@ def update_all(self) -> None:
210211
for device in self.home.devices:
211212
device.fire_update_event()
212213

213-
async def async_connect(self) -> None:
214-
"""Start WebSocket connection."""
215-
tries = 0
216-
while True:
217-
retry_delay = 2 ** min(tries, 8)
218-
219-
try:
220-
await self.home.get_current_state_async()
221-
hmip_events = self.home.enable_events()
222-
self.home.set_on_connected_handler(self.ws_connected_handler)
223-
self.home.set_on_disconnected_handler(self.ws_disconnected_handler)
224-
tries = 0
225-
await hmip_events
226-
except HmipConnectionError:
227-
_LOGGER.error(
228-
(
229-
"Error connecting to HomematicIP with HAP %s. "
230-
"Retrying in %d seconds"
231-
),
232-
self.config_entry.unique_id,
233-
retry_delay,
234-
)
235-
236-
if self._ws_close_requested:
237-
break
238-
self._ws_close_requested = False
239-
tries += 1
240-
241-
try:
242-
self._retry_task = self.hass.async_create_task(
243-
asyncio.sleep(retry_delay)
244-
)
245-
await self._retry_task
246-
except asyncio.CancelledError:
247-
break
214+
async def async_connect(self, home: AsyncHome) -> None:
215+
"""Connect to HomematicIP Cloud Websocket."""
216+
await home.enable_events()
217+
218+
home.set_on_connected_handler(self.ws_connected_handler)
219+
home.set_on_disconnected_handler(self.ws_disconnected_handler)
220+
home.set_on_reconnect_handler(self.ws_reconnected_handler)
248221

249222
async def async_reset(self) -> bool:
250223
"""Close the websocket connection."""
@@ -272,14 +245,22 @@ def shutdown(self, event) -> None:
272245

273246
async def ws_connected_handler(self) -> None:
274247
"""Handle websocket connected."""
275-
_LOGGER.debug("WebSocket connection to HomematicIP established")
248+
_LOGGER.info("Websocket connection to HomematicIP Cloud established")
276249
if self._ws_connection_closed.is_set():
277250
await self.get_state()
278251
self._ws_connection_closed.clear()
279252

280253
async def ws_disconnected_handler(self) -> None:
281254
"""Handle websocket disconnection."""
282-
_LOGGER.warning("WebSocket connection to HomematicIP closed")
255+
_LOGGER.warning("Websocket connection to HomematicIP Cloud closed")
256+
self._ws_connection_closed.set()
257+
258+
async def ws_reconnected_handler(self, reason: str) -> None:
259+
"""Handle websocket reconnection."""
260+
_LOGGER.info(
261+
"Websocket connection to HomematicIP Cloud re-established due to reason: %s",
262+
reason,
263+
)
283264
self._ws_connection_closed.set()
284265

285266
async def get_hap(
@@ -306,6 +287,6 @@ async def get_hap(
306287
home.on_update(self.async_update)
307288
home.on_create(self.async_create_entity)
308289

309-
hass.loop.create_task(self.async_connect())
290+
await self.async_connect(home)
310291

311292
return home

homeassistant/components/homematicip_cloud/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
"documentation": "https://www.home-assistant.io/integrations/homematicip_cloud",
77
"iot_class": "cloud_push",
88
"loggers": ["homematicip"],
9-
"requirements": ["homematicip==2.0.4"]
9+
"requirements": ["homematicip==2.0.5"]
1010
}

requirements_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements_test_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/components/homematicip_cloud/test_hap.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Test HomematicIP Cloud accesspoint."""
22

3-
from unittest.mock import Mock, patch
3+
from unittest.mock import AsyncMock, Mock, patch
44

55
from homematicip.auth import Auth
66
from homematicip.connection.connection_context import ConnectionContext
@@ -16,6 +16,7 @@
1616
)
1717
from homeassistant.components.homematicip_cloud.errors import HmipcConnectionError
1818
from homeassistant.components.homematicip_cloud.hap import (
19+
AsyncHome,
1920
HomematicipAuth,
2021
HomematicipHAP,
2122
)
@@ -251,3 +252,21 @@ async def test_get_state_after_disconnect(
251252
assert hap._ws_connection_closed.is_set()
252253
await hap.ws_connected_handler()
253254
mock_get_state.assert_called_once()
255+
256+
257+
async def test_async_connect(
258+
hass: HomeAssistant, hmip_config_entry: MockConfigEntry, simple_mock_home
259+
) -> None:
260+
"""Test async_connect."""
261+
hass.config.components.add(DOMAIN)
262+
hap = HomematicipHAP(hass, hmip_config_entry)
263+
assert hap
264+
265+
simple_mock_home = AsyncMock(spec=AsyncHome, autospec=True)
266+
267+
await hap.async_connect(simple_mock_home)
268+
269+
simple_mock_home.set_on_connected_handler.assert_called_once()
270+
simple_mock_home.set_on_disconnected_handler.assert_called_once()
271+
simple_mock_home.set_on_reconnect_handler.assert_called_once()
272+
simple_mock_home.enable_events.assert_called_once()

0 commit comments

Comments
 (0)