Skip to content

Commit

Permalink
Updates and fix broken code (#378)
Browse files Browse the repository at this point in the history
* update 3rd party deps

* update ha

* Revert "Add some system stats sensors. System Load, MCU Load, Memory Used (#354)"

This reverts commit d17985e.

* keep old ha
  • Loading branch information
marcolivierarsenault authored Aug 10, 2024
1 parent d520973 commit cb83c80
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 167 deletions.
1 change: 0 additions & 1 deletion custom_components/moonraker/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class METHODS(Enum):
MACHINE_DEVICE_POWER_POST_DEVICE = "machine.device_power.post_device"
MACHINE_UPDATE_REFRESH = "machine.update.refresh"
MACHINE_UPDATE_STATUS = "machine.update.status"
MACHINE_SYSTEM_INFO = "machine.system_info"
PRINTER_EMERGENCY_STOP = "printer.emergency_stop"
PRINTER_INFO = "printer.info"
PRINTER_GCODE_HELP = "printer.gcode.help"
Expand Down
105 changes: 10 additions & 95 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,25 +270,6 @@ class MoonrakerSensorDescription(SensorEntityDescription):
device_class=SensorDeviceClass.DISTANCE,
unit=UnitOfLength.MILLIMETERS,
),
MoonrakerSensorDescription(
key="sysload",
name="System Load",
value_fn=lambda sensor: round(
sensor.coordinator.data["status"]["system_stats"]["sysload"], 2
),
subscriptions=[("system_stats", "sysload")],
icon="mdi:cpu-64-bit",
state_class=SensorStateClass.MEASUREMENT,
),
MoonrakerSensorDescription(
key="memused",
name="Memory Used",
value_fn=lambda sensor: calculate_memory_used(sensor.coordinator.data) or 0.0,
subscriptions=[("system_stats", "memavail")],
icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT,
unit=PERCENTAGE,
),
]


Expand All @@ -303,17 +284,8 @@ async def async_setup_entry(hass, entry, async_add_entities):
await async_setup_queue_sensors(coordinator, entry, async_add_entities)


async def _machine_system_info_updater(coordinator):
return {
"system_info": (
await coordinator.async_fetch_data(METHODS.MACHINE_SYSTEM_INFO)
)["system_info"]
}


async def async_setup_basic_sensor(coordinator, entry, async_add_entities):
"""Set basic sensor platform."""
coordinator.add_data_updater(_machine_system_info_updater)
coordinator.load_sensor_data(SENSORS)
async_add_entities([MoonrakerSensor(coordinator, entry, desc) for desc in SENSORS])

Expand Down Expand Up @@ -403,49 +375,7 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
state_class=SensorStateClass.MEASUREMENT,
)
sensors.append(desc)
elif split_obj[0] == "mcu":
if len(split_obj) > 1:
key = f"{split_obj[0]}_{split_obj[1]}"
name = obj.replace("_", " ").title()
else:
key = split_obj[0]
name = split_obj[0].title()
desc = MoonrakerSensorDescription(
key=f"{key}_load",
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"]
)
/ 0.0025
* 100,
subscriptions=[(obj, "last_stats")],
icon="mdi:cpu-64-bit",
state_class=SensorStateClass.MEASUREMENT,
unit=PERCENTAGE,
)
sensors.append(desc)
desc = MoonrakerSensorDescription(
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,
icon="mdi:cpu-64-bit",
subscriptions=[(obj, "last_stats")],
state_class=SensorStateClass.MEASUREMENT,
unit=PERCENTAGE,
)
sensors.append(desc)

elif split_obj[0] in fan_keys:
desc = MoonrakerSensorDescription(
key=f"{split_obj[0]}_{split_obj[1]}",
Expand Down Expand Up @@ -497,8 +427,7 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
(
sensor.coordinator.data["status"][sensor.status_key]["power"]
or 0.0
)
* 100
) * 100
),
subscriptions=[(obj, "power")],
icon="mdi:flash",
Expand Down Expand Up @@ -580,8 +509,7 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
(
sensor.coordinator.data["status"][sensor.status_key]["power"]
or 0.0
)
* 100
) * 100
),
subscriptions=[(obj, "power")],
icon="mdi:flash",
Expand Down Expand Up @@ -658,13 +586,11 @@ async def async_setup_history_sensors(coordinator, entry, async_add_entities):
await coordinator.async_refresh()
async_add_entities([MoonrakerSensor(coordinator, entry, desc) for desc in sensors])


async def _queue_updater(coordinator):
return {
"queue": await coordinator.async_fetch_data(METHODS.SERVER_JOB_QUEUE_STATUS)
}


