Skip to content

Commit

Permalink
Fix update data when printer goes online
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed Jun 20, 2024
1 parent 0ede2bd commit 4a94ad0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.0.4

- Fix update data when printer goes online [#161](https://github.com/elad-bar/ha-hpprinter/issues/161)

## 2.0.3

- Add support for `inktank` cartridge type [#162](https://github.com/elad-bar/ha-hpprinter/issues/162)
Expand Down
90 changes: 37 additions & 53 deletions custom_components/hpprinter/managers/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self, hass, config_manager: HAConfigManager):
self._loop = hass.loop
self._config_manager = config_manager
self._hass = hass
self._endpoints = self._config_manager.endpoints

self._session: ClientSession | None = None

Expand All @@ -49,9 +50,10 @@ def __init__(self, hass, config_manager: HAConfigManager):
self._is_connected: bool = False

self._device_dispatched: list[str] = []
self._all_endpoints: list[str] = []
self._support_prefetch: bool = False

self._is_printer_online: bool = False

@property
def data(self) -> dict | None:
return self._data
Expand Down Expand Up @@ -119,77 +121,61 @@ def _get_ssl_connector(self):
return connector

async def _load_metadata(self):
self._all_endpoints = []

endpoints = await self._get_request("/Prefetch?type=dtree", True)

self._support_prefetch = endpoints is not None
is_connected = self._support_prefetch
status = await self._revalidate_printer_status()

if self._support_prefetch:
for endpoint in endpoints:
is_valid = self._config_manager.is_valid_endpoint(endpoint)
if status is None:
raise IntegrationAPIError(PRODUCT_STATUS_ENDPOINT)

if is_valid:
endpoint_uri = endpoint.get("uri")

self._all_endpoints.append(endpoint_uri)
async def _revalidate_printer_status(self):
now = datetime.now()
now_ts = now.timestamp()

else:
self._all_endpoints = self._config_manager.endpoints.copy()
status_endpoint = PRODUCT_STATUS_ENDPOINT
last_update = self._last_update.get(status_endpoint, 0)

updates = await self._update_data(self._config_manager.endpoints, False)
last_update_diff = int(now_ts - last_update)
interval = self._config_manager.get_update_interval(status_endpoint)

_LOGGER.debug(f"Startup: {updates} endpoints were updated")
if interval > last_update_diff:
return None

endpoints_found = len(self._raw_data.keys())
is_connected = endpoints_found > 0
available_endpoints = len(self._all_endpoints)
product_status_data = await self._get_request(status_endpoint)
self._is_printer_online = product_status_data is not None

if is_connected:
_LOGGER.info(
"No support for prefetch endpoint, "
f"{endpoints_found}/{available_endpoints} Endpoints found"
)
else:
endpoint_urls = ", ".join(self._all_endpoints)
if self._is_printer_online:
self._last_update = {}

raise IntegrationAPIError(endpoint_urls)
return product_status_data

self._is_connected = is_connected
return None

async def update(self):
updates = await self._update_data(self._config_manager.endpoints)

_LOGGER.debug(f"Scheduled update: {updates} endpoints were updated")

async def update_full(self):
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:
product_status_data: dict | None = None
endpoints_updated = 0

if not self._is_connected and connectivity_check:
return endpoints_updated
if not self._is_printer_online:
product_status_data = await self._revalidate_printer_status()

if product_status_data is None:
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
)
for endpoint in self._endpoints:
last_update = self._last_update.get(endpoint, 0)
last_update_diff = int(now_ts - last_update)
interval = self._config_manager.get_update_interval(endpoint)

if interval > last_update_diff:
continue

resource_data = await self._get_request(endpoint)
if endpoint == PRODUCT_STATUS_ENDPOINT and product_status_data is not None:
resource_data = product_status_data

else:
resource_data = await self._get_request(endpoint)

endpoints_updated += 1

if resource_data is None:
Expand All @@ -198,15 +184,13 @@ async def _update_data(

else:
self._raw_data[endpoint] = resource_data

if connectivity_check:
self._last_update[endpoint] = now.timestamp()
self._last_update[endpoint] = now.timestamp()

devices = self._get_devices_data()

self._extract_data(devices)

return endpoints_updated
_LOGGER.debug(f"Scheduled update: {endpoints_updated} endpoints were updated")

def _extract_data(self, devices: list[dict]):
device_data = {}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.3"
"version": "2.0.4"
}

0 comments on commit 4a94ad0

Please sign in to comment.