From cf6eddee74eee8763fa603d5913540bae47bc0b3 Mon Sep 17 00:00:00 2001
From: Jan-Philipp Benecke <jan-philipp@bnck.me>
Date: Tue, 19 Sep 2023 09:45:56 +0200
Subject: [PATCH] Move uptimerobot coordinator to its own file (#100558)

* Move uptimerobot coordinator to its own file

* Fix import of coordinator in platforms
---
 .../components/uptimerobot/__init__.py        | 72 +----------------
 .../components/uptimerobot/binary_sensor.py   |  2 +-
 .../components/uptimerobot/coordinator.py     | 78 +++++++++++++++++++
 .../components/uptimerobot/diagnostics.py     |  2 +-
 .../components/uptimerobot/sensor.py          |  2 +-
 .../components/uptimerobot/switch.py          |  2 +-
 6 files changed, 85 insertions(+), 73 deletions(-)
 create mode 100644 homeassistant/components/uptimerobot/coordinator.py

diff --git a/homeassistant/components/uptimerobot/__init__.py b/homeassistant/components/uptimerobot/__init__.py
index 3cb119837d7..58979d7defb 100644
--- a/homeassistant/components/uptimerobot/__init__.py
+++ b/homeassistant/components/uptimerobot/__init__.py
@@ -1,12 +1,7 @@
 """The UptimeRobot integration."""
 from __future__ import annotations
 
-from pyuptimerobot import (
-    UptimeRobot,
-    UptimeRobotAuthenticationException,
-    UptimeRobotException,
-    UptimeRobotMonitor,
-)
+from pyuptimerobot import UptimeRobot
 
 from homeassistant.config_entries import ConfigEntry
 from homeassistant.const import CONF_API_KEY
@@ -14,9 +9,9 @@ from homeassistant.core import HomeAssistant
 from homeassistant.exceptions import ConfigEntryAuthFailed
 from homeassistant.helpers import device_registry as dr
 from homeassistant.helpers.aiohttp_client import async_get_clientsession
-from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
 
-from .const import API_ATTR_OK, COORDINATOR_UPDATE_INTERVAL, DOMAIN, LOGGER, PLATFORMS
+from .const import DOMAIN, PLATFORMS
+from .coordinator import UptimeRobotDataUpdateCoordinator
 
 
 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
@@ -51,64 +46,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
         hass.data[DOMAIN].pop(entry.entry_id)
 
     return unload_ok
-
-
-class UptimeRobotDataUpdateCoordinator(DataUpdateCoordinator[list[UptimeRobotMonitor]]):
-    """Data update coordinator for UptimeRobot."""
-
-    config_entry: ConfigEntry
-
-    def __init__(
-        self,
-        hass: HomeAssistant,
-        config_entry_id: str,
-        dev_reg: dr.DeviceRegistry,
-        api: UptimeRobot,
-    ) -> None:
-        """Initialize coordinator."""
-        super().__init__(
-            hass,
-            LOGGER,
-            name=DOMAIN,
-            update_interval=COORDINATOR_UPDATE_INTERVAL,
-        )
-        self._config_entry_id = config_entry_id
-        self._device_registry = dev_reg
-        self.api = api
-
-    async def _async_update_data(self) -> list[UptimeRobotMonitor]:
-        """Update data."""
-        try:
-            response = await self.api.async_get_monitors()
-        except UptimeRobotAuthenticationException as exception:
-            raise ConfigEntryAuthFailed(exception) from exception
-        except UptimeRobotException as exception:
-            raise UpdateFailed(exception) from exception
-
-        if response.status != API_ATTR_OK:
-            raise UpdateFailed(response.error.message)
-
-        monitors: list[UptimeRobotMonitor] = response.data
-
-        current_monitors = {
-            list(device.identifiers)[0][1]
-            for device in dr.async_entries_for_config_entry(
-                self._device_registry, self._config_entry_id
-            )
-        }
-        new_monitors = {str(monitor.id) for monitor in monitors}
-        if stale_monitors := current_monitors - new_monitors:
-            for monitor_id in stale_monitors:
-                if device := self._device_registry.async_get_device(
-                    identifiers={(DOMAIN, monitor_id)}
-                ):
-                    self._device_registry.async_remove_device(device.id)
-
-        # If there are new monitors, we should reload the config entry so we can
-        # create new devices and entities.
-        if self.data and new_monitors - {str(monitor.id) for monitor in self.data}:
-            self.hass.async_create_task(
-                self.hass.config_entries.async_reload(self._config_entry_id)
-            )
-
-        return monitors
diff --git a/homeassistant/components/uptimerobot/binary_sensor.py b/homeassistant/components/uptimerobot/binary_sensor.py
index a4aeeb3151b..2710d5166c2 100644
--- a/homeassistant/components/uptimerobot/binary_sensor.py
+++ b/homeassistant/components/uptimerobot/binary_sensor.py
@@ -10,8 +10,8 @@ from homeassistant.config_entries import ConfigEntry
 from homeassistant.core import HomeAssistant
 from homeassistant.helpers.entity_platform import AddEntitiesCallback
 
-from . import UptimeRobotDataUpdateCoordinator
 from .const import DOMAIN
+from .coordinator import UptimeRobotDataUpdateCoordinator
 from .entity import UptimeRobotEntity
 
 
diff --git a/homeassistant/components/uptimerobot/coordinator.py b/homeassistant/components/uptimerobot/coordinator.py
new file mode 100644
index 00000000000..4c1d3ea2c78
--- /dev/null
+++ b/homeassistant/components/uptimerobot/coordinator.py
@@ -0,0 +1,78 @@
+"""DataUpdateCoordinator for the uptimerobot integration."""
+from __future__ import annotations
+
+from pyuptimerobot import (
+    UptimeRobot,
+    UptimeRobotAuthenticationException,
+    UptimeRobotException,
+    UptimeRobotMonitor,
+)
+
+from homeassistant.config_entries import ConfigEntry
+from homeassistant.core import HomeAssistant
+from homeassistant.exceptions import ConfigEntryAuthFailed
+from homeassistant.helpers import device_registry as dr
+from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
+
+from .const import API_ATTR_OK, COORDINATOR_UPDATE_INTERVAL, DOMAIN, LOGGER
+
+
+class UptimeRobotDataUpdateCoordinator(DataUpdateCoordinator[list[UptimeRobotMonitor]]):
+    """Data update coordinator for UptimeRobot."""
+
+    config_entry: ConfigEntry
+
+    def __init__(
+        self,
+        hass: HomeAssistant,
+        config_entry_id: str,
+        dev_reg: dr.DeviceRegistry,
+        api: UptimeRobot,
+    ) -> None:
+        """Initialize coordinator."""
+        super().__init__(
+            hass,
+            LOGGER,
+            name=DOMAIN,
+            update_interval=COORDINATOR_UPDATE_INTERVAL,
+        )
+        self._config_entry_id = config_entry_id
+        self._device_registry = dev_reg
+        self.api = api
+
+    async def _async_update_data(self) -> list[UptimeRobotMonitor]:
+        """Update data."""
+        try:
+            response = await self.api.async_get_monitors()
+        except UptimeRobotAuthenticationException as exception:
+            raise ConfigEntryAuthFailed(exception) from exception
+        except UptimeRobotException as exception:
+            raise UpdateFailed(exception) from exception
+
+        if response.status != API_ATTR_OK:
+            raise UpdateFailed(response.error.message)
+
+        monitors: list[UptimeRobotMonitor] = response.data
+
+        current_monitors = {
+            list(device.identifiers)[0][1]
+            for device in dr.async_entries_for_config_entry(
+                self._device_registry, self._config_entry_id
+            )
+        }
+        new_monitors = {str(monitor.id) for monitor in monitors}
+        if stale_monitors := current_monitors - new_monitors:
+            for monitor_id in stale_monitors:
+                if device := self._device_registry.async_get_device(
+                    identifiers={(DOMAIN, monitor_id)}
+                ):
+                    self._device_registry.async_remove_device(device.id)
+
+        # If there are new monitors, we should reload the config entry so we can
+        # create new devices and entities.
+        if self.data and new_monitors - {str(monitor.id) for monitor in self.data}:
+            self.hass.async_create_task(
+                self.hass.config_entries.async_reload(self._config_entry_id)
+            )
+
+        return monitors
diff --git a/homeassistant/components/uptimerobot/diagnostics.py b/homeassistant/components/uptimerobot/diagnostics.py
index 94710235ab7..15173a5e43c 100644
--- a/homeassistant/components/uptimerobot/diagnostics.py
+++ b/homeassistant/components/uptimerobot/diagnostics.py
@@ -8,8 +8,8 @@ from pyuptimerobot import UptimeRobotException
 from homeassistant.config_entries import ConfigEntry
 from homeassistant.core import HomeAssistant
 
-from . import UptimeRobotDataUpdateCoordinator
 from .const import DOMAIN
+from .coordinator import UptimeRobotDataUpdateCoordinator
 
 
 async def async_get_config_entry_diagnostics(
diff --git a/homeassistant/components/uptimerobot/sensor.py b/homeassistant/components/uptimerobot/sensor.py
index f9d4097fe40..4ae40bf4134 100644
--- a/homeassistant/components/uptimerobot/sensor.py
+++ b/homeassistant/components/uptimerobot/sensor.py
@@ -13,8 +13,8 @@ from homeassistant.const import EntityCategory
 from homeassistant.core import HomeAssistant
 from homeassistant.helpers.entity_platform import AddEntitiesCallback
 
-from . import UptimeRobotDataUpdateCoordinator
 from .const import DOMAIN
+from .coordinator import UptimeRobotDataUpdateCoordinator
 from .entity import UptimeRobotEntity
 
 
diff --git a/homeassistant/components/uptimerobot/switch.py b/homeassistant/components/uptimerobot/switch.py
index 397d2085357..3406c9fe21a 100644
--- a/homeassistant/components/uptimerobot/switch.py
+++ b/homeassistant/components/uptimerobot/switch.py
@@ -14,8 +14,8 @@ from homeassistant.config_entries import ConfigEntry
 from homeassistant.core import HomeAssistant
 from homeassistant.helpers.entity_platform import AddEntitiesCallback
 
-from . import UptimeRobotDataUpdateCoordinator
 from .const import API_ATTR_OK, DOMAIN, LOGGER
+from .coordinator import UptimeRobotDataUpdateCoordinator
 from .entity import UptimeRobotEntity
 
 
-- 
GitLab