Skip to content

Commit

Permalink
Merge pull request #151 from elad-bar/re-add-status-entity
Browse files Browse the repository at this point in the history
Re add status entity
  • Loading branch information
elad-bar authored Jun 15, 2024
2 parents eaaa195 + e3b7d68 commit a02e5e8
Show file tree
Hide file tree
Showing 21 changed files with 259 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

## 2.0.1

- Set printer status to `Off` when printer is offline, instead of reset data
- Add status sensor for main device
- Add sensor statistics attribute
- measurement - level, remaining level
- total_increasing - pages, refills
- Add device hostname to unique ID
- Add support for `tonercartridge` cartridge type
- Add translations for Russian, Ukrainian - Using Google Translate
- Add translations for Greek [PR#142](https://github.com/elad-bar/ha-hpprinter/pull/142) by [@ChriZathens](https://github.com/ChriZathens)
- Improved Dutch translations [PR#118](https://github.com/elad-bar/ha-hpprinter/pull/118) by [@hmmbob](https://github.com/hmmbob)
- Improved German translations [PR#130](https://github.com/elad-bar/ha-hpprinter/pull/130) by [@SukramJ](https://github.com/SukramJ)
- Fix Product Status URL for all translations (For error 404)

## 2.0.0
Expand Down
15 changes: 11 additions & 4 deletions custom_components/hpprinter/common/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

PRINTER_STATUS = {
"ready": "On",
"scanProcessing": "Scanning",
"scanprocessing": "Scanning",
"copying": "Copying",
"processing": "Printing",
"cancelJob": "Cancelling Job",
"inPowerSave": "Idle",
"": "Off",
"canceljob": "Cancelling Job",
"inpowersave": "Idle",
"off": "Off",
}

PRINTER_MAIN_DEVICE = "Main"

IGNORED_KEYS = ["@schemaLocation", "Version"]

SIGNAL_HA_DEVICE_CREATED = f"signal_{DOMAIN}_device_created"
Expand All @@ -46,3 +48,8 @@
UNIT_OF_MEASUREMENT_REFILLS = "refills"

NUMERIC_UNITS_OF_MEASUREMENT = [UNIT_OF_MEASUREMENT_PAGES, UNIT_OF_MEASUREMENT_REFILLS]

PRODUCT_STATUS_ENDPOINT = "/DevMgmt/ProductStatusDyn.xml"
PRODUCT_STATUS_OFFLINE_PAYLOAD = {
"ProductStatusDyn": {"Status": [{"StatusCategory": "off"}]}
}
2 changes: 2 additions & 0 deletions custom_components/hpprinter/managers/ha_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def _load_entity_descriptions(self):
self._entity_descriptions.append(entity_description)

elif property_platform == str(Platform.SENSOR):
state_class = property_data.get("state_class")
unit_of_measurement = property_data.get("unit_of_measurement")
options = property_data.get("options")

Expand All @@ -323,6 +324,7 @@ def _load_entity_descriptions(self):
icon=icon,
translation_key=translation_key,
options=options,
state_class=state_class,
)

self._entity_descriptions.append(entity_description)
Expand Down
3 changes: 2 additions & 1 deletion custom_components/hpprinter/managers/ha_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from ..common.consts import (
DOMAIN,
PRINTER_MAIN_DEVICE,
SIGNAL_HA_DEVICE_CREATED,
SIGNAL_HA_DEVICE_DISCOVERED,
)
Expand Down Expand Up @@ -46,7 +47,7 @@ def __init__(
self._main_device_id: str | None = None

self._device_handlers = {
"Main": self.create_main_device,
PRINTER_MAIN_DEVICE: self.create_main_device,
"Consumable": self.create_consumable_device,
"Adapter": self.create_adapter_device,
}
Expand Down
36 changes: 31 additions & 5 deletions custom_components/hpprinter/managers/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
from homeassistant.util import slugify, ssl
from homeassistant.util.ssl import SSLCipherList

from ..common.consts import IGNORED_KEYS, SIGNAL_HA_DEVICE_DISCOVERED
from ..common.consts import (
IGNORED_KEYS,
PRODUCT_STATUS_ENDPOINT,
PRODUCT_STATUS_OFFLINE_PAYLOAD,
SIGNAL_HA_DEVICE_DISCOVERED,
)
from ..models.config_data import ConfigData
from ..models.exceptions import IntegrationAPIError, IntegrationParameterError
from .ha_config_manager import HAConfigManager
Expand Down Expand Up @@ -160,7 +165,12 @@ async def _update_data(self, endpoints: list[str], connectivity_check: bool = Tr
for endpoint in endpoints:
resource_data = await self._get_request(endpoint)

self._raw_data[endpoint] = resource_data
if resource_data is None:
if endpoint == PRODUCT_STATUS_ENDPOINT:
self._raw_data[endpoint] = PRODUCT_STATUS_OFFLINE_PAYLOAD

else:
self._raw_data[endpoint] = resource_data

devices = self._get_devices_data()

Expand Down Expand Up @@ -213,6 +223,7 @@ def _extract_data(self, devices: list[dict]):
device_key = f"{device_type}.{device_id}"

data = device_data[device_key] if device_key in device_data else {}

has_data = len(list(item_data.keys())) > 0
data.update(item_data)

Expand Down Expand Up @@ -315,11 +326,26 @@ 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

if property_accept is not None:
for property_accept_key in property_accept:
property_accept_data = property_accept[property_accept_key]

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}"
)

value = data_item_flat.get(property_path)
if is_valid:
value = data_item_flat.get(property_path)

if value is not None:
device_data[property_key] = value
if value is not None:
device_data[property_key] = value

data = {"config": device_config, "data": device_data}

Expand Down
55 changes: 54 additions & 1 deletion custom_components/hpprinter/parameters/data_points.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@
"path": "ConsumableTypeEnum",
"platform": "sensor",
"device_class": "enum",
"options": ["ink", "inkcartridge", "printhead", "toner"]
"options": [
"ink",
"inkcartridge",
"printhead",
"toner",
"tonercartridge"
]
},
"installation_date": {
"path": "Installation.Date",
Expand All @@ -73,6 +79,7 @@
"path": "ConsumablePercentageLevelRemaining",
"platform": "sensor",
"unit_of_measurement": "%",
"state_class": "measurement",
"exclude": {
"consumable_type_enum": "printhead"
}
Expand Down Expand Up @@ -136,6 +143,7 @@
"path": "EstimatedPagesRemaining",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "measurement",
"exclude": {
"consumable_type_enum": "printhead"
}
Expand All @@ -153,12 +161,14 @@
"path": "RefilledCount.CounterfeitRefilledCount.#text",
"platform": "sensor",
"unit_of_measurement": "refills",
"state_class": "total_increasing",
"icon": "mdi:format-color-fill"
},
"refilled_count_genuine_refilled_count": {
"path": "RefilledCount.GenuineRefilledCount",
"platform": "sensor",
"unit_of_measurement": "refills",
"state_class": "total_increasing",
"icon": "mdi:format-color-fill"
}
}
Expand All @@ -173,42 +183,49 @@
"path": "TotalImpressions.#text",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:file-document-check"
},
"monochrome_impressions": {
"path": "MonochromeImpressions",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:file-document-check"
},
"color_impressions": {
"path": "ColorImpressions",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:file-document-check"
},
"simplex_sheets": {
"path": "SimplexSheets",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:file-document-check"
},
"duplex_sheets": {
"path": "DuplexSheets.#text",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:file-document-multiple"
},
"jam_events": {
"path": "JamEvents.#text",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:file-document-remove"
},
"mispick_events": {
"path": "MispickEvents",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:file-document-minus"
}
}
Expand All @@ -223,36 +240,42 @@
"path": "ScanImages.#text",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:credit-card-scan"
},
"adf_images": {
"path": "AdfImages.#text",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:credit-card-scan"
},
"duplex_sheets": {
"path": "DuplexSheets.#text",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:credit-card-scan"
},
"flatbed_images": {
"path": "FlatbedImages",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:credit-card-scan"
},
"jam_events": {
"path": "JamEvents",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:credit-card-scan"
},
"mispick_events": {
"path": "MispickEvents",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:credit-card-scan"
}
}
Expand All @@ -267,30 +290,35 @@
"path": "TotalImpressions.#text",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:content-copy"
},
"adf_images": {
"path": "AdfImages",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:content-copy"
},
"flatbed_images": {
"path": "FlatbedImages",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:content-copy"
},
"monochrome_impressions": {
"path": "MonochromeImpressions",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:content-copy"
},
"color_impressions": {
"path": "ColorImpressions",
"platform": "sensor",
"unit_of_measurement": "pages",
"state_class": "total_increasing",
"icon": "mdi:content-copy"
}
}
Expand Down Expand Up @@ -371,5 +399,30 @@
"path": "ConnectionMethod"
}
}
},
{
"name": "Status",
"endpoint": "/DevMgmt/ProductStatusDyn.xml",
"path": "ProductStatusDyn.Status",
"device_type": "Main",
"properties": {
"device_status": {
"path": "StatusCategory",
"platform": "sensor",
"device_class": "enum",
"options": [
"off",
"ready",
"scanprocessing",
"copying",
"processing",
"canceljob",
"inpowersave"
],
"accept": {
"LocString": null
}
}
}
}
]
1 change: 1 addition & 0 deletions custom_components/hpprinter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
):
super().__init__(entity_description, coordinator, device_key)

self._attr_state_class = entity_description.state_class
self._attr_device_class = entity_description.device_class
self._attr_native_unit_of_measurement = (
entity_description.native_unit_of_measurement
Expand Down
12 changes: 12 additions & 0 deletions custom_components/hpprinter/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@
},
"scan_images": {
"name": "Total pages"
},
"device_status": {
"name": "Status",
"state": {
"ready": "On",
"scanprocessing": "Scanning",
"copying": "Copying",
"processing": "Printing",
"canceljob": "Cancelling Job",
"inpowersave": "Idle",
"off": "Off"
}
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions custom_components/hpprinter/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@
"tonercartridge": "Toner"
}
},
"device_status": {
"name": "Status",
"state": {
"off": "Aus",
"canceljob": "Aufgabe stornieren",
"copying": "Kopieren",
"inpowersave": "Leerlauf",
"processing": "Drucken",
"ready": "An",
"scanprocessing": "Scannen"
}
},
"duplex_sheets": {
"name": "Gesamt zweiseitige Seiten"
},
Expand Down
Loading

0 comments on commit a02e5e8

Please sign in to comment.