From a366960a40b86177ad15a178429f001cdbf5d9ab Mon Sep 17 00:00:00 2001 From: Marc-Olivier Arsenault Date: Sun, 15 Dec 2024 19:07:42 -0500 Subject: [PATCH] Null temp value (#456) * add None management for optional sensors * add tests --- custom_components/moonraker/sensor.py | 46 +++++++++++++++++++++------ tests/test_sensor.py | 17 ++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/custom_components/moonraker/sensor.py b/custom_components/moonraker/sensor.py index 44d8891..a3c15c3 100755 --- a/custom_components/moonraker/sensor.py +++ b/custom_components/moonraker/sensor.py @@ -352,9 +352,18 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities): status_key=obj, name=split_obj[1].removesuffix("_temp").replace("_", " ").title() + " Temp", - value_fn=lambda sensor: round( - sensor.coordinator.data["status"][sensor.status_key]["temperature"], - 2, + value_fn=lambda sensor: ( + round( + sensor.coordinator.data["status"][sensor.status_key][ + "temperature" + ], + 2, + ) + if sensor.coordinator.data["status"][sensor.status_key][ + "temperature" + ] + is not None + else None ), subscriptions=[(obj, "temperature")], icon="mdi:thermometer", @@ -531,9 +540,19 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities): key=f"{split_obj[0]}_{split_obj[1]}_temperature", status_key=obj, name=f"{split_obj[1].replace('_',' ')} Temperature".title(), - value_fn=lambda sensor: sensor.coordinator.data["status"][ - sensor.status_key - ]["temperature"], + value_fn=lambda sensor: ( + round( + sensor.coordinator.data["status"][sensor.status_key][ + "temperature" + ], + 2, + ) + if sensor.coordinator.data["status"][sensor.status_key][ + "temperature" + ] + is not None + else None + ), subscriptions=[(obj, "temperature")], icon="mdi:thermometer", unit=UnitOfTemperature.CELSIUS, @@ -566,9 +585,18 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities): key=f"{obj}_temp", status_key=obj, name=f"{base_name} Temperature".title(), - value_fn=lambda sensor: float( - sensor.coordinator.data["status"][sensor.status_key]["temperature"] - or 0.0 + value_fn=lambda sensor: ( + round( + sensor.coordinator.data["status"][sensor.status_key][ + "temperature" + ], + 2, + ) + if sensor.coordinator.data["status"][sensor.status_key][ + "temperature" + ] + is not None + else None ), subscriptions=[(obj, "temperature")], icon=icon, diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 6cbbeb6..e1c90de 100755 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -601,3 +601,20 @@ async def test_update_no_info_item(hass, get_machine_update_status): entity = entity_registry.async_get("sensor.mainsail_machine_update_system") assert entity is not None + + +async def test_optional_sensor_is_none(hass, get_data): + """Test.""" + get_data["status"]["temperature_sensor mcu_temp"]["temperature"] = None + + config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test") + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + entity_registry = er.async_get(hass) + entity = entity_registry.async_get("sensor.mainsail_mcu_temp") + assert entity is not None + + state = hass.states.get("sensor.mainsail_mcu_temp") + assert state.state == "unknown"