From 1f7ebe9249f3490bb94df893b77c2915fbdb1fbd Mon Sep 17 00:00:00 2001
From: Mark Adkins <mark@bymark.co>
Date: Tue, 4 Apr 2023 21:06:24 -0400
Subject: [PATCH] SharkIQ Hotfix - Handle current installations by using
 default `REGION` (#90741)

* Add default region on async_setup_entry

* Move logic to migration function

* Move update logic back to setup function, but updates the config if needed.

* Remove commented out code

* Update Tests & Config setting method

* Update homeassistant/components/sharkiq/__init__.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Update homeassistant/components/sharkiq/__init__.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Accept Suggestions & Formatting

---------

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
---
 homeassistant/components/sharkiq/__init__.py | 15 +++++++++++-
 tests/components/sharkiq/const.py            |  4 ++++
 tests/components/sharkiq/test_config_flow.py | 25 ++++++++++++++++++--
 3 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/homeassistant/components/sharkiq/__init__.py b/homeassistant/components/sharkiq/__init__.py
index 738dd595a5a..b6cae8ad605 100644
--- a/homeassistant/components/sharkiq/__init__.py
+++ b/homeassistant/components/sharkiq/__init__.py
@@ -17,7 +17,14 @@ from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_USERNAME
 from homeassistant.core import HomeAssistant
 from homeassistant.helpers.aiohttp_client import async_get_clientsession
 
-from .const import API_TIMEOUT, DOMAIN, LOGGER, PLATFORMS, SHARKIQ_REGION_EUROPE
+from .const import (
+    API_TIMEOUT,
+    DOMAIN,
+    LOGGER,
+    PLATFORMS,
+    SHARKIQ_REGION_DEFAULT,
+    SHARKIQ_REGION_EUROPE,
+)
 from .update_coordinator import SharkIqUpdateCoordinator
 
 
@@ -43,6 +50,12 @@ async def async_connect_or_timeout(ayla_api: AylaApi) -> bool:
 
 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
     """Initialize the sharkiq platform via config entry."""
+    if CONF_REGION not in config_entry.data:
+        hass.config_entries.async_update_entry(
+            config_entry,
+            data={**config_entry.data, CONF_REGION: SHARKIQ_REGION_DEFAULT},
+        )
+
     ayla_api = get_ayla_api(
         username=config_entry.data[CONF_USERNAME],
         password=config_entry.data[CONF_PASSWORD],
diff --git a/tests/components/sharkiq/const.py b/tests/components/sharkiq/const.py
index 8ec7d424ffa..b4f9d72dafd 100644
--- a/tests/components/sharkiq/const.py
+++ b/tests/components/sharkiq/const.py
@@ -76,4 +76,8 @@ CONFIG = {
     CONF_PASSWORD: TEST_PASSWORD,
     CONF_REGION: TEST_REGION,
 }
+CONFIG_NO_REGION = {
+    CONF_USERNAME: TEST_USERNAME,
+    CONF_PASSWORD: TEST_PASSWORD,
+}
 ENTRY_ID = "0123456789abcdef0123456789abcdef"
diff --git a/tests/components/sharkiq/test_config_flow.py b/tests/components/sharkiq/test_config_flow.py
index c7a0603f865..248bb57f04c 100644
--- a/tests/components/sharkiq/test_config_flow.py
+++ b/tests/components/sharkiq/test_config_flow.py
@@ -8,12 +8,32 @@ from sharkiq import AylaApi, SharkIqAuthError, SharkIqError
 from homeassistant import config_entries
 from homeassistant.components.sharkiq.const import DOMAIN
 from homeassistant.core import HomeAssistant
-
-from .const import CONFIG, TEST_PASSWORD, TEST_REGION, TEST_USERNAME, UNIQUE_ID
+from homeassistant.setup import async_setup_component
+
+from .const import (
+    CONFIG,
+    CONFIG_NO_REGION,
+    TEST_PASSWORD,
+    TEST_REGION,
+    TEST_USERNAME,
+    UNIQUE_ID,
+)
 
 from tests.common import MockConfigEntry
 
 
+async def test_setup_success_no_region(hass: HomeAssistant) -> None:
+    """Test reauth flow."""
+    mock_config = MockConfigEntry(
+        domain=DOMAIN, unique_id=UNIQUE_ID, data=CONFIG_NO_REGION
+    )
+    mock_config.add_to_hass(hass)
+
+    result = await async_setup_component(hass=hass, domain=DOMAIN, config=mock_config)
+
+    assert result is True
+
+
 async def test_form(hass: HomeAssistant) -> None:
     """Test we get the form."""
 
@@ -39,6 +59,7 @@ async def test_form(hass: HomeAssistant) -> None:
         "password": TEST_PASSWORD,
         "region": TEST_REGION,
     }
+
     await hass.async_block_till_done()
     mock_setup_entry.assert_called_once()
 
-- 
GitLab