diff --git a/homeassistant/components/deconz/sensor.py b/homeassistant/components/deconz/sensor.py index a4dcaed8f18..d3a20fad522 100644 --- a/homeassistant/components/deconz/sensor.py +++ b/homeassistant/components/deconz/sensor.py @@ -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, diff --git a/tests/components/deconz/test_sensor.py b/tests/components/deconz/test_sensor.py index bd51bab44e4..590ccee25d5 100644 --- a/tests/components/deconz/test_sensor.py +++ b/tests/components/deconz/test_sensor.py @@ -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 = {