From 7ad3be89b96704c2c262ea518955acf07ac8ae60 Mon Sep 17 00:00:00 2001 From: KiraPC Date: Thu, 18 Jul 2024 11:56:07 +0200 Subject: [PATCH] #50 remove unregistered devices --- custom_components/switchbotremote/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/custom_components/switchbotremote/__init__.py b/custom_components/switchbotremote/__init__.py index 1382784..fce3c3f 100644 --- a/custom_components/switchbotremote/__init__.py +++ b/custom_components/switchbotremote/__init__.py @@ -8,6 +8,9 @@ from .client import SwitchBot from .const import DOMAIN +from homeassistant.helpers import ( + device_registry as dr, +) PLATFORMS: list[Platform] = [ Platform.CLIMATE, @@ -37,6 +40,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + device_registry = dr.async_get(hass) + for device_entry in dr.async_entries_for_config_entry( + device_registry, entry.entry_id + ): + device_id = list(device_entry.identifiers)[0][1] + registered = next((remote for remote in remotes if remote.id == device_id), None) + + if not registered: + device_registry.async_remove_device(device_entry.id) + return True