Skip to content

Commit

Permalink
Merge pull request #154 from elad-bar/bug-fixes-v2-0-1
Browse files Browse the repository at this point in the history
bug fixes
  • Loading branch information
elad-bar authored Jun 16, 2024
2 parents a02e5e8 + f3e04ee commit 8595315
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 76 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 1 addition & 5 deletions custom_components/hpprinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions custom_components/hpprinter/common/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
78 changes: 53 additions & 25 deletions custom_components/hpprinter/managers/ha_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
from homeassistant.helpers.storage import Store

from ..common.consts import (
CONF_UPDATE_INTERVAL,
CONFIGURATION_FILE,
DEFAULT_ENTRY_ID,
DEFAULT_INTERVAL,
DEFAULT_NAME,
DOMAIN,
DURATION_UNITS,
)
from ..common.entity_descriptions import (
IntegrationBinarySensorEntityDescription,
Expand All @@ -39,22 +40,33 @@ 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

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._data_points: dict | None = None
self._exclude_uri_list: list[str] | None = None
self._exclude_type_list: list[str] | None = None
self._default_update_interval = self._convert_to_seconds(DEFAULT_INTERVAL)

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
Expand Down Expand Up @@ -88,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
Expand All @@ -100,13 +116,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
Expand Down Expand Up @@ -191,13 +200,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()

Expand Down Expand Up @@ -233,7 +235,7 @@ async def _load_config_from_file(self):

@staticmethod
def _get_defaults() -> dict:
data = {CONF_UPDATE_INTERVAL: 5}
data = {}

return data

Expand Down Expand Up @@ -379,27 +381,41 @@ 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)

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"
Expand All @@ -415,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")
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/managers/ha_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
hass,
_LOGGER,
name=config_manager.entry_title,
update_interval=config_manager.update_interval,
update_interval=config_manager.minimum_update_interval,
update_method=self._async_update_data,
)

Expand Down
Loading

0 comments on commit 8595315

Please sign in to comment.