Skip to content

Commit

Permalink
Null temp value (#456)
Browse files Browse the repository at this point in the history
* add None management for optional sensors

* add tests
  • Loading branch information
marcolivierarsenault authored Dec 16, 2024
1 parent 79cfe45 commit a366960
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
46 changes: 37 additions & 9 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit a366960

Please sign in to comment.