Add binary sensor to add-ons to show if they are running (#58120)

pull/58445/head^2
Joakim Sørensen 2021-10-26 00:21:44 +02:00 committed by GitHub
parent b71773fd1d
commit 8b223be073
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -1,7 +1,10 @@
"""Binary sensor platform for Hass.io addons.""" """Binary sensor platform for Hass.io addons."""
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_RUNNING,
DEVICE_CLASS_UPDATE, DEVICE_CLASS_UPDATE,
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
@ -11,16 +14,31 @@ 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, 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 from .entity import HassioAddonEntity, HassioOSEntity
@dataclass
class HassioBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Hassio binary sensor entity description."""
target: str | None = None
ENTITY_DESCRIPTIONS = ( ENTITY_DESCRIPTIONS = (
BinarySensorEntityDescription( HassioBinarySensorEntityDescription(
device_class=DEVICE_CLASS_UPDATE, device_class=DEVICE_CLASS_UPDATE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
key=ATTR_UPDATE_AVAILABLE, key=ATTR_UPDATE_AVAILABLE,
name="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): 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 @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.coordinator.data[DATA_KEY_ADDONS][self._addon_slug][ value = self.coordinator.data[DATA_KEY_ADDONS][self._addon_slug][
self.entity_description.key self.entity_description.key
] ]
if self.entity_description.target is None:
return value
return value == self.entity_description.target
class HassioOSBinarySensor(HassioOSEntity, BinarySensorEntity): class HassioOSBinarySensor(HassioOSEntity, BinarySensorEntity):

View File

@ -45,6 +45,7 @@ ATTR_UPDATE_AVAILABLE = "update_available"
ATTR_CPU_PERCENT = "cpu_percent" ATTR_CPU_PERCENT = "cpu_percent"
ATTR_MEMORY_PERCENT = "memory_percent" ATTR_MEMORY_PERCENT = "memory_percent"
ATTR_SLUG = "slug" ATTR_SLUG = "slug"
ATTR_STATE = "state"
ATTR_URL = "url" ATTR_URL = "url"
ATTR_REPOSITORY = "repository" ATTR_REPOSITORY = "repository"