Clean up homematicip cloud (#19481)

* Better logging, remove unused method, re-try handling fix. Other minor fixes.

* fix test

* typo fix
This commit is contained in:
sander76 2018-12-25 09:40:36 +01:00 committed by Martin Hjelmare
parent b2081c579b
commit 18b7f74ad7
2 changed files with 13 additions and 23 deletions

View file

@ -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(

View file

@ -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',