Make sure sensor state value is not None prior to trying to used the scaled value (#71189)

This commit is contained in:
Robert Svensson 2022-05-02 09:51:19 +02:00 committed by GitHub
parent 5db014666c
commit 37b59dfcc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View file

@ -112,7 +112,7 @@ ENTITY_DESCRIPTIONS = {
DeconzSensorDescription(
key="consumption",
value_fn=lambda device: device.scaled_consumption
if isinstance(device, Consumption)
if isinstance(device, Consumption) and isinstance(device.consumption, int)
else None,
update_key="consumption",
device_class=SensorDeviceClass.ENERGY,
@ -144,7 +144,7 @@ ENTITY_DESCRIPTIONS = {
DeconzSensorDescription(
key="humidity",
value_fn=lambda device: device.scaled_humidity
if isinstance(device, Humidity)
if isinstance(device, Humidity) and isinstance(device.humidity, int)
else None,
update_key="humidity",
device_class=SensorDeviceClass.HUMIDITY,
@ -156,7 +156,7 @@ ENTITY_DESCRIPTIONS = {
DeconzSensorDescription(
key="light_level",
value_fn=lambda device: device.scaled_light_level
if isinstance(device, LightLevel)
if isinstance(device, LightLevel) and isinstance(device.light_level, int)
else None,
update_key="lightlevel",
device_class=SensorDeviceClass.ILLUMINANCE,
@ -189,7 +189,7 @@ ENTITY_DESCRIPTIONS = {
DeconzSensorDescription(
key="temperature",
value_fn=lambda device: device.scaled_temperature
if isinstance(device, Temperature)
if isinstance(device, Temperature) and isinstance(device.temperature, int)
else None,
update_key="temperature",
device_class=SensorDeviceClass.TEMPERATURE,

View file

@ -785,6 +785,36 @@ async def test_add_new_sensor(hass, aioclient_mock, mock_deconz_websocket):
assert hass.states.get("sensor.light_level_sensor").state == "999.8"
BAD_SENSOR_DATA = [
("ZHAConsumption", "consumption"),
("ZHAHumidity", "humidity"),
("ZHALightLevel", "lightlevel"),
("ZHATemperature", "temperature"),
]
@pytest.mark.parametrize("sensor_type, sensor_property", BAD_SENSOR_DATA)
async def test_dont_add_sensor_if_state_is_none(
hass, aioclient_mock, sensor_type, sensor_property
):
"""Test sensor with scaled data is not created if state is None."""
data = {
"sensors": {
"1": {
"name": "Sensor 1",
"type": sensor_type,
"state": {sensor_property: None},
"config": {},
"uniqueid": "00:00:00:00:00:00:00:00-00",
}
}
}
with patch.dict(DECONZ_WEB_REQUEST, data):
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
async def test_add_battery_later(hass, aioclient_mock, mock_deconz_websocket):
"""Test that a sensor without an initial battery state creates a battery sensor once state exist."""
data = {