mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Remove global registry reference in coordinator for UptimeRobot (#142938)
* Remove global registry reference in coordinator for UptimeRobot * rework current_monitors listing * fix logic
This commit is contained in:
parent
13d530d110
commit
535d128f8a
@ -39,7 +39,6 @@ class UptimeRobotDataUpdateCoordinator(DataUpdateCoordinator[list[UptimeRobotMon
|
|||||||
name=DOMAIN,
|
name=DOMAIN,
|
||||||
update_interval=COORDINATOR_UPDATE_INTERVAL,
|
update_interval=COORDINATOR_UPDATE_INTERVAL,
|
||||||
)
|
)
|
||||||
self._device_registry = dr.async_get(hass)
|
|
||||||
self.api = api
|
self.api = api
|
||||||
|
|
||||||
async def _async_update_data(self) -> list[UptimeRobotMonitor]:
|
async def _async_update_data(self) -> list[UptimeRobotMonitor]:
|
||||||
@ -56,23 +55,21 @@ class UptimeRobotDataUpdateCoordinator(DataUpdateCoordinator[list[UptimeRobotMon
|
|||||||
|
|
||||||
monitors: list[UptimeRobotMonitor] = response.data
|
monitors: list[UptimeRobotMonitor] = response.data
|
||||||
|
|
||||||
current_monitors = {
|
current_monitors = (
|
||||||
list(device.identifiers)[0][1]
|
{str(monitor.id) for monitor in self.data} if self.data else set()
|
||||||
for device in dr.async_entries_for_config_entry(
|
)
|
||||||
self._device_registry, self.config_entry.entry_id
|
|
||||||
)
|
|
||||||
}
|
|
||||||
new_monitors = {str(monitor.id) for monitor in monitors}
|
new_monitors = {str(monitor.id) for monitor in monitors}
|
||||||
if stale_monitors := current_monitors - new_monitors:
|
if stale_monitors := current_monitors - new_monitors:
|
||||||
for monitor_id in stale_monitors:
|
for monitor_id in stale_monitors:
|
||||||
if device := self._device_registry.async_get_device(
|
device_registry = dr.async_get(self.hass)
|
||||||
|
if device := device_registry.async_get_device(
|
||||||
identifiers={(DOMAIN, monitor_id)}
|
identifiers={(DOMAIN, monitor_id)}
|
||||||
):
|
):
|
||||||
self._device_registry.async_remove_device(device.id)
|
device_registry.async_remove_device(device.id)
|
||||||
|
|
||||||
# If there are new monitors, we should reload the config entry so we can
|
# If there are new monitors, we should reload the config entry so we can
|
||||||
# create new devices and entities.
|
# create new devices and entities.
|
||||||
if self.data and new_monitors - {str(monitor.id) for monitor in self.data}:
|
if self.data and new_monitors - current_monitors:
|
||||||
self.hass.async_create_task(
|
self.hass.async_create_task(
|
||||||
self.hass.config_entries.async_reload(self.config_entry.entry_id)
|
self.hass.config_entries.async_reload(self.config_entry.entry_id)
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user