Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix availability for GIOS index sensors #113021

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions homeassistant/components/gios/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ def native_value(self) -> StateType:
@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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool(sensor_data) will be False if the value is 0. Should you check against None instead ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sensor_data could be a Sensor object or None, it couldn't be 0 https://github.com/bieniu/gios/blob/d836f0c048bf2eb2847559467c5b05f264c42641/gios/model.py#L20


# 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An index can also be 0 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index is a non-empty string (good, very_good, moderate, etc.) or None https://github.com/bieniu/gios/blob/d836f0c048bf2eb2847559467c5b05f264c42641/gios/model.py#L12


return available and bool(sensor_data)
return available
66 changes: 35 additions & 31 deletions tests/components/gios/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test sensor of GIOS integration."""

from copy import deepcopy
from datetime import timedelta
import json
from unittest.mock import patch
Expand Down Expand Up @@ -277,44 +278,47 @@ 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={},
):
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(
Expand All @@ -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:
Expand Down