Removal of stale add-on devices on startup (#52245)

This commit is contained in:
Joakim Sørensen 2021-06-28 13:49:58 +02:00 committed by GitHub
parent 3e1d32f4e0
commit 8255a2f6c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View File

@ -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)
)

View File

@ -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"