From 1c65c52f79c3c1fc40c4e73199cef3f9769751ec Mon Sep 17 00:00:00 2001 From: Master92 Date: Wed, 29 May 2024 09:48:40 +0200 Subject: [PATCH 1/5] Add route to job queue status --- custom_components/moonraker/const.py | 1 + 1 file changed, 1 insertion(+) diff --git a/custom_components/moonraker/const.py b/custom_components/moonraker/const.py index fab7064..8c9261f 100644 --- a/custom_components/moonraker/const.py +++ b/custom_components/moonraker/const.py @@ -59,6 +59,7 @@ class METHODS(Enum): PRINTER_FIRMWARE_RESTART = "printer.firmware_restart" SERVER_FILES_METADATA = "server.files.metadata" SERVER_HISTORY_TOTALS = "server.history.totals" + SERVER_JOB_QUEUE_STATUS = "server.job_queue.status" SERVER_RESTART = "server.restart" SERVER_WEBCAMS_LIST = "server.webcams.list" From c6915adcd941b4e4a3704e626e79f6c4901fc8a0 Mon Sep 17 00:00:00 2001 From: Master92 Date: Wed, 29 May 2024 09:49:31 +0200 Subject: [PATCH 2/5] Add sensors for job queue status and count --- custom_components/moonraker/sensor.py | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/custom_components/moonraker/sensor.py b/custom_components/moonraker/sensor.py index ef47609..677fb1a 100755 --- a/custom_components/moonraker/sensor.py +++ b/custom_components/moonraker/sensor.py @@ -280,6 +280,7 @@ async def async_setup_entry(hass, entry, async_add_entities): await async_setup_basic_sensor(coordinator, entry, async_add_entities) await async_setup_optional_sensors(coordinator, entry, async_add_entities) await async_setup_history_sensors(coordinator, entry, async_add_entities) + await async_setup_queue_sensors(coordinator, entry, async_add_entities) await async_setup_machine_update_sensors(coordinator, entry, async_add_entities) @@ -579,6 +580,43 @@ 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): + queue = await coordinator.async_fetch_data(METHODS.SERVER_JOB_QUEUE_STATUS) + if queue.get("error"): + return + + coordinator.add_data_updater(_queue_updater) + + sensors = [ + MoonrakerSensorDescription( + key="queue_state", + name="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" + ]), + 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 { From 9748e735f0629f8db9bbf7a90363c7ae704726d1 Mon Sep 17 00:00:00 2001 From: Master92 Date: Mon, 3 Jun 2024 09:55:52 +0200 Subject: [PATCH 3/5] Add docstring to public function --- custom_components/moonraker/sensor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/custom_components/moonraker/sensor.py b/custom_components/moonraker/sensor.py index 677fb1a..7599f57 100755 --- a/custom_components/moonraker/sensor.py +++ b/custom_components/moonraker/sensor.py @@ -586,6 +586,7 @@ async def _queue_updater(coordinator): } 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) if queue.get("error"): return From fced780d82f87b3690a47e4a1d512d0f9a8e1709 Mon Sep 17 00:00:00 2001 From: Master92 Date: Mon, 3 Jun 2024 12:54:07 +0200 Subject: [PATCH 4/5] Fix failing unit tests --- custom_components/moonraker/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/moonraker/sensor.py b/custom_components/moonraker/sensor.py index 3b44f30..589a9a7 100755 --- a/custom_components/moonraker/sensor.py +++ b/custom_components/moonraker/sensor.py @@ -280,8 +280,8 @@ async def async_setup_entry(hass, entry, async_add_entities): await async_setup_basic_sensor(coordinator, entry, async_add_entities) await async_setup_optional_sensors(coordinator, entry, async_add_entities) await async_setup_history_sensors(coordinator, entry, async_add_entities) - await async_setup_queue_sensors(coordinator, entry, async_add_entities) await async_setup_machine_update_sensors(coordinator, entry, async_add_entities) + await async_setup_queue_sensors(coordinator, entry, async_add_entities) async def async_setup_basic_sensor(coordinator, entry, async_add_entities): From fe5edcba223783956a456c7229dc84c036ed13e5 Mon Sep 17 00:00:00 2001 From: Master92 Date: Fri, 7 Jun 2024 08:48:43 +0200 Subject: [PATCH 5/5] Add new sensors to documentation --- docs/entities/sensors.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/entities/sensors.rst b/docs/entities/sensors.rst index f759842..3068a53 100644 --- a/docs/entities/sensors.rst +++ b/docs/entities/sensors.rst @@ -97,6 +97,12 @@ Sensors that are added on integration startup. * - Total Layer - Total number of layer in the current print. - From Moonraker API (print_stats, info, total_layer). Make sure your Slicer include it. `Details `__ + * - Queue State + - State of the print job queue. + - From Moonraker API (ready, loading, starting, paused). `Details `__ + * - Jobs in queue + - Number of print jobs in the job queue. + - From Moonraker API (queued_jobs) History Sensors