Add binary sensor to add-ons to show if they are running (#58120)
parent
b71773fd1d
commit
8b223be073
|
@ -1,7 +1,10 @@
|
|||
"""Binary sensor platform for Hass.io addons."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASS_RUNNING,
|
||||
DEVICE_CLASS_UPDATE,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
|
@ -11,16 +14,31 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import ADDONS_COORDINATOR
|
||||
from .const import ATTR_UPDATE_AVAILABLE, DATA_KEY_ADDONS, DATA_KEY_OS
|
||||
from .const import ATTR_STATE, ATTR_UPDATE_AVAILABLE, DATA_KEY_ADDONS, DATA_KEY_OS
|
||||
from .entity import HassioAddonEntity, HassioOSEntity
|
||||
|
||||
|
||||
@dataclass
|
||||
class HassioBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||
"""Hassio binary sensor entity description."""
|
||||
|
||||
target: str | None = None
|
||||
|
||||
|
||||
ENTITY_DESCRIPTIONS = (
|
||||
BinarySensorEntityDescription(
|
||||
HassioBinarySensorEntityDescription(
|
||||
device_class=DEVICE_CLASS_UPDATE,
|
||||
entity_registry_enabled_default=False,
|
||||
key=ATTR_UPDATE_AVAILABLE,
|
||||
name="Update Available",
|
||||
),
|
||||
HassioBinarySensorEntityDescription(
|
||||
device_class=DEVICE_CLASS_RUNNING,
|
||||
entity_registry_enabled_default=False,
|
||||
key=ATTR_STATE,
|
||||
name="Running",
|
||||
target="started",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
@ -56,14 +74,19 @@ async def async_setup_entry(
|
|||
|
||||
|
||||
class HassioAddonBinarySensor(HassioAddonEntity, BinarySensorEntity):
|
||||
"""Binary sensor to track whether an update is available for a Hass.io add-on."""
|
||||
"""Binary sensor for Hass.io add-ons."""
|
||||
|
||||
entity_description: HassioBinarySensorEntityDescription
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self.coordinator.data[DATA_KEY_ADDONS][self._addon_slug][
|
||||
value = self.coordinator.data[DATA_KEY_ADDONS][self._addon_slug][
|
||||
self.entity_description.key
|
||||
]
|
||||
if self.entity_description.target is None:
|
||||
return value
|
||||
return value == self.entity_description.target
|
||||
|
||||
|
||||
class HassioOSBinarySensor(HassioOSEntity, BinarySensorEntity):
|
||||
|
|
|
@ -45,6 +45,7 @@ ATTR_UPDATE_AVAILABLE = "update_available"
|
|||
ATTR_CPU_PERCENT = "cpu_percent"
|
||||
ATTR_MEMORY_PERCENT = "memory_percent"
|
||||
ATTR_SLUG = "slug"
|
||||
ATTR_STATE = "state"
|
||||
ATTR_URL = "url"
|
||||
ATTR_REPOSITORY = "repository"
|
||||
|
||||
|
|
Loading…
Reference in New Issue