Skip to content

Commit

Permalink
fix sensor, account for missing data
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolivierarsenault committed Aug 16, 2024
1 parent 957e911 commit 2847524
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
38 changes: 24 additions & 14 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,16 +415,21 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
status_key=obj,
name=f"{name} Load",
value_fn=lambda sensor: (
sensor.coordinator.data["status"][sensor.status_key]["last_stats"][
"mcu_task_avg"
]
+ 3
* sensor.coordinator.data["status"][sensor.status_key][
"last_stats"
]["mcu_task_stddev"]
(
sensor.coordinator.data["status"][sensor.status_key][
"last_stats"
]["mcu_task_avg"]
+ 3
* sensor.coordinator.data["status"][sensor.status_key][
"last_stats"
]["mcu_task_stddev"]
)
/ 0.0025
* 100
)
/ 0.0025
* 100,
if sensor.coordinator.data["status"][sensor.status_key]["last_stats"]
is not None
else 0,
subscriptions=[(obj, "last_stats")],
icon="mdi:cpu-64-bit",
state_class=SensorStateClass.MEASUREMENT,
Expand All @@ -435,11 +440,16 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
key=f"{key}_awake",
status_key=obj,
name=f"{name} Awake",
value_fn=lambda sensor: sensor.coordinator.data["status"][
sensor.status_key
]["last_stats"]["mcu_awake"]
/ 5
* 100,
value_fn=lambda sensor: (
sensor.coordinator.data["status"][sensor.status_key]["last_stats"][
"mcu_awake"
]
/ 5
* 100
)
if sensor.coordinator.data["status"][sensor.status_key]["last_stats"]
is not None
else 0,
icon="mdi:cpu-64-bit",
subscriptions=[(obj, "last_stats")],
state_class=SensorStateClass.MEASUREMENT,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ async def test_multi_mcu_sensor_data(hass, get_data, get_printer_objects_list):

config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry.add_to_hass(hass)
assert await async_setup_entry(hass, config_entry)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

registry = get_entity_registry(hass)
Expand Down

0 comments on commit 2847524

Please sign in to comment.