From 8b223be0733dbe31aa66514cd4f5d82152cbf3aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Tue, 26 Oct 2021 00:21:44 +0200 Subject: [PATCH] Add binary sensor to add-ons to show if they are running (#58120) --- .../components/hassio/binary_sensor.py | 31 ++++++++++++++++--- homeassistant/components/hassio/const.py | 1 + 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/hassio/binary_sensor.py b/homeassistant/components/hassio/binary_sensor.py index dfd13adbde6..8953bd47942 100644 --- a/homeassistant/components/hassio/binary_sensor.py +++ b/homeassistant/components/hassio/binary_sensor.py @@ -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): diff --git a/homeassistant/components/hassio/const.py b/homeassistant/components/hassio/const.py index 1b24013163f..540d00b4906 100644 --- a/homeassistant/components/hassio/const.py +++ b/homeassistant/components/hassio/const.py @@ -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"