From 1692808d5b4e91c3316ac04dcf93e1213dec9a1c Mon Sep 17 00:00:00 2001
From: On Freund <onfreund@gmail.com>
Date: Thu, 1 Sep 2022 21:02:09 +0300
Subject: [PATCH] Increase sleep in Risco setup (#77619)

---
 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_alarm_control_panel.py | 3 +++
 tests/components/risco/test_config_flow.py         | 6 ------
 8 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/homeassistant/components/risco/__init__.py b/homeassistant/components/risco/__init__.py
index e95b3016139..179ddd5cad6 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 1befe626347..5e1cdb75b5a 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 @@ from .const import (
     DEFAULT_OPTIONS,
     DOMAIN,
     RISCO_STATES,
-    SLEEP_INTERVAL,
     TYPE_LOCAL,
 )
 
@@ -150,9 +148,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
                 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 f4ac170d3c7..9f0e71701c6 100644
--- a/homeassistant/components/risco/const.py
+++ b/homeassistant/components/risco/const.py
@@ -46,5 +46,3 @@ DEFAULT_OPTIONS = {
     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 38035e22c62..9703b5775bc 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 3e778246ec2..3fa89f3898e 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 5e1c1b44334..e9bbc312652 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_alarm_control_panel.py b/tests/components/risco/test_alarm_control_panel.py
index 0014e712ab1..1625e78ece6 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(
diff --git a/tests/components/risco/test_config_flow.py b/tests/components/risco/test_config_flow.py
index a39a724d7b9..396aad8015d 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:
-- 
GitLab