From a4ffa631653cc6cc32e825ea6f8de75db0a1b89b Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sun, 5 Dec 2021 18:51:57 +0100 Subject: [PATCH] Handle unknown/unavailable state for mobile_app (#60974) --- homeassistant/components/mobile_app/entity.py | 13 ++++++++++++- homeassistant/components/mobile_app/sensor.py | 7 +++++-- tests/components/mobile_app/test_sensor.py | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/mobile_app/entity.py b/homeassistant/components/mobile_app/entity.py index 03b6d95c2a2..0c26533b7cb 100644 --- a/homeassistant/components/mobile_app/entity.py +++ b/homeassistant/components/mobile_app/entity.py @@ -1,6 +1,12 @@ """A entity class for mobile_app.""" from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_ICON, CONF_NAME, CONF_UNIQUE_ID, CONF_WEBHOOK_ID +from homeassistant.const import ( + ATTR_ICON, + CONF_NAME, + CONF_UNIQUE_ID, + CONF_WEBHOOK_ID, + STATE_UNAVAILABLE, +) from homeassistant.core import callback from homeassistant.helpers.device_registry import DeviceEntry from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -101,6 +107,11 @@ class MobileAppEntity(RestoreEntity): """Return device registry information for this entity.""" return device_info(self._registration) + @property + def available(self) -> bool: + """Return True if entity is available.""" + return self._config.get(ATTR_SENSOR_STATE) != STATE_UNAVAILABLE + @callback def _handle_update(self, data): """Handle async event updates.""" diff --git a/homeassistant/components/mobile_app/sensor.py b/homeassistant/components/mobile_app/sensor.py index b58beef96ba..0631f8f72aa 100644 --- a/homeassistant/components/mobile_app/sensor.py +++ b/homeassistant/components/mobile_app/sensor.py @@ -8,6 +8,7 @@ from homeassistant.const import ( CONF_WEBHOOK_ID, DEVICE_CLASS_DATE, DEVICE_CLASS_TIMESTAMP, + STATE_UNKNOWN, ) from homeassistant.core import callback from homeassistant.helpers import entity_registry as er @@ -88,9 +89,11 @@ class MobileAppSensor(MobileAppEntity, SensorEntity): @property def native_value(self): """Return the state of the sensor.""" + if (state := self._config[ATTR_SENSOR_STATE]) in (None, STATE_UNKNOWN): + return None + if ( - (state := self._config[ATTR_SENSOR_STATE]) is not None - and self.device_class + self.device_class in ( DEVICE_CLASS_DATE, DEVICE_CLASS_TIMESTAMP, diff --git a/tests/components/mobile_app/test_sensor.py b/tests/components/mobile_app/test_sensor.py index cfd9efa34c2..295e37ee7d9 100644 --- a/tests/components/mobile_app/test_sensor.py +++ b/tests/components/mobile_app/test_sensor.py @@ -4,7 +4,7 @@ from http import HTTPStatus import pytest from homeassistant.components.sensor import DEVICE_CLASS_DATE, DEVICE_CLASS_TIMESTAMP -from homeassistant.const import PERCENTAGE, STATE_UNKNOWN +from homeassistant.const import PERCENTAGE, STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.helpers import device_registry as dr, entity_registry as er @@ -89,7 +89,7 @@ async def test_sensor(hass, create_registrations, webhook_client): await hass.config_entries.async_unload(config_entry.entry_id) await hass.async_block_till_done() unloaded_entity = hass.states.get("sensor.test_1_battery_state") - assert unloaded_entity.state == "unavailable" + assert unloaded_entity.state == STATE_UNAVAILABLE await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() @@ -295,6 +295,16 @@ async def test_update_sensor_no_state(hass, create_registrations, webhook_client "2021-11-18 20:25:00+01:00", "2021-11-18T19:25:00+00:00", ), + ( + DEVICE_CLASS_TIMESTAMP, + "unavailable", + STATE_UNAVAILABLE, + ), + ( + DEVICE_CLASS_TIMESTAMP, + "unknown", + STATE_UNKNOWN, + ), ], ) async def test_sensor_datetime(