From 51d5b47e617eeb2f533e6a83686536599349e528 Mon Sep 17 00:00:00 2001 From: Marc-Olivier Arsenault Date: Sat, 20 Apr 2024 12:06:45 -0400 Subject: [PATCH] add polling delay in config (#325) * add tpolling delay in config * add doc --- custom_components/moonraker/__init__.py | 35 +++++++++---------- custom_components/moonraker/config_flow.py | 8 +++++ custom_components/moonraker/const.py | 2 ++ .../moonraker/translations/en.json | 3 +- docs/connection.rst | 7 ++++ 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/custom_components/moonraker/__init__.py b/custom_components/moonraker/__init__.py index c2c9d77..b58118e 100755 --- a/custom_components/moonraker/__init__.py +++ b/custom_components/moonraker/__init__.py @@ -1,4 +1,5 @@ """Moonraker integration for Home Assistant.""" + import asyncio import logging import os.path @@ -18,6 +19,7 @@ CONF_API_KEY, CONF_PORT, CONF_PRINTER_NAME, + CONF_OPTION_POLLING_RATE, CONF_TLS, CONF_URL, DOMAIN, @@ -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, {}) @@ -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), @@ -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") @@ -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 diff --git a/custom_components/moonraker/config_flow.py b/custom_components/moonraker/config_flow.py index 8c713b6..ec29a5a 100755 --- a/custom_components/moonraker/config_flow.py +++ b/custom_components/moonraker/config_flow.py @@ -1,4 +1,5 @@ """Adds config flow for Moonraker.""" + import logging from typing import Any @@ -18,6 +19,7 @@ CONF_PRINTER_NAME, CONF_TLS, CONF_URL, + CONF_OPTION_POLLING_RATE, CONF_OPTION_CAMERA_STREAM, CONF_OPTION_CAMERA_SNAPSHOT, DOMAIN, @@ -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( diff --git a/custom_components/moonraker/const.py b/custom_components/moonraker/const.py index a77ea14..fab7064 100644 --- a/custom_components/moonraker/const.py +++ b/custom_components/moonraker/const.py @@ -1,4 +1,5 @@ """Constants for Moonraker.""" + from enum import Enum from homeassistant.const import Platform @@ -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" diff --git a/custom_components/moonraker/translations/en.json b/custom_components/moonraker/translations/en.json index 15639f0..cfdb641 100644 --- a/custom_components/moonraker/translations/en.json +++ b/custom_components/moonraker/translations/en.json @@ -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" } } } diff --git a/docs/connection.rst b/docs/connection.rst index b41dcdc..649219d 100644 --- a/docs/connection.rst +++ b/docs/connection.rst @@ -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 -------------------------------------