From e460f472df4ce627aa2ed515cfa3d36cc5ce9cad Mon Sep 17 00:00:00 2001 From: On Freund Date: Wed, 31 Aug 2022 21:54:48 +0300 Subject: [PATCH 1/2] Increase sleep in Risco setup Move sleep to library --- homeassistant/components/risco/__init__.py | 4 ++++ homeassistant/components/risco/config_flow.py | 5 ----- homeassistant/components/risco/const.py | 2 -- homeassistant/components/risco/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/risco/test_config_flow.py | 6 ------ 7 files changed, 7 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/risco/__init__.py b/homeassistant/components/risco/__init__.py index e95b3016139de4..179ddd5cad64fd 100644 --- a/homeassistant/components/risco/__init__.py +++ b/homeassistant/components/risco/__init__.py @@ -154,6 +154,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: + if is_local(entry): + local_data: LocalData = hass.data[DOMAIN][entry.entry_id] + await local_data.system.disconnect() + hass.data[DOMAIN].pop(entry.entry_id) return unload_ok diff --git a/homeassistant/components/risco/config_flow.py b/homeassistant/components/risco/config_flow.py index 1befe6263479b8..5e1cdb75b5a977 100644 --- a/homeassistant/components/risco/config_flow.py +++ b/homeassistant/components/risco/config_flow.py @@ -1,7 +1,6 @@ """Config flow for Risco integration.""" from __future__ import annotations -import asyncio from collections.abc import Mapping import logging @@ -32,7 +31,6 @@ DEFAULT_OPTIONS, DOMAIN, RISCO_STATES, - SLEEP_INTERVAL, TYPE_LOCAL, ) @@ -150,9 +148,6 @@ async def async_step_local(self, user_input=None): await self.async_set_unique_id(info["title"]) self._abort_if_unique_id_configured() - # Risco can hang if we don't wait before creating a new connection - await asyncio.sleep(SLEEP_INTERVAL) - return self.async_create_entry( title=info["title"], data={**user_input, **{CONF_TYPE: TYPE_LOCAL}} ) diff --git a/homeassistant/components/risco/const.py b/homeassistant/components/risco/const.py index f4ac170d3c7e29..9f0e71701c65b9 100644 --- a/homeassistant/components/risco/const.py +++ b/homeassistant/components/risco/const.py @@ -46,5 +46,3 @@ CONF_RISCO_STATES_TO_HA: DEFAULT_RISCO_STATES_TO_HA, CONF_HA_STATES_TO_RISCO: DEFAULT_HA_STATES_TO_RISCO, } - -SLEEP_INTERVAL = 1 diff --git a/homeassistant/components/risco/manifest.json b/homeassistant/components/risco/manifest.json index 38035e22c62319..9703b5775bc425 100644 --- a/homeassistant/components/risco/manifest.json +++ b/homeassistant/components/risco/manifest.json @@ -3,7 +3,7 @@ "name": "Risco", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/risco", - "requirements": ["pyrisco==0.5.3"], + "requirements": ["pyrisco==0.5.4"], "codeowners": ["@OnFreund"], "quality_scale": "platinum", "iot_class": "local_push", diff --git a/requirements_all.txt b/requirements_all.txt index 7486e46e1dc491..4072e0d0ad9ec1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1814,7 +1814,7 @@ pyrecswitch==1.0.2 pyrepetierng==0.1.0 # homeassistant.components.risco -pyrisco==0.5.3 +pyrisco==0.5.4 # homeassistant.components.rituals_perfume_genie pyrituals==0.0.6 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 4544ad2a65e74f..69a30a355c0d6c 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1270,7 +1270,7 @@ pyps4-2ndscreen==1.3.1 pyqwikswitch==0.93 # homeassistant.components.risco -pyrisco==0.5.3 +pyrisco==0.5.4 # homeassistant.components.rituals_perfume_genie pyrituals==0.0.6 diff --git a/tests/components/risco/test_config_flow.py b/tests/components/risco/test_config_flow.py index a39a724d7b94bf..396aad8015dd4e 100644 --- a/tests/components/risco/test_config_flow.py +++ b/tests/components/risco/test_config_flow.py @@ -72,9 +72,6 @@ async def test_cloud_form(hass): ), patch( "homeassistant.components.risco.config_flow.RiscoCloud.close" ) as mock_close, patch( - "homeassistant.components.risco.config_flow.SLEEP_INTERVAL", - 0, - ), patch( "homeassistant.components.risco.async_setup_entry", return_value=True, ) as mock_setup_entry: @@ -168,9 +165,6 @@ async def test_local_form(hass): ), patch( "homeassistant.components.risco.config_flow.RiscoLocal.disconnect" ) as mock_close, patch( - "homeassistant.components.risco.config_flow.SLEEP_INTERVAL", - 0, - ), patch( "homeassistant.components.risco.async_setup_entry", return_value=True, ) as mock_setup_entry: From afc49075f805ec1c37fb5313808c075262a571b6 Mon Sep 17 00:00:00 2001 From: On Freund Date: Thu, 1 Sep 2022 19:43:32 +0300 Subject: [PATCH 2/2] Test disconnect is called on unload --- tests/components/risco/test_alarm_control_panel.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/components/risco/test_alarm_control_panel.py b/tests/components/risco/test_alarm_control_panel.py index 0014e712ab1cbd..1625e78ece6905 100644 --- a/tests/components/risco/test_alarm_control_panel.py +++ b/tests/components/risco/test_alarm_control_panel.py @@ -479,6 +479,9 @@ async def test_local_setup(hass, two_part_local_alarm, setup_risco_local): device = registry.async_get_device({(DOMAIN, TEST_SITE_UUID + "_1_local")}) assert device is not None assert device.manufacturer == "Risco" + with patch("homeassistant.components.risco.RiscoLocal.disconnect") as mock_close: + await hass.config_entries.async_unload(setup_risco_local.entry_id) + mock_close.assert_awaited_once() async def _check_local_state(