core/homeassistant/components/hassio/entity.py

94 lines
2.7 KiB
Python
Raw Normal View History

Add hassio addon_update service and hassio config entry with addon and OS devices and entities (#46342) * add addon_update service, use config flow to set up config entry, create disabled sensors * move most of entity logic to common entity class, improve device info, get rid of config_flow user step * fix setup logic * additional refactor * fix refactored logic * fix config flow tests * add test for addon_update service and get_addons_info * add entry setup and unload test and fix update coordinator * handle if entry setup calls unload * return nothing for coordinator if entry is being reloaded because coordinator will get recreated anyway * remove entry when HA instance is no longer hassio and add corresponding test * handle adding and removing device registry entries * better config entry reload logic * fix comment * bugfix * fix flake error * switch pass to return * use repository attribute for model and fallback to url * use custom 'system' source since hassio source is misleading * Update homeassistant/components/hassio/entity.py Co-authored-by: Franck Nijhof <frenck@frenck.nl> * update remove addons function name * Update homeassistant/components/hassio/__init__.py Co-authored-by: Franck Nijhof <frenck@frenck.nl> * fix import * pop coordinator after unload * additional fixes * always pass in sensor name when creating entity * prefix one more function with async and fix tests * use supervisor info for addons since list is already filtered on what's installed * remove unused service * update sensor names * remove added handler function * use walrus * add OS device and sensors * fix * re-add addon_update service schema * add more test coverage and exclude entities from tests * check if instance is using hass OS in order to create OS entities Co-authored-by: Franck Nijhof <frenck@frenck.nl>
2021-03-01 08:41:04 +00:00
"""Base for Hass.io entities."""
from typing import Any, Dict
from homeassistant.const import ATTR_NAME
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import DOMAIN, HassioDataUpdateCoordinator
from .const import ATTR_SLUG
class HassioAddonEntity(CoordinatorEntity):
"""Base entity for a Hass.io add-on."""
def __init__(
self,
coordinator: HassioDataUpdateCoordinator,
addon: Dict[str, Any],
attribute_name: str,
sensor_name: str,
) -> None:
"""Initialize base entity."""
self.addon_slug = addon[ATTR_SLUG]
self.addon_name = addon[ATTR_NAME]
self._data_key = "addons"
self.attribute_name = attribute_name
self.sensor_name = sensor_name
super().__init__(coordinator)
@property
def addon_info(self) -> Dict[str, Any]:
"""Return add-on info."""
return self.coordinator.data[self._data_key][self.addon_slug]
@property
def name(self) -> str:
"""Return entity name."""
return f"{self.addon_name}: {self.sensor_name}"
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return False
@property
def unique_id(self) -> str:
"""Return unique ID for entity."""
return f"{self.addon_slug}_{self.attribute_name}"
@property
def device_info(self) -> Dict[str, Any]:
"""Return device specific attributes."""
return {"identifiers": {(DOMAIN, self.addon_slug)}}
class HassioOSEntity(CoordinatorEntity):
"""Base Entity for Hass.io OS."""
def __init__(
self,
coordinator: HassioDataUpdateCoordinator,
attribute_name: str,
sensor_name: str,
) -> None:
"""Initialize base entity."""
self._data_key = "os"
self.attribute_name = attribute_name
self.sensor_name = sensor_name
super().__init__(coordinator)
@property
def os_info(self) -> Dict[str, Any]:
"""Return OS info."""
return self.coordinator.data[self._data_key]
@property
def name(self) -> str:
"""Return entity name."""
return f"Home Assistant Operating System: {self.sensor_name}"
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return False
@property
def unique_id(self) -> str:
"""Return unique ID for entity."""
return f"home_assistant_os_{self.attribute_name}"
@property
def device_info(self) -> Dict[str, Any]:
"""Return device specific attributes."""
return {"identifiers": {(DOMAIN, "OS")}}