From 9e4be56939dc4c83b749ccbf6fb5cfd16f6ef709 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Sun, 22 Jan 2023 22:18:47 +0200 Subject: [PATCH] Shelly - handle None in RPC power sensors (#86399) Handle None in RPC power sensors --- homeassistant/components/shelly/sensor.py | 4 ++-- tests/components/shelly/test_sensor.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 6d41a42ad88..1f91cf4844b 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -318,7 +318,7 @@ RPC_SENSORS: Final = { sub_key="apower", name="Power", native_unit_of_measurement=UnitOfPower.WATT, - value=lambda status, _: round(float(status), 1), + value=lambda status, _: None if status is None else round(float(status), 1), device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), @@ -327,7 +327,7 @@ RPC_SENSORS: Final = { sub_key="voltage", name="Voltage", native_unit_of_measurement=UnitOfElectricPotential.VOLT, - value=lambda status, _: round(float(status), 1), + value=lambda status, _: None if status is None else round(float(status), 1), device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 6dcd903219f..d62682df5a9 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -180,6 +180,11 @@ async def test_rpc_sensor(hass, mock_rpc_device, monkeypatch) -> None: assert hass.states.get(entity_id).state == "88.2" + mutate_rpc_device_status(monkeypatch, mock_rpc_device, "cover:0", "apower", None) + mock_rpc_device.mock_update() + + assert hass.states.get(entity_id).state == STATE_UNKNOWN + async def test_rpc_sensor_error(hass, mock_rpc_device, monkeypatch): """Test RPC sensor unavailable on sensor error."""