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