From 18b7f74ad7d21aabbf92bdd7a89f13c77f25ac7c Mon Sep 17 00:00:00 2001 From: sander76 Date: Tue, 25 Dec 2018 09:40:36 +0100 Subject: [PATCH] Clean up homematicip cloud (#19481) * Better logging, remove unused method, re-try handling fix. Other minor fixes. * fix test * typo fix --- .../components/homematicip_cloud/hap.py | 33 +++++++------------ .../components/homematicip_cloud/test_hap.py | 3 +- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/homematicip_cloud/hap.py b/homeassistant/components/homematicip_cloud/hap.py index b4605aa37f0..37507a1fca8 100644 --- a/homeassistant/components/homematicip_cloud/hap.py +++ b/homeassistant/components/homematicip_cloud/hap.py @@ -96,7 +96,7 @@ class HomematicipHAP: self.config_entry.data.get(HMIPC_NAME) ) except HmipcConnectionError: - retry_delay = 2 ** min(tries + 1, 6) + retry_delay = 2 ** min(tries, 8) _LOGGER.error("Error connecting to HomematicIP with HAP %s. " "Retrying in %d seconds", self.config_entry.data.get(HMIPC_HAPID), retry_delay) @@ -138,6 +138,8 @@ class HomematicipHAP: self._accesspoint_connected = False self.set_all_to_unavailable() elif not self._accesspoint_connected: + # Now the HOME_CHANGED event has fired indicating the access + # point has reconnected to the cloud again. # Explicitly getting an update as device states might have # changed during access point disconnect.""" @@ -173,43 +175,30 @@ class HomematicipHAP: for device in self.home.devices: device.fire_update_event() - async def _handle_connection(self): - """Handle websocket connection.""" - from homematicip.base.base_connection import HmipConnectionError - - try: - await self.home.get_current_state() - except HmipConnectionError: - return - hmip_events = await self.home.enable_events() - try: - await hmip_events - except HmipConnectionError: - return - async def async_connect(self): """Start WebSocket connection.""" from homematicip.base.base_connection import HmipConnectionError tries = 0 while True: + retry_delay = 2 ** min(tries, 8) + try: await self.home.get_current_state() hmip_events = await self.home.enable_events() tries = 0 await hmip_events except HmipConnectionError: - pass + _LOGGER.error("Error connecting to HomematicIP with HAP %s. " + "Retrying in %d seconds", + self.config_entry.data.get(HMIPC_HAPID), + retry_delay) if self._ws_close_requested: break self._ws_close_requested = False - tries += 1 - retry_delay = 2 ** min(tries + 1, 6) - _LOGGER.error("Error connecting to HomematicIP with HAP %s. " - "Retrying in %d seconds", - self.config_entry.data.get(HMIPC_HAPID), retry_delay) + try: self._retry_task = self.hass.async_create_task(asyncio.sleep( retry_delay, loop=self.hass.loop)) @@ -224,7 +213,7 @@ class HomematicipHAP: self._retry_setup.cancel() if self._retry_task is not None: self._retry_task.cancel() - self.home.disable_events() + await self.home.disable_events() _LOGGER.info("Closed connection to HomematicIP cloud server") for component in COMPONENTS: await self.hass.config_entries.async_forward_entry_unload( diff --git a/tests/components/homematicip_cloud/test_hap.py b/tests/components/homematicip_cloud/test_hap.py index 476bed368d7..92f58b37662 100644 --- a/tests/components/homematicip_cloud/test_hap.py +++ b/tests/components/homematicip_cloud/test_hap.py @@ -3,7 +3,7 @@ from unittest.mock import Mock, patch from homeassistant.components.homematicip_cloud import hap as hmipc from homeassistant.components.homematicip_cloud import const, errors -from tests.common import mock_coro +from tests.common import mock_coro, mock_coro_func async def test_auth_setup(hass): @@ -95,6 +95,7 @@ async def test_hap_reset_unloads_entry_if_setup(): hass = Mock() entry = Mock() home = Mock() + home.disable_events = mock_coro_func() entry.data = { hmipc.HMIPC_HAPID: 'ABC123', hmipc.HMIPC_AUTHTOKEN: '123',