Handle unknown/unavailable state for mobile_app (#60974)
parent
95f0098593
commit
a4ffa63165
|
@ -1,6 +1,12 @@
|
||||||
"""A entity class for mobile_app."""
|
"""A entity class for mobile_app."""
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import callback
|
||||||
from homeassistant.helpers.device_registry import DeviceEntry
|
from homeassistant.helpers.device_registry import DeviceEntry
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
@ -101,6 +107,11 @@ class MobileAppEntity(RestoreEntity):
|
||||||
"""Return device registry information for this entity."""
|
"""Return device registry information for this entity."""
|
||||||
return device_info(self._registration)
|
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
|
@callback
|
||||||
def _handle_update(self, data):
|
def _handle_update(self, data):
|
||||||
"""Handle async event updates."""
|
"""Handle async event updates."""
|
||||||
|
|
|
@ -8,6 +8,7 @@ from homeassistant.const import (
|
||||||
CONF_WEBHOOK_ID,
|
CONF_WEBHOOK_ID,
|
||||||
DEVICE_CLASS_DATE,
|
DEVICE_CLASS_DATE,
|
||||||
DEVICE_CLASS_TIMESTAMP,
|
DEVICE_CLASS_TIMESTAMP,
|
||||||
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
@ -88,9 +89,11 @@ class MobileAppSensor(MobileAppEntity, SensorEntity):
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
|
if (state := self._config[ATTR_SENSOR_STATE]) in (None, STATE_UNKNOWN):
|
||||||
|
return None
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(state := self._config[ATTR_SENSOR_STATE]) is not None
|
self.device_class
|
||||||
and self.device_class
|
|
||||||
in (
|
in (
|
||||||
DEVICE_CLASS_DATE,
|
DEVICE_CLASS_DATE,
|
||||||
DEVICE_CLASS_TIMESTAMP,
|
DEVICE_CLASS_TIMESTAMP,
|
||||||
|
|
|
@ -4,7 +4,7 @@ from http import HTTPStatus
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.sensor import DEVICE_CLASS_DATE, DEVICE_CLASS_TIMESTAMP
|
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
|
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.config_entries.async_unload(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
unloaded_entity = hass.states.get("sensor.test_1_battery_state")
|
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.config_entries.async_setup(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
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-18 20:25:00+01:00",
|
||||||
"2021-11-18T19:25:00+00: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(
|
async def test_sensor_datetime(
|
||||||
|
|
Loading…
Reference in New Issue