From bc0209df5547a5965cfe3af5dfd9c67f48afe795 Mon Sep 17 00:00:00 2001 From: Elad Bar Date: Sun, 16 Jun 2024 11:13:18 +0300 Subject: [PATCH 1/2] bug fixes --- CHANGELOG.md | 16 ++ custom_components/hpprinter/common/consts.py | 13 +- .../hpprinter/managers/ha_config_manager.py | 25 +--- .../hpprinter/managers/ha_coordinator.py | 3 +- .../hpprinter/managers/rest_api.py | 139 ++++++++++++++---- custom_components/hpprinter/manifest.json | 2 +- .../hpprinter/parameters/data_points.json | 8 +- requirements.txt | 3 + utils/api_test.py | 11 +- 9 files changed, 151 insertions(+), 69 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63d2beb..5fad1b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # Changelog +## 2.0.2 + +- Use HA client session instead of aiohttp directly +- Improved validation for enum sensors (Status and Consumable Type) +- Set update interval per endpoint (Default - 5m), instead of 5 minutes as configuration + +| Endpoint | Data | Interval | Times a day | +| -------------------------------- | ------------------------------------------ | -------- | ----------- | +| /DevMgmt/ProductConfigDyn.xml | Main device details | 52w | 0.0027 | +| /DevMgmt/ProductStatusDyn.xml | Device status | 10s | 8,640 | +| /DevMgmt/ConsumableConfigDyn.xml | Consumables | 5m | 288 | +| /DevMgmt/ProductUsageDyn.xml | Consumables, Printer, Scanner, Copier, Fax | 5m | 288 | +| /ePrint/ePrintConfigDyn.xml | ePrint | 5m | 288 | +| /IoMgmt/Adapters | Network Adapters | 5m | 288 | +| /DevMgmt/NetAppsSecureDyn.xml | Wifi | 5m | 288 | + ## 2.0.1 - Set printer status to `Off` when printer is offline, instead of reset data diff --git a/custom_components/hpprinter/common/consts.py b/custom_components/hpprinter/common/consts.py index 1081bc9..7282545 100644 --- a/custom_components/hpprinter/common/consts.py +++ b/custom_components/hpprinter/common/consts.py @@ -34,10 +34,9 @@ CONFIGURATION_FILE = f"{DOMAIN}.config.json" LEGACY_KEY_FILE = f"{DOMAIN}.key" -UPDATE_API_INTERVAL = timedelta(minutes=5) +UPDATE_API_INTERVAL = timedelta(seconds=1) DEFAULT_ENTRY_ID = "config" -CONF_UPDATE_INTERVAL = "update_interval" CONF_TITLE = "title" DEFAULT_PORT = 80 @@ -53,3 +52,13 @@ PRODUCT_STATUS_OFFLINE_PAYLOAD = { "ProductStatusDyn": {"Status": [{"StatusCategory": "off"}]} } + +DURATION_UNITS = { + "s": "seconds", + "m": "minutes", + "h": "hours", + "d": "days", + "w": "weeks", +} + +DEFAULT_INTERVAL = "5m" diff --git a/custom_components/hpprinter/managers/ha_config_manager.py b/custom_components/hpprinter/managers/ha_config_manager.py index f79b935..58d5809 100644 --- a/custom_components/hpprinter/managers/ha_config_manager.py +++ b/custom_components/hpprinter/managers/ha_config_manager.py @@ -1,4 +1,3 @@ -from datetime import timedelta import json import logging import os @@ -14,13 +13,7 @@ from homeassistant.helpers.json import JSONEncoder from homeassistant.helpers.storage import Store -from ..common.consts import ( - CONF_UPDATE_INTERVAL, - CONFIGURATION_FILE, - DEFAULT_ENTRY_ID, - DEFAULT_NAME, - DOMAIN, -) +from ..common.consts import CONFIGURATION_FILE, DEFAULT_ENTRY_ID, DEFAULT_NAME, DOMAIN from ..common.entity_descriptions import ( IntegrationBinarySensorEntityDescription, IntegrationEntityDescription, @@ -100,13 +93,6 @@ def config_data(self) -> ConfigData: return config_data - @property - def update_interval(self) -> timedelta: - interval = self._data.get(CONF_UPDATE_INTERVAL, 5) - result = timedelta(minutes=interval) - - return result - @property def endpoints(self) -> list[str] | None: endpoints = self._endpoints @@ -191,13 +177,6 @@ def get_entity_name( return entity_name - async def set_update_interval(self, value: int): - _LOGGER.debug(f"Set update interval in minutes to to {value}") - - self._data[CONF_UPDATE_INTERVAL] = value - - await self._save() - def get_debug_data(self) -> dict: data = self._config_data.to_dict() @@ -233,7 +212,7 @@ async def _load_config_from_file(self): @staticmethod def _get_defaults() -> dict: - data = {CONF_UPDATE_INTERVAL: 5} + data = {} return data diff --git a/custom_components/hpprinter/managers/ha_coordinator.py b/custom_components/hpprinter/managers/ha_coordinator.py index 68447e5..e28e724 100644 --- a/custom_components/hpprinter/managers/ha_coordinator.py +++ b/custom_components/hpprinter/managers/ha_coordinator.py @@ -15,6 +15,7 @@ PRINTER_MAIN_DEVICE, SIGNAL_HA_DEVICE_CREATED, SIGNAL_HA_DEVICE_DISCOVERED, + UPDATE_API_INTERVAL, ) from .ha_config_manager import HAConfigManager from .rest_api import RestAPIv2 @@ -35,7 +36,7 @@ def __init__( hass, _LOGGER, name=config_manager.entry_title, - update_interval=config_manager.update_interval, + update_interval=UPDATE_API_INTERVAL, update_method=self._async_update_data, ) diff --git a/custom_components/hpprinter/managers/rest_api.py b/custom_components/hpprinter/managers/rest_api.py index 0a0329c..b41b333 100644 --- a/custom_components/hpprinter/managers/rest_api.py +++ b/custom_components/hpprinter/managers/rest_api.py @@ -1,3 +1,4 @@ +from datetime import datetime, timedelta import json import logging import sys @@ -12,12 +13,15 @@ ENABLE_CLEANUP_CLOSED, MAXIMUM_CONNECTIONS, MAXIMUM_CONNECTIONS_PER_HOST, + async_create_clientsession, ) from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.util import slugify, ssl from homeassistant.util.ssl import SSLCipherList from ..common.consts import ( + DEFAULT_INTERVAL, + DURATION_UNITS, IGNORED_KEYS, PRODUCT_STATUS_ENDPOINT, PRODUCT_STATUS_OFFLINE_PAYLOAD, @@ -40,6 +44,8 @@ def __init__(self, hass, config_manager: HAConfigManager): self._data: dict = {} self._data_config: dict = {} + self._last_update: dict[str, float] = {} + self._update_intervals: dict[str, int] = {} self._raw_data: dict = {} @@ -84,10 +90,12 @@ async def initialize(self, throw_exception: bool = False): raise IntegrationParameterError(CONF_HOST) if self._session is None: - self._session = ClientSession( - loop=self._loop, connector=self._get_ssl_connector() - ) + if self._hass is None: + self._session = ClientSession(loop=self._loop) + else: + self._session = async_create_clientsession(hass=self._hass) + self._load_update_intervals() await self._load_metadata() except Exception as ex: @@ -134,7 +142,9 @@ async def _load_metadata(self): else: self._all_endpoints = self._config_manager.endpoints.copy() - await self._update_data(self._config_manager.endpoints, False) + updates = await self._update_data(self._config_manager.endpoints, False) + + _LOGGER.debug(f"Startup: {updates} endpoints were updated") endpoints_found = len(self._raw_data.keys()) is_connected = endpoints_found > 0 @@ -153,17 +163,44 @@ async def _load_metadata(self): self._is_connected = is_connected async def update(self): - await self._update_data(self._config_manager.endpoints) + updates = await self._update_data(self._config_manager.endpoints) + + _LOGGER.debug(f"Scheduled update: {updates} endpoints were updated") async def update_full(self): - await self._update_data(self._all_endpoints) + updates = await self._update_data(self._all_endpoints) + + _LOGGER.debug(f"Full update: {updates} endpoints were updated") + + async def _update_data( + self, endpoints: list[str], connectivity_check: bool = True + ) -> int: + endpoints_updated = 0 - async def _update_data(self, endpoints: list[str], connectivity_check: bool = True): if not self._is_connected and connectivity_check: - return + return endpoints_updated + + now = datetime.now() + now_ts = now.timestamp() for endpoint in endpoints: + last_update = ( + self._last_update.get(endpoint, 0) if connectivity_check else 0 + ) + last_update_diff = int(now_ts - last_update) + interval = self._update_intervals.get(endpoint, 0) + + if interval > last_update_diff: + _LOGGER.debug( + f"Skip updating '{endpoint}', " + f"it's too soon, " + f"interval: {interval}s, " + f"time since last update: {last_update_diff}s" + ) + continue + resource_data = await self._get_request(endpoint) + endpoints_updated += 1 if resource_data is None: if endpoint == PRODUCT_STATUS_ENDPOINT: @@ -172,10 +209,15 @@ async def _update_data(self, endpoints: list[str], connectivity_check: bool = Tr else: self._raw_data[endpoint] = resource_data + if connectivity_check: + self._last_update[endpoint] = now.timestamp() + devices = self._get_devices_data() self._extract_data(devices) + return endpoints_updated + def _extract_data(self, devices: list[dict]): device_data = {} device_config = {} @@ -286,6 +328,13 @@ def _get_data_section(data: dict, path: str) -> dict: return result + def _load_update_intervals(self): + for data_point in self._config_manager.data_points: + endpoint = data_point.get("endpoint") + interval = data_point.get("interval", DEFAULT_INTERVAL) + + self._update_intervals[endpoint] = self._convert_to_seconds(interval) + def _get_devices_data(self): devices = [] @@ -326,26 +375,21 @@ def _get_device_data( for property_key in properties: property_details = properties.get(property_key) property_path = property_details.get("path") - property_accept = property_details.get("accept") - - is_valid = True + options = property_details.get("options") + validation_warning = property_details.get("validationWarning", False) + value = data_item_flat.get(property_path) - if property_accept is not None: - for property_accept_key in property_accept: - property_accept_data = property_accept[property_accept_key] + is_valid = True if options is None else str(value).lower() in options - if data_item.get(property_accept_key) != property_accept_data: - is_valid = False - _LOGGER.debug( - f"Ignoring {property_key}, " - f"not match to accept criteria {property_accept_key}: {property_accept_data}" - ) - - if is_valid: - value = data_item_flat.get(property_path) - - if value is not None: + if value is not None: + if is_valid: device_data[property_key] = value + else: + log = _LOGGER.warning if validation_warning else _LOGGER.debug + + log( + f"Unsupported value of {property_key}, expecting: {options}, received: {value}" + ) data = {"config": device_config, "data": device_data} @@ -355,10 +399,12 @@ async def _get_request( self, endpoint: str, ignore_error: bool = False ) -> dict | None: result: dict | None = None + start_ts = datetime.now().timestamp() + try: url = f"{self.config_data.url}{endpoint}" - timeout = ClientTimeout(connect=3, sock_read=10) + timeout = ClientTimeout(total=5) async with self._session.get(url, timeout=timeout) as response: response.raise_for_status() @@ -379,31 +425,52 @@ async def _get_request( if ignored_key in result[root_key]: del result[root_key][ignored_key] - _LOGGER.debug(f"Request to {url}") + completed_ts = datetime.now().timestamp() + time_taken = completed_ts - start_ts + _LOGGER.debug(f"Request to {url} completed, Time: {time_taken:.3f}s") except ClientResponseError as cre: if cre.status == 404: if not ignore_error: exc_type, exc_obj, tb = sys.exc_info() line_number = tb.tb_lineno + completed_ts = datetime.now().timestamp() + time_taken = completed_ts - start_ts + _LOGGER.debug( - f"Failed to get response from {endpoint}, Error: {cre}, Line: {line_number}" + f"Failed to get response from {endpoint}, " + f"Error: {cre}, " + f"Line: {line_number}, " + f"Time: {time_taken:.3f}s" ) else: if not ignore_error: exc_type, exc_obj, tb = sys.exc_info() line_number = tb.tb_lineno + completed_ts = datetime.now().timestamp() + time_taken = completed_ts - start_ts + _LOGGER.error( - f"Failed to get response from {endpoint}, Error: {cre}, Line: {line_number}" + f"Failed to get response from {endpoint}, " + f"Error: {cre}, " + f"Line: {line_number}, " + f"Time: {time_taken:.3f}s" ) except Exception as ex: if not ignore_error: exc_type, exc_obj, tb = sys.exc_info() line_number = tb.tb_lineno + + completed_ts = datetime.now().timestamp() + time_taken = completed_ts - start_ts + _LOGGER.error( - f"Failed to get {endpoint}, Error: {ex}, Line: {line_number}" + f"Failed to get {endpoint}, " + f"Error: {ex}, " + f"Line: {line_number}, " + f"Time: {time_taken:.3f}s" ) return result @@ -447,3 +514,15 @@ def device_data_changed(self, device_key: str): device_data, device_config, ) + + @staticmethod + def _convert_to_seconds(duration: str | None) -> int: + if duration is None: + duration = DEFAULT_INTERVAL + + count = int(duration[:-1]) + unit = DURATION_UNITS[duration[-1]] + td = timedelta(**{unit: count}) + seconds = td.seconds + 60 * 60 * 24 * td.days + + return seconds diff --git a/custom_components/hpprinter/manifest.json b/custom_components/hpprinter/manifest.json index b633132..38eae55 100644 --- a/custom_components/hpprinter/manifest.json +++ b/custom_components/hpprinter/manifest.json @@ -9,5 +9,5 @@ "iot_class": "local_polling", "issue_tracker": "https://github.com/elad-bar/ha-hpprinter/issues", "requirements": ["xmltodict~=0.13.0", "flatten_json", "defusedxml"], - "version": "2.0.0" + "version": "2.0.2" } diff --git a/custom_components/hpprinter/parameters/data_points.json b/custom_components/hpprinter/parameters/data_points.json index 87a17b3..dea458c 100644 --- a/custom_components/hpprinter/parameters/data_points.json +++ b/custom_components/hpprinter/parameters/data_points.json @@ -4,6 +4,7 @@ "endpoint": "/DevMgmt/ProductConfigDyn.xml", "path": "ProductConfigDyn.ProductInformation", "device_type": "Main", + "interval": "52w", "properties": { "make_and_model": { "path": "MakeAndModel" @@ -59,6 +60,7 @@ "path": "ConsumableTypeEnum", "platform": "sensor", "device_class": "enum", + "validationWarning": true, "options": [ "ink", "inkcartridge", @@ -404,6 +406,7 @@ "name": "Status", "endpoint": "/DevMgmt/ProductStatusDyn.xml", "path": "ProductStatusDyn.Status", + "interval": "10s", "device_type": "Main", "properties": { "device_status": { @@ -418,10 +421,7 @@ "processing", "canceljob", "inpowersave" - ], - "accept": { - "LocString": null - } + ] } } } diff --git a/requirements.txt b/requirements.txt index 8af47d0..170bc6c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,6 @@ translators~= 5.4 deep-translator~=1.9 python-slugify~=4.0.1 + +custom_components~=0.0.0 +aiofiles~=23.2.1 diff --git a/utils/api_test.py b/utils/api_test.py index 9f6a587..970dc4a 100644 --- a/utils/api_test.py +++ b/utils/api_test.py @@ -1,11 +1,10 @@ import asyncio -import json import logging import os import sys from custom_components.hpprinter import HAConfigManager -from custom_components.hpprinter.common.consts import DATA_KEYS, PRINTER_MAIN_DEVICE +from custom_components.hpprinter.common.consts import DATA_KEYS from custom_components.hpprinter.managers.rest_api import RestAPIv2 from homeassistant.core import HomeAssistant @@ -49,10 +48,9 @@ async def initialize(self): # print(json.dumps(self._api.data_config, indent=4)) # print(json.dumps(self._api.data, indent=4)) - print(json.dumps(self._api.data[PRINTER_MAIN_DEVICE], indent=4)) + # print(json.dumps(self._api.data[PRINTER_MAIN_DEVICE], indent=4)) - async def terminate(self): - await self._api.terminate() + await asyncio.sleep(5) if __name__ == "__main__": @@ -68,6 +66,3 @@ async def terminate(self): except Exception as rex: _LOGGER.error(f"Error: {rex}") - - finally: - loop.run_until_complete(instance.terminate()) From f3e04ee733b177d3a437ff28fab34cfcb80fff67 Mon Sep 17 00:00:00 2001 From: Elad Bar Date: Sun, 16 Jun 2024 14:52:25 +0300 Subject: [PATCH 2/2] update interval is being set by the minimum update interval of endpoints --- custom_components/hpprinter/__init__.py | 6 +- .../hpprinter/managers/ha_config_manager.py | 69 ++++++++++++++++--- .../hpprinter/managers/ha_coordinator.py | 3 +- .../hpprinter/managers/rest_api.py | 33 +-------- 4 files changed, 63 insertions(+), 48 deletions(-) diff --git a/custom_components/hpprinter/__init__.py b/custom_components/hpprinter/__init__.py index e8f66e4..64533ea 100644 --- a/custom_components/hpprinter/__init__.py +++ b/custom_components/hpprinter/__init__.py @@ -2,7 +2,7 @@ import sys from homeassistant.config_entries import ConfigEntry -from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP +from homeassistant.const import EVENT_HOMEASSISTANT_START from homeassistant.core import HomeAssistant from .common.consts import DEFAULT_NAME, DOMAIN @@ -41,10 +41,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: EVENT_HOMEASSISTANT_START, coordinator.on_home_assistant_start ) - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, coordinator.on_home_assistant_stop - ) - _LOGGER.info("Finished loading integration") initialized = is_initialized diff --git a/custom_components/hpprinter/managers/ha_config_manager.py b/custom_components/hpprinter/managers/ha_config_manager.py index 58d5809..2498065 100644 --- a/custom_components/hpprinter/managers/ha_config_manager.py +++ b/custom_components/hpprinter/managers/ha_config_manager.py @@ -1,3 +1,4 @@ +from datetime import timedelta import json import logging import os @@ -13,7 +14,14 @@ from homeassistant.helpers.json import JSONEncoder from homeassistant.helpers.storage import Store -from ..common.consts import CONFIGURATION_FILE, DEFAULT_ENTRY_ID, DEFAULT_NAME, DOMAIN +from ..common.consts import ( + CONFIGURATION_FILE, + DEFAULT_ENTRY_ID, + DEFAULT_INTERVAL, + DEFAULT_NAME, + DOMAIN, + DURATION_UNITS, +) from ..common.entity_descriptions import ( IntegrationBinarySensorEntityDescription, IntegrationEntityDescription, @@ -32,6 +40,13 @@ class HAConfigManager: _entry_title: str _config_data: ConfigData _store: Store | None + _update_intervals: dict[str, int] | None + _data_points: dict | None + _endpoints: list[str] | None + _exclude_uri_list: list[str] | None + _exclude_type_list: list[str] | None + _entity_descriptions: list[IntegrationEntityDescription] | None + _minimum_update_interval: timedelta def __init__(self, hass: HomeAssistant | None, entry: ConfigEntry | None): self._hass = hass @@ -39,15 +54,19 @@ def __init__(self, hass: HomeAssistant | None, entry: ConfigEntry | None): self._data = None self.platforms = [] - self._entity_descriptions: list[IntegrationEntityDescription] | None = None + self._entity_descriptions = None self._translations = None - self._endpoints: list[str] | None = None + self._endpoints = None + + self._default_update_interval = self._convert_to_seconds(DEFAULT_INTERVAL) - self._data_points: dict | None = None - self._exclude_uri_list: list[str] | None = None - self._exclude_type_list: list[str] | None = None + self._update_intervals = None + self._minimum_update_interval = timedelta(seconds=self._default_update_interval) + self._data_points = None + self._exclude_uri_list = None + self._exclude_type_list = None self._entry = entry self._entry_id = DEFAULT_ENTRY_ID if entry is None else entry.entry_id @@ -81,6 +100,10 @@ def entry_title(self) -> str: return entry_title + @property + def minimum_update_interval(self) -> timedelta: + return self._minimum_update_interval + @property def entry(self) -> ConfigEntry: entry = self._entry @@ -358,20 +381,27 @@ def _is_valid_entity( return is_valid async def _load_data_points_configuration(self): - self._endpoints = [] - + self._update_intervals = {} self._data_points = await self._get_parameters(ParameterType.DATA_POINTS) endpoint_objects = self._data_points for endpoint in endpoint_objects: endpoint_uri = endpoint.get("endpoint") + interval = endpoint.get("interval", DEFAULT_INTERVAL) if ( - endpoint_uri not in self._endpoints + endpoint_uri not in self._update_intervals and endpoint_uri not in self._exclude_uri_list ): - self._endpoints.append(endpoint_uri) + self._update_intervals[endpoint_uri] = self._convert_to_seconds( + interval + ) + + minimum_update_interval = min(self._update_intervals.values()) + self._minimum_update_interval = timedelta(seconds=minimum_update_interval) + + self._endpoints = list(self._update_intervals.keys()) async def _load_exclude_endpoints_configuration(self): endpoints = await self._get_parameters(ParameterType.ENDPOINT_VALIDATIONS) @@ -379,6 +409,13 @@ async def _load_exclude_endpoints_configuration(self): self._exclude_uri_list = endpoints.get("exclude_uri") self._exclude_type_list = endpoints.get("exclude_type") + def get_update_interval(self, endpoint: str) -> int: + update_interval = self._update_intervals.get( + endpoint, self._default_update_interval + ) + + return update_interval + @staticmethod async def _get_parameters(parameter_type: ParameterType) -> dict: config_file = f"{parameter_type}.json" @@ -394,6 +431,18 @@ async def _get_parameters(parameter_type: ParameterType) -> dict: return data + @staticmethod + def _convert_to_seconds(duration: str | None) -> int: + if duration is None: + duration = DEFAULT_INTERVAL + + count = int(duration[:-1]) + unit = DURATION_UNITS[duration[-1]] + td = timedelta(**{unit: count}) + seconds = td.seconds + 60 * 60 * 24 * td.days + + return seconds + def is_valid_endpoint(self, endpoint: dict): endpoint_type = endpoint.get("type") uri = endpoint.get("uri") diff --git a/custom_components/hpprinter/managers/ha_coordinator.py b/custom_components/hpprinter/managers/ha_coordinator.py index e28e724..baf0589 100644 --- a/custom_components/hpprinter/managers/ha_coordinator.py +++ b/custom_components/hpprinter/managers/ha_coordinator.py @@ -15,7 +15,6 @@ PRINTER_MAIN_DEVICE, SIGNAL_HA_DEVICE_CREATED, SIGNAL_HA_DEVICE_DISCOVERED, - UPDATE_API_INTERVAL, ) from .ha_config_manager import HAConfigManager from .rest_api import RestAPIv2 @@ -36,7 +35,7 @@ def __init__( hass, _LOGGER, name=config_manager.entry_title, - update_interval=UPDATE_API_INTERVAL, + update_interval=config_manager.minimum_update_interval, update_method=self._async_update_data, ) diff --git a/custom_components/hpprinter/managers/rest_api.py b/custom_components/hpprinter/managers/rest_api.py index b41b333..b86f3ce 100644 --- a/custom_components/hpprinter/managers/rest_api.py +++ b/custom_components/hpprinter/managers/rest_api.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime import json import logging import sys @@ -20,8 +20,6 @@ from homeassistant.util.ssl import SSLCipherList from ..common.consts import ( - DEFAULT_INTERVAL, - DURATION_UNITS, IGNORED_KEYS, PRODUCT_STATUS_ENDPOINT, PRODUCT_STATUS_OFFLINE_PAYLOAD, @@ -45,7 +43,6 @@ def __init__(self, hass, config_manager: HAConfigManager): self._data: dict = {} self._data_config: dict = {} self._last_update: dict[str, float] = {} - self._update_intervals: dict[str, int] = {} self._raw_data: dict = {} @@ -95,7 +92,6 @@ async def initialize(self, throw_exception: bool = False): else: self._session = async_create_clientsession(hass=self._hass) - self._load_update_intervals() await self._load_metadata() except Exception as ex: @@ -188,15 +184,9 @@ async def _update_data( self._last_update.get(endpoint, 0) if connectivity_check else 0 ) last_update_diff = int(now_ts - last_update) - interval = self._update_intervals.get(endpoint, 0) + interval = self._config_manager.get_update_interval(endpoint) if interval > last_update_diff: - _LOGGER.debug( - f"Skip updating '{endpoint}', " - f"it's too soon, " - f"interval: {interval}s, " - f"time since last update: {last_update_diff}s" - ) continue resource_data = await self._get_request(endpoint) @@ -328,13 +318,6 @@ def _get_data_section(data: dict, path: str) -> dict: return result - def _load_update_intervals(self): - for data_point in self._config_manager.data_points: - endpoint = data_point.get("endpoint") - interval = data_point.get("interval", DEFAULT_INTERVAL) - - self._update_intervals[endpoint] = self._convert_to_seconds(interval) - def _get_devices_data(self): devices = [] @@ -514,15 +497,3 @@ def device_data_changed(self, device_key: str): device_data, device_config, ) - - @staticmethod - def _convert_to_seconds(duration: str | None) -> int: - if duration is None: - duration = DEFAULT_INTERVAL - - count = int(duration[:-1]) - unit = DURATION_UNITS[duration[-1]] - td = timedelta(**{unit: count}) - seconds = td.seconds + 60 * 60 * 24 * td.days - - return seconds