Skip to content

Commit

Permalink
add polling delay in config (#325)
Browse files Browse the repository at this point in the history
* add tpolling delay in config

* add doc
  • Loading branch information
marcolivierarsenault authored Apr 20, 2024
1 parent 4020c86 commit 51d5b47
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
35 changes: 16 additions & 19 deletions custom_components/moonraker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Moonraker integration for Home Assistant."""

import asyncio
import logging
import os.path
Expand All @@ -18,6 +19,7 @@
CONF_API_KEY,
CONF_PORT,
CONF_PRINTER_NAME,
CONF_OPTION_POLLING_RATE,
CONF_TLS,
CONF_URL,
DOMAIN,
Expand Down Expand Up @@ -54,6 +56,9 @@ def get_user_name(hass: HomeAssistant, entry: ConfigEntry):

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""

global SCAN_INTERVAL

if hass.data.get(DOMAIN) is None:
hass.data.setdefault(DOMAIN, {})

Expand All @@ -67,6 +72,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
entry.data.get(CONF_PRINTER_NAME) if custom_name is None else custom_name
)

if entry.options.get(CONF_OPTION_POLLING_RATE) is not None:
SCAN_INTERVAL = timedelta(seconds=entry.options.get(CONF_OPTION_POLLING_RATE))
else:
SCAN_INTERVAL = timedelta(seconds=30)

api = MoonrakerApiClient(
url,
async_get_clientsession(hass, verify_ssl=False),
Expand All @@ -76,7 +86,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
)

try:

async with async_timeout.timeout(TIMEOUT):
await api.start()
printer_info = await api.client.call_method("printer.info")
Expand Down Expand Up @@ -189,24 +198,12 @@ async def _async_get_gcode_file_detail(self, gcode_filename):
gcode = await self._async_fetch_data(
METHODS.SERVER_FILES_METADATA, query_object
)
return_gcode["estimated_time"] = (
gcode.get("estimated_time", 0)
)
return_gcode["object_height"] = (
gcode.get("object_height", 0)
)
return_gcode["filament_total"] = (
gcode.get("filament_total", 0)
)
return_gcode["layer_count"] = (
gcode.get("layer_count", 0)
)
return_gcode["layer_height"] = (
gcode.get("layer_height", 0)
)
return_gcode["first_layer_height"] = (
gcode.get("first_layer_height", 0)
)
return_gcode["estimated_time"] = gcode.get("estimated_time", 0)
return_gcode["object_height"] = gcode.get("object_height", 0)
return_gcode["filament_total"] = gcode.get("filament_total", 0)
return_gcode["layer_count"] = gcode.get("layer_count", 0)
return_gcode["layer_height"] = gcode.get("layer_height", 0)
return_gcode["first_layer_height"] = gcode.get("first_layer_height", 0)

try:
# Keep last since this can fail but, we still want the other data
Expand Down
8 changes: 8 additions & 0 deletions custom_components/moonraker/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Adds config flow for Moonraker."""

import logging

from typing import Any
Expand All @@ -18,6 +19,7 @@
CONF_PRINTER_NAME,
CONF_TLS,
CONF_URL,
CONF_OPTION_POLLING_RATE,
CONF_OPTION_CAMERA_STREAM,
CONF_OPTION_CAMERA_SNAPSHOT,
DOMAIN,
Expand Down Expand Up @@ -167,6 +169,12 @@ async def async_step_init(
step_id="init",
data_schema=vol.Schema(
{
vol.Optional(
CONF_OPTION_POLLING_RATE,
default=self.config_entry.options.get(
CONF_OPTION_POLLING_RATE, 30
),
): int,
vol.Optional(
CONF_OPTION_CAMERA_STREAM,
default=self.config_entry.options.get(
Expand Down
2 changes: 2 additions & 0 deletions custom_components/moonraker/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Constants for Moonraker."""

from enum import Enum

from homeassistant.const import Platform
Expand Down Expand Up @@ -26,6 +27,7 @@
CONF_PRINTER_NAME = "printer_name"
CONF_OPTION_CAMERA_STREAM = "camera_stream_url"
CONF_OPTION_CAMERA_SNAPSHOT = "camera_snapshot_url"
CONF_OPTION_POLLING_RATE = "polling_rate"

# API dict keys
HOSTNAME = "hostname"
Expand Down
3 changes: 2 additions & 1 deletion custom_components/moonraker/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
"step": {
"init": {
"data": {
"polling_rate": "Integration polling rate (s)",
"camera_stream_url": "Camera Stream URL",
"camera_snapshot_url": "Camera Snapshot URL"
},
"title": "Configuration Camera"
"title": "Configuration"
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions docs/connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ Note: Encrypted connections must be configured in Moonraker API or by using a
reverse proxy to connect to Moonraker API.


Polling Rate Configuration
-------------------------------------

It is possible to change the default polling rate value (30 sec) of the integration.

In the integration configuration, you can set the polling rate manually.

Camera Manual Configuration
-------------------------------------

Expand Down

0 comments on commit 51d5b47

Please sign in to comment.