From f01095fb66b745f5016a77cc1ca6691dce72ab5c Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Tue, 12 Mar 2024 17:41:16 +0100 Subject: [PATCH] Fix availability for GIOS index sensors (#113021) * Fix availability for index sensors * Improve test_availability() --------- Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com> --- homeassistant/components/gios/sensor.py | 6 +-- tests/components/gios/test_sensor.py | 66 +++++++++++++------------ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/gios/sensor.py b/homeassistant/components/gios/sensor.py index f097a14c943..c2da9239453 100644 --- a/homeassistant/components/gios/sensor.py +++ b/homeassistant/components/gios/sensor.py @@ -230,11 +230,11 @@ class GiosSensor(CoordinatorEntity[GiosDataUpdateCoordinator], SensorEntity): @property def available(self) -> bool: """Return if entity is available.""" - available = super().available sensor_data = getattr(self.coordinator.data, self.entity_description.key) + available = super().available and bool(sensor_data) # Sometimes the API returns sensor data without indexes - if self.entity_description.subkey: + if self.entity_description.subkey and available: return available and bool(sensor_data.index) - return available and bool(sensor_data) + return available diff --git a/tests/components/gios/test_sensor.py b/tests/components/gios/test_sensor.py index 4f35587d4fe..6714882ad3f 100644 --- a/tests/components/gios/test_sensor.py +++ b/tests/components/gios/test_sensor.py @@ -1,5 +1,6 @@ """Test sensor of GIOS integration.""" +from copy import deepcopy from datetime import timedelta import json from unittest.mock import patch @@ -277,22 +278,24 @@ async def test_availability(hass: HomeAssistant) -> None: async_fire_time_changed(hass, future) await hass.async_block_till_done() - state = hass.states.get("sensor.home_pm2_5") - assert state - assert state.state == STATE_UNAVAILABLE + state = hass.states.get("sensor.home_pm2_5") + assert state + assert state.state == STATE_UNAVAILABLE - state = hass.states.get("sensor.home_pm2_5_index") - assert state - assert state.state == STATE_UNAVAILABLE + state = hass.states.get("sensor.home_pm2_5_index") + assert state + assert state.state == STATE_UNAVAILABLE - state = hass.states.get("sensor.home_air_quality_index") - assert state - assert state.state == STATE_UNAVAILABLE + state = hass.states.get("sensor.home_air_quality_index") + assert state + assert state.state == STATE_UNAVAILABLE + incomplete_sensors = deepcopy(sensors) + incomplete_sensors["pm2.5"] = {} future = utcnow() + timedelta(minutes=120) with patch( "homeassistant.components.gios.Gios._get_all_sensors", - return_value=sensors, + return_value=incomplete_sensors, ), patch( "homeassistant.components.gios.Gios._get_indexes", return_value={}, @@ -300,21 +303,22 @@ async def test_availability(hass: HomeAssistant) -> None: async_fire_time_changed(hass, future) await hass.async_block_till_done() - state = hass.states.get("sensor.home_pm2_5") - assert state - assert state.state == "4" + # There is no PM2.5 data so the state should be unavailable + state = hass.states.get("sensor.home_pm2_5") + assert state + assert state.state == STATE_UNAVAILABLE - # Indexes are empty so the state should be unavailable - state = hass.states.get("sensor.home_air_quality_index") - assert state - assert state.state == STATE_UNAVAILABLE + # Indexes are empty so the state should be unavailable + state = hass.states.get("sensor.home_air_quality_index") + assert state + assert state.state == STATE_UNAVAILABLE - # Indexes are empty so the state should be unavailable - state = hass.states.get("sensor.home_pm2_5_index") - assert state - assert state.state == STATE_UNAVAILABLE + # Indexes are empty so the state should be unavailable + state = hass.states.get("sensor.home_pm2_5_index") + assert state + assert state.state == STATE_UNAVAILABLE - future = utcnow() + timedelta(minutes=180) + future = utcnow() + timedelta(minutes=180) with patch( "homeassistant.components.gios.Gios._get_all_sensors", return_value=sensors ), patch( @@ -324,17 +328,17 @@ async def test_availability(hass: HomeAssistant) -> None: async_fire_time_changed(hass, future) await hass.async_block_till_done() - state = hass.states.get("sensor.home_pm2_5") - assert state - assert state.state == "4" + state = hass.states.get("sensor.home_pm2_5") + assert state + assert state.state == "4" - state = hass.states.get("sensor.home_pm2_5_index") - assert state - assert state.state == "good" + state = hass.states.get("sensor.home_pm2_5_index") + assert state + assert state.state == "good" - state = hass.states.get("sensor.home_air_quality_index") - assert state - assert state.state == "good" + state = hass.states.get("sensor.home_air_quality_index") + assert state + assert state.state == "good" async def test_invalid_indexes(hass: HomeAssistant) -> None: