From f15840e7ff1ed94c488ce9c9dddb93e409536ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Tue, 26 Oct 2021 19:58:17 +0200 Subject: [PATCH] Limit add-on stats to add-ons that are running (#58479) --- homeassistant/components/hassio/__init__.py | 9 +++++--- .../components/hassio/binary_sensor.py | 10 +++++++-- homeassistant/components/hassio/const.py | 1 + homeassistant/components/hassio/entity.py | 19 +++++++++++++++- tests/components/hassio/test_init.py | 5 +++++ tests/components/hassio/test_sensor.py | 22 ++++--------------- 6 files changed, 42 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index 0f8ff5441f3..6ad1c67f1f3 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -46,6 +46,8 @@ from .const import ( ATTR_PASSWORD, ATTR_REPOSITORY, ATTR_SLUG, + ATTR_STARTED, + ATTR_STATE, ATTR_URL, ATTR_VERSION, DATA_KEY_ADDONS, @@ -539,12 +541,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa: hassio.get_os_info(), ) - addon_slugs = [ - addon[ATTR_SLUG] + addons = [ + addon for addon in hass.data[DATA_SUPERVISOR_INFO].get("addons", []) + if addon[ATTR_STATE] == ATTR_STARTED ] stats_data = await asyncio.gather( - *[update_addon_stats(slug) for slug in addon_slugs] + *[update_addon_stats(addon[ATTR_SLUG]) for addon in addons] ) hass.data[DATA_ADDONS_STATS] = dict(stats_data) diff --git a/homeassistant/components/hassio/binary_sensor.py b/homeassistant/components/hassio/binary_sensor.py index 8953bd47942..5078e11c26e 100644 --- a/homeassistant/components/hassio/binary_sensor.py +++ b/homeassistant/components/hassio/binary_sensor.py @@ -14,7 +14,13 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import ADDONS_COORDINATOR -from .const import ATTR_STATE, ATTR_UPDATE_AVAILABLE, DATA_KEY_ADDONS, DATA_KEY_OS +from .const import ( + ATTR_STARTED, + ATTR_STATE, + ATTR_UPDATE_AVAILABLE, + DATA_KEY_ADDONS, + DATA_KEY_OS, +) from .entity import HassioAddonEntity, HassioOSEntity @@ -37,7 +43,7 @@ ENTITY_DESCRIPTIONS = ( entity_registry_enabled_default=False, key=ATTR_STATE, name="Running", - target="started", + target=ATTR_STARTED, ), ) diff --git a/homeassistant/components/hassio/const.py b/homeassistant/components/hassio/const.py index 540d00b4906..7cdc87708ae 100644 --- a/homeassistant/components/hassio/const.py +++ b/homeassistant/components/hassio/const.py @@ -46,6 +46,7 @@ ATTR_CPU_PERCENT = "cpu_percent" ATTR_MEMORY_PERCENT = "memory_percent" ATTR_SLUG = "slug" ATTR_STATE = "state" +ATTR_STARTED = "started" ATTR_URL = "url" ATTR_REPOSITORY = "repository" diff --git a/homeassistant/components/hassio/entity.py b/homeassistant/components/hassio/entity.py index 1fd2926fe56..5dd41166c32 100644 --- a/homeassistant/components/hassio/entity.py +++ b/homeassistant/components/hassio/entity.py @@ -8,7 +8,7 @@ from homeassistant.helpers.entity import DeviceInfo, EntityDescription from homeassistant.helpers.update_coordinator import CoordinatorEntity from . import DOMAIN, HassioDataUpdateCoordinator -from .const import ATTR_SLUG +from .const import ATTR_SLUG, DATA_KEY_ADDONS, DATA_KEY_OS class HassioAddonEntity(CoordinatorEntity): @@ -28,6 +28,15 @@ class HassioAddonEntity(CoordinatorEntity): self._attr_unique_id = f"{addon[ATTR_SLUG]}_{entity_description.key}" self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, addon[ATTR_SLUG])}) + @property + def available(self) -> bool: + """Return True if entity is available.""" + return ( + super().available + and self.entity_description.key + in self.coordinator.data[DATA_KEY_ADDONS][self._addon_slug] + ) + class HassioOSEntity(CoordinatorEntity): """Base Entity for Hass.io OS.""" @@ -43,3 +52,11 @@ class HassioOSEntity(CoordinatorEntity): self._attr_name = f"Home Assistant Operating System: {entity_description.name}" self._attr_unique_id = f"home_assistant_os_{entity_description.key}" self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, "OS")}) + + @property + def available(self) -> bool: + """Return True if entity is available.""" + return ( + super().available + and self.entity_description.key in self.coordinator.data[DATA_KEY_OS] + ) diff --git a/tests/components/hassio/test_init.py b/tests/components/hassio/test_init.py index 62d2cb67d0d..f5214b563b3 100644 --- a/tests/components/hassio/test_init.py +++ b/tests/components/hassio/test_init.py @@ -493,6 +493,7 @@ async def test_device_registry_calls(hass): "addons": [ { "name": "test", + "state": "started", "slug": "test", "installed": True, "update_available": False, @@ -503,6 +504,7 @@ async def test_device_registry_calls(hass): }, { "name": "test2", + "state": "started", "slug": "test2", "installed": True, "update_available": False, @@ -537,6 +539,7 @@ async def test_device_registry_calls(hass): "addons": [ { "name": "test2", + "state": "started", "slug": "test2", "installed": True, "update_available": False, @@ -568,6 +571,7 @@ async def test_device_registry_calls(hass): { "name": "test2", "slug": "test2", + "state": "started", "installed": True, "update_available": False, "version": "1.0.0", @@ -577,6 +581,7 @@ async def test_device_registry_calls(hass): { "name": "test3", "slug": "test3", + "state": "stopped", "installed": True, "update_available": False, "version": "1.0.0", diff --git a/tests/components/hassio/test_sensor.py b/tests/components/hassio/test_sensor.py index 9039d302640..00d2c32c520 100644 --- a/tests/components/hassio/test_sensor.py +++ b/tests/components/hassio/test_sensor.py @@ -66,6 +66,7 @@ def mock_all(aioclient_mock, request): "addons": [ { "name": "test", + "state": "started", "slug": "test", "installed": True, "update_available": False, @@ -76,6 +77,7 @@ def mock_all(aioclient_mock, request): }, { "name": "test2", + "state": "stopped", "slug": "test2", "installed": True, "update_available": False, @@ -104,22 +106,6 @@ def mock_all(aioclient_mock, request): }, }, ) - aioclient_mock.get( - "http://127.0.0.1/addons/test2/stats", - json={ - "result": "ok", - "data": { - "cpu_percent": 0.8, - "memory_usage": 51941376, - "memory_limit": 3977146368, - "memory_percent": 1.31, - "network_rx": 31338284, - "network_tx": 15692900, - "blk_read": 740077568, - "blk_write": 6004736, - }, - }, - ) aioclient_mock.get( "http://127.0.0.1/ingress/panels", json={"result": "ok", "data": {"panels": {}}} ) @@ -149,9 +135,9 @@ async def test_sensors(hass, aioclient_mock): "sensor.test2_version": "3.1.0", "sensor.test2_newest_version": "3.2.0", "sensor.test_cpu_percent": "0.99", - "sensor.test2_cpu_percent": "0.8", + "sensor.test2_cpu_percent": "unavailable", "sensor.test_memory_percent": "4.59", - "sensor.test2_memory_percent": "1.31", + "sensor.test2_memory_percent": "unavailable", } """Check that entities are disabled by default."""