Address late hassio review (#47229)

* hassio code cleanup to address comments in #46342

* fix code
This commit is contained in:
Raman Gupta 2021-03-01 12:10:51 -05:00 committed by GitHub
parent 61f509bdd8
commit 3c290c9a44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 23 deletions

View File

@ -522,15 +522,17 @@ def async_register_addons_in_dev_reg(
) -> None:
"""Register addons in the device registry."""
for addon in addons:
dev_reg.async_get_or_create(
config_entry_id=entry_id,
identifiers={(DOMAIN, addon[ATTR_SLUG])},
manufacturer=addon.get(ATTR_REPOSITORY) or addon.get(ATTR_URL) or "unknown",
model="Home Assistant Add-on",
sw_version=addon[ATTR_VERSION],
name=addon[ATTR_NAME],
entry_type=ATTR_SERVICE,
)
params = {
"config_entry_id": entry_id,
"identifiers": {(DOMAIN, addon[ATTR_SLUG])},
"model": "Home Assistant Add-on",
"sw_version": addon[ATTR_VERSION],
"name": addon[ATTR_NAME],
"entry_type": ATTR_SERVICE,
}
if manufacturer := addon.get(ATTR_REPOSITORY) or addon.get(ATTR_URL):
params["manufacturer"] = manufacturer
dev_reg.async_get_or_create(**params)
@callback
@ -538,15 +540,16 @@ def async_register_os_in_dev_reg(
entry_id: str, dev_reg: DeviceRegistry, os_dict: Dict[str, Any]
) -> None:
"""Register OS in the device registry."""
dev_reg.async_get_or_create(
config_entry_id=entry_id,
identifiers={(DOMAIN, "OS")},
manufacturer="Home Assistant",
model="Home Assistant Operating System",
sw_version=os_dict[ATTR_VERSION],
name="Home Assistant Operating System",
entry_type=ATTR_SERVICE,
)
params = {
"config_entry_id": entry_id,
"identifiers": {(DOMAIN, "OS")},
"manufacturer": "Home Assistant",
"model": "Home Assistant Operating System",
"sw_version": os_dict[ATTR_VERSION],
"name": "Home Assistant Operating System",
"entry_type": ATTR_SERVICE,
}
dev_reg.async_get_or_create(**params)
@callback
@ -600,15 +603,13 @@ class HassioDataUpdateCoordinator(DataUpdateCoordinator):
return new_data
# Remove add-ons that are no longer installed from device registry
if removed_addons := list(
set(self.data["addons"].keys()) - set(new_data["addons"].keys())
):
if removed_addons := list(set(self.data["addons"]) - set(new_data["addons"])):
async_remove_addons_from_dev_reg(self.dev_reg, removed_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"].keys()) - set(self.data["addons"].keys())):
if 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

@ -393,7 +393,7 @@ async def test_entry_load_and_unload(hass):
assert BINARY_SENSOR_DOMAIN in hass.config.components
assert ADDONS_COORDINATOR in hass.data
assert await config_entry.async_unload(hass)
assert await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()
assert ADDONS_COORDINATOR not in hass.data