async def async_setup_queue_sensors(coordinator, entry, async_add_entities):
"""Job queue sensors."""
queue = await coordinator.async_fetch_data(METHODS.SERVER_JOB_QUEUE_STATUS)
Expand All @@ -677,27 +603,28 @@ async def async_setup_queue_sensors(coordinator, entry, async_add_entities):
MoonrakerSensorDescription(
key="queue_state",
name="Queue State",
value_fn=lambda sensor: sensor.coordinator.data["queue"]["queue_state"],
value_fn=lambda sensor: sensor.coordinator.data["queue"][
"queue_state"
],
subscriptions=[("queue_state")],
),
MoonrakerSensorDescription(
key="queue_count",
name="Jobs in queue",
value_fn=lambda sensor: len(
sensor.coordinator.data["queue"]["queued_jobs"]
),
value_fn=lambda sensor: len(sensor.coordinator.data["queue"][
"queued_jobs"
]),
subscriptions=[("queued_jobs")],
icon="mdi:numeric",
unit="Jobs",
state_class=SensorStateClass.MEASUREMENT,
),
)
]

coordinator.load_sensor_data(sensors)
await coordinator.async_refresh()
async_add_entities([MoonrakerSensor(coordinator, entry, desc) for desc in sensors])


async def _machine_update_updater(coordinator):
return {
"machine_update": await coordinator.async_fetch_data(
Expand Down Expand Up @@ -873,15 +800,3 @@ def convert_time(time_s):
return (
f"{round(time_s // 3600)}h {round(time_s % 3600 // 60)}m {round(time_s % 60)}s"
)


def calculate_memory_used(data):
"""Calculate memory used."""

if "system_info" not in data:
return None

total_memory = data["system_info"]["cpu_info"]["total_memory"]
memory_used = total_memory - data["status"]["system_stats"]["memavail"]
percent_mem_used = memory_used / total_memory * 100
return round(percent_mem_used, 2)
4 changes: 2 additions & 2 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sphinx==7.4.4
furo==2024.5.6
sphinx==7.4.7
furo==2024.7.18
sphinx-toolbox==3.7.0
11 changes: 5 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
pytest-homeassistant-custom-component==0.13.127
pytest-homeassistant-custom-component==0.13.128
moonraker-api==2.0.5
aiohttp_cors==0.7.0
Pillow==10.3.0
pre-commit==3.7.1
sphinx==7.4.4
pre-commit==3.8.0
sphinx==7.4.7
colorlog==6.8.2
furo==2024.5.6
ruff==0.5.2
furo==2024.7.18
ruff==0.5.6
ha-ffmpeg==3.2.0
sphinx-toolbox==3.7.0
34 changes: 0 additions & 34 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ def get_data_fixture():
return {
"eventtime": 128684.342831779,
"status": {
"system_stats": {
"sysload": 0.244140625,
"cputime": 384.86964173,
"memavail": 1291812,
},
"configfile": {
"settings": {
"output_pin digital": {
Expand Down Expand Up @@ -174,13 +169,6 @@ def get_data_fixture():
"gcode_move": {
"speed_factor": 2.0,
},
"mcu": {
"last_stats": {
"mcu_awake": 0.031,
"mcu_task_avg": 0.000002,
"mcu_task_stddev": 0.000012,
},
},
},
"printer.info": {
"result": {
Expand Down Expand Up @@ -388,26 +376,6 @@ def get_machine_update_status_fixture():
}


@pytest.fixture(name="get_machine_system_info")
def get_machine_system_info_fixture():
"""Get Machine Update Status fixture."""
return {
"system_info": {
"cpu_info": {
"cpu_count": 4,
"bits": "64bit",
"processor": "aarch64",
"cpu_desc": "",
"serial_number": "10000000e2e0a55f",
"hardware_desc": "",
"model": "Raspberry Pi Compute Module 4 Rev 1.1",
"total_memory": 1891256,
"memory_units": "kB",
},
}
}


@pytest.fixture(name="get_default_api_response")
def get_default_api_response_fixure(
get_data,
Expand All @@ -418,7 +386,6 @@ def get_default_api_response_fixure(
get_gcode_help,
get_power_devices,
get_machine_update_status,
get_machine_system_info,
):
"""Get all the default fixture returned by moonraker."""
return {
Expand All @@ -430,7 +397,6 @@ def get_default_api_response_fixure(
**get_gcode_help,
**get_power_devices,
**get_machine_update_status,
**get_machine_system_info,
}


Expand Down
29 changes: 0 additions & 29 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,35 +362,6 @@ async def test_no_fan_sensor(hass, get_data, get_printer_objects_list):
assert state is None


async def test_multi_mcu_sensor_data(hass, get_data, get_printer_objects_list):
"""Test."""
get_printer_objects_list["objects"].append("mcu Extruder")
get_data["status"]["mcu Extruder"] = {
"last_stats": {
"mcu_awake": 0.031,
"mcu_task_avg": 0.000002,
"mcu_task_stddev": 0.000012,
},
}

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.async_block_till_done()

registry = get_entity_registry(hass)

assert (
registry.async_get_entity_id("sensor", DOMAIN, "test_mcu_Extruder_load")
is not None
)

assert (
registry.async_get_entity_id("sensor", DOMAIN, "test_mcu_Extruder_awake")
is not None
)


async def test_rounding_fan(hass, get_data):
"""Test."""
get_data["status"]["fan"]["speed"] = 0.33333333333
Expand Down

0 comments on commit cb83c80

Please sign in to comment.