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 (
|
||||
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]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue