diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index 1feb34cd173..6f983a04e79 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -47,6 +47,7 @@ from .const import ( ATTR_URL, ATTR_VERSION, DOMAIN, + SupervisorEntityModel, ) from .discovery import async_setup_discovery_view from .handler import HassIO, HassioAPIError, api_data @@ -597,7 +598,7 @@ def async_register_addons_in_dev_reg( params = { "config_entry_id": entry_id, "identifiers": {(DOMAIN, addon[ATTR_SLUG])}, - "model": "Home Assistant Add-on", + "model": SupervisorEntityModel.ADDON, "sw_version": addon[ATTR_VERSION], "name": addon[ATTR_NAME], "entry_type": ATTR_SERVICE, @@ -616,7 +617,7 @@ def async_register_os_in_dev_reg( "config_entry_id": entry_id, "identifiers": {(DOMAIN, "OS")}, "manufacturer": "Home Assistant", - "model": "Home Assistant Operating System", + "model": SupervisorEntityModel.OS, "sw_version": os_dict[ATTR_VERSION], "name": "Home Assistant Operating System", "entry_type": ATTR_SERVICE, @@ -625,9 +626,7 @@ def async_register_os_in_dev_reg( @callback -def async_remove_addons_from_dev_reg( - dev_reg: DeviceRegistry, addons: list[dict[str, Any]] -) -> None: +def async_remove_addons_from_dev_reg(dev_reg: DeviceRegistry, addons: set[str]) -> None: """Remove addons from the device registry.""" for addon_slug in addons: if dev := dev_reg.async_get_device({(DOMAIN, addon_slug)}): @@ -684,16 +683,21 @@ class HassioDataUpdateCoordinator(DataUpdateCoordinator): async_register_os_in_dev_reg( self.entry_id, self.dev_reg, new_data["os"] ) - return new_data # Remove add-ons that are no longer installed from device registry - if removed_addons := list(set(self.data["addons"]) - set(new_data["addons"])): - async_remove_addons_from_dev_reg(self.dev_reg, removed_addons) + supervisor_addon_devices = { + list(device.identifiers)[0][1] + for device in self.dev_reg.devices.values() + if self.entry_id in device.config_entries + and device.model == SupervisorEntityModel.ADDON + } + if stale_addons := supervisor_addon_devices - set(new_data["addons"]): + async_remove_addons_from_dev_reg(self.dev_reg, stale_addons) # If there are new add-ons, we should reload the config entry so we can # create new devices and entities. We can return an empty dict because # coordinator will be recreated. - if list(set(new_data["addons"]) - set(self.data["addons"])): + if self.data and list(set(new_data["addons"]) - set(self.data["addons"])): self.hass.async_create_task( self.hass.config_entries.async_reload(self.entry_id) ) diff --git a/homeassistant/components/hassio/const.py b/homeassistant/components/hassio/const.py index 435d42349fd..6104e57fb17 100644 --- a/homeassistant/components/hassio/const.py +++ b/homeassistant/components/hassio/const.py @@ -1,4 +1,5 @@ """Hass.io const variables.""" +from enum import Enum DOMAIN = "hassio" @@ -46,3 +47,10 @@ ATTR_UPDATE_AVAILABLE = "update_available" ATTR_SLUG = "slug" ATTR_URL = "url" ATTR_REPOSITORY = "repository" + + +class SupervisorEntityModel(str, Enum): + """Supervisor entity model.""" + + ADDON = "Home Assistant Add-on" + OS = "Home Assistant Operating System"