Use entity descriptions for hassio entities (#54749)
parent
f1f05cdf1b
commit
a2c9cfbf41
|
@ -4,15 +4,25 @@ from __future__ import annotations
|
|||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASS_UPDATE,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import ADDONS_COORDINATOR
|
||||
from .const import ATTR_UPDATE_AVAILABLE
|
||||
from .const import ATTR_UPDATE_AVAILABLE, DATA_KEY_ADDONS, DATA_KEY_OS
|
||||
from .entity import HassioAddonEntity, HassioOSEntity
|
||||
|
||||
ENTITY_DESCRIPTIONS = (
|
||||
BinarySensorEntityDescription(
|
||||
device_class=DEVICE_CLASS_UPDATE,
|
||||
entity_registry_enabled_default=False,
|
||||
key=ATTR_UPDATE_AVAILABLE,
|
||||
name="Update Available",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
|
@ -22,36 +32,44 @@ async def async_setup_entry(
|
|||
"""Binary sensor set up for Hass.io config entry."""
|
||||
coordinator = hass.data[ADDONS_COORDINATOR]
|
||||
|
||||
entities = [
|
||||
HassioAddonBinarySensor(
|
||||
coordinator, addon, ATTR_UPDATE_AVAILABLE, "Update Available"
|
||||
)
|
||||
for addon in coordinator.data["addons"].values()
|
||||
]
|
||||
if coordinator.is_hass_os:
|
||||
entities.append(
|
||||
HassioOSBinarySensor(coordinator, ATTR_UPDATE_AVAILABLE, "Update Available")
|
||||
)
|
||||
entities = []
|
||||
|
||||
for entity_description in ENTITY_DESCRIPTIONS:
|
||||
for addon in coordinator.data[DATA_KEY_ADDONS].values():
|
||||
entities.append(
|
||||
HassioAddonBinarySensor(
|
||||
addon=addon,
|
||||
coordinator=coordinator,
|
||||
entity_description=entity_description,
|
||||
)
|
||||
)
|
||||
|
||||
if coordinator.is_hass_os:
|
||||
entities.append(
|
||||
HassioOSBinarySensor(
|
||||
coordinator=coordinator,
|
||||
entity_description=entity_description,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class HassioAddonBinarySensor(HassioAddonEntity, BinarySensorEntity):
|
||||
"""Binary sensor to track whether an update is available for a Hass.io add-on."""
|
||||
|
||||
_attr_device_class = DEVICE_CLASS_UPDATE
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self.addon_info[self.attribute_name]
|
||||
return self.coordinator.data[DATA_KEY_ADDONS][self._addon_slug][
|
||||
self.entity_description.key
|
||||
]
|
||||
|
||||
|
||||
class HassioOSBinarySensor(HassioOSEntity, BinarySensorEntity):
|
||||
"""Binary sensor to track whether an update is available for Hass.io OS."""
|
||||
|
||||
_attr_device_class = DEVICE_CLASS_UPDATE
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self.os_info[self.attribute_name]
|
||||
return self.coordinator.data[DATA_KEY_OS][self.entity_description.key]
|
||||
|
|
|
@ -40,7 +40,6 @@ WS_TYPE_SUBSCRIBE = "supervisor/subscribe"
|
|||
|
||||
EVENT_SUPERVISOR_EVENT = "supervisor_event"
|
||||
|
||||
# Add-on keys
|
||||
ATTR_VERSION = "version"
|
||||
ATTR_VERSION_LATEST = "version_latest"
|
||||
ATTR_UPDATE_AVAILABLE = "update_available"
|
||||
|
@ -49,6 +48,10 @@ ATTR_URL = "url"
|
|||
ATTR_REPOSITORY = "repository"
|
||||
|
||||
|
||||
DATA_KEY_ADDONS = "addons"
|
||||
DATA_KEY_OS = "os"
|
||||
|
||||
|
||||
class SupervisorEntityModel(str, Enum):
|
||||
"""Supervisor entity model."""
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||
from typing import Any
|
||||
|
||||
from homeassistant.const import ATTR_NAME
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from . import DOMAIN, HassioDataUpdateCoordinator
|
||||
|
@ -17,42 +17,16 @@ class HassioAddonEntity(CoordinatorEntity):
|
|||
def __init__(
|
||||
self,
|
||||
coordinator: HassioDataUpdateCoordinator,
|
||||
entity_description: EntityDescription,
|
||||
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) -> DeviceInfo:
|
||||
"""Return device specific attributes."""
|
||||
return {"identifiers": {(DOMAIN, self.addon_slug)}}
|
||||
self.entity_description = entity_description
|
||||
self._addon_slug = addon[ATTR_SLUG]
|
||||
self._attr_name = f"{addon[ATTR_NAME]}: {entity_description.name}"
|
||||
self._attr_unique_id = f"{addon[ATTR_SLUG]}_{entity_description.key}"
|
||||
self._attr_device_info = {"identifiers": {(DOMAIN, addon[ATTR_SLUG])}}
|
||||
|
||||
|
||||
class HassioOSEntity(CoordinatorEntity):
|
||||
|
@ -61,36 +35,11 @@ class HassioOSEntity(CoordinatorEntity):
|
|||
def __init__(
|
||||
self,
|
||||
coordinator: HassioDataUpdateCoordinator,
|
||||
attribute_name: str,
|
||||
sensor_name: str,
|
||||
entity_description: EntityDescription,
|
||||
) -> 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) -> DeviceInfo:
|
||||
"""Return device specific attributes."""
|
||||
return {"identifiers": {(DOMAIN, "OS")}}
|
||||
self.entity_description = entity_description
|
||||
self._attr_name = f"Home Assistant Operating System: {entity_description.name}"
|
||||
self._attr_unique_id = f"home_assistant_os_{entity_description.key}"
|
||||
self._attr_device_info = {"identifiers": {(DOMAIN, "OS")}}
|
||||
|
|
|
@ -1,15 +1,28 @@
|
|||
"""Sensor platform for Hass.io addons."""
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import ADDONS_COORDINATOR
|
||||
from .const import ATTR_VERSION, ATTR_VERSION_LATEST
|
||||
from .const import ATTR_VERSION, ATTR_VERSION_LATEST, DATA_KEY_ADDONS, DATA_KEY_OS
|
||||
from .entity import HassioAddonEntity, HassioOSEntity
|
||||
|
||||
ENTITY_DESCRIPTIONS = (
|
||||
SensorEntityDescription(
|
||||
entity_registry_enabled_default=False,
|
||||
key=ATTR_VERSION,
|
||||
name="Version",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
entity_registry_enabled_default=False,
|
||||
key=ATTR_VERSION_LATEST,
|
||||
name="Newest Version",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
|
@ -21,16 +34,23 @@ async def async_setup_entry(
|
|||
|
||||
entities = []
|
||||
|
||||
for attribute_name, sensor_name in (
|
||||
(ATTR_VERSION, "Version"),
|
||||
(ATTR_VERSION_LATEST, "Newest Version"),
|
||||
):
|
||||
for addon in coordinator.data["addons"].values():
|
||||
for entity_description in ENTITY_DESCRIPTIONS:
|
||||
for addon in coordinator.data[DATA_KEY_ADDONS].values():
|
||||
entities.append(
|
||||
HassioAddonSensor(coordinator, addon, attribute_name, sensor_name)
|
||||
HassioAddonSensor(
|
||||
addon=addon,
|
||||
coordinator=coordinator,
|
||||
entity_description=entity_description,
|
||||
)
|
||||
)
|
||||
|
||||
if coordinator.is_hass_os:
|
||||
entities.append(HassioOSSensor(coordinator, attribute_name, sensor_name))
|
||||
entities.append(
|
||||
HassioOSSensor(
|
||||
coordinator=coordinator,
|
||||
entity_description=entity_description,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
@ -40,8 +60,10 @@ class HassioAddonSensor(HassioAddonEntity, SensorEntity):
|
|||
|
||||
@property
|
||||
def native_value(self) -> str:
|
||||
"""Return state of entity."""
|
||||
return self.addon_info[self.attribute_name]
|
||||
"""Return native value of entity."""
|
||||
return self.coordinator.data[DATA_KEY_ADDONS][self._addon_slug][
|
||||
self.entity_description.key
|
||||
]
|
||||
|
||||
|
||||
class HassioOSSensor(HassioOSEntity, SensorEntity):
|
||||
|
@ -49,5 +71,5 @@ class HassioOSSensor(HassioOSEntity, SensorEntity):
|
|||
|
||||
@property
|
||||
def native_value(self) -> str:
|
||||
"""Return state of entity."""
|
||||
return self.os_info[self.attribute_name]
|
||||
"""Return native value of entity."""
|
||||
return self.coordinator.data[DATA_KEY_OS][self.entity_description.key]
|
||||
|
|
Loading…
Reference in New Issue