Skip to content

Commit 9e4be56

Browse files
authored
Shelly - handle None in RPC power sensors (home-assistant#86399)
Handle None in RPC power sensors
1 parent 32c1a01 commit 9e4be56

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

homeassistant/components/shelly/sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class RestSensorDescription(RestEntityDescription, SensorEntityDescription):
318318
sub_key="apower",
319319
name="Power",
320320
native_unit_of_measurement=UnitOfPower.WATT,
321-
value=lambda status, _: round(float(status), 1),
321+
value=lambda status, _: None if status is None else round(float(status), 1),
322322
device_class=SensorDeviceClass.POWER,
323323
state_class=SensorStateClass.MEASUREMENT,
324324
),
@@ -327,7 +327,7 @@ class RestSensorDescription(RestEntityDescription, SensorEntityDescription):
327327
sub_key="voltage",
328328
name="Voltage",
329329
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
330-
value=lambda status, _: round(float(status), 1),
330+
value=lambda status, _: None if status is None else round(float(status), 1),
331331
device_class=SensorDeviceClass.VOLTAGE,
332332
state_class=SensorStateClass.MEASUREMENT,
333333
entity_registry_enabled_default=False,

tests/components/shelly/test_sensor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ async def test_rpc_sensor(hass, mock_rpc_device, monkeypatch) -> None:
180180

181181
assert hass.states.get(entity_id).state == "88.2"
182182

183+
mutate_rpc_device_status(monkeypatch, mock_rpc_device, "cover:0", "apower", None)
184+
mock_rpc_device.mock_update()
185+
186+
assert hass.states.get(entity_id).state == STATE_UNKNOWN
187+
183188

184189
async def test_rpc_sensor_error(hass, mock_rpc_device, monkeypatch):
185190
"""Test RPC sensor unavailable on sensor error."""

0 commit comments

Comments
 (0)