Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sensors for job queue status and queue count #346

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading