Skip to content

Commit

Permalink
Add sensors for job queue status and queue count (#346)
Browse files Browse the repository at this point in the history
* Add route to job queue status

* Add sensors for job queue status and count

* Add docstring to public function

* Fix failing unit tests

* Add new sensors to documentation

---------

Co-authored-by: Marc-Olivier Arsenault <[email protected]>
  • Loading branch information
Master92 and marcolivierarsenault authored Jun 8, 2024
1 parent a737cff commit ff12213
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom_components/moonraker/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
39 changes: 39 additions & 0 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ async def async_setup_entry(hass, 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_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):
Expand Down Expand Up @@ -585,6 +586,44 @@ 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)
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 {
Expand Down
6 changes: 6 additions & 0 deletions docs/entities/sensors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/marcolivierarsenault/moonraker-home-assistant/issues/112#issuecomment-1505664692>`__
* - Queue State
- State of the print job queue.
- From Moonraker API (ready, loading, starting, paused). `Details <https://moonraker.readthedocs.io/en/latest/web_api/#retrieve-the-job-queue-status>`__
* - Jobs in queue
- Number of print jobs in the job queue.
- From Moonraker API (queued_jobs)


History Sensors
Expand Down

0 comments on commit ff12213

Please sign in to comment.