Skip to content

Commit

Permalink
Add fallback mechanism for consumables, fix hassfest error
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed May 17, 2024
1 parent d3c1f7b commit 53c0350
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Changelog

## 2.0.0b9

- Add fallback mechanism for consumables, if station is not available, will use color mapping
- Fix hassfest failure caused by invalid enums values for translation

## 2.0.0b8

- Fix async dispatcher send
- Change all sensors with date device class to timestamp [#127](https://github.com/elad-bar/ha-hpprinter/issues/127)
- Add fallback mechanism for consumables, if station is not available, will use color mapping

## 2.0.0b7

Expand Down
22 changes: 19 additions & 3 deletions custom_components/hpprinter/managers/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,34 @@ def _extract_data(self, devices: list[dict]):
device_key = device_type

if identifier is not None:
device_id = item_data.get(identifier)
identifier_key = identifier.get("key")

device_id = item_data.get(identifier_key)

if device_id is None:
fallback = identifier.get("fallback")
if fallback is not None:
fallback_key = fallback.get("key")
fallback_mapping = fallback.get("mapping")

fallback_key_data = item_data.get(fallback_key)
device_id = (
fallback_key_data
if fallback_mapping is None
else fallback_mapping.get(fallback_key_data)
)

if flat:
new_items_data = {
slugify(f"{device_id}_{key}"): item_data[key]
for key in item_data
if key != identifier
if key != identifier_key
}

new_properties = {
slugify(f"{device_id}_{key}"): properties[key]
for key in properties
if key != identifier
if key != identifier_key
}

item_data = new_items_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.0b8"
"version": "2.0.0b9"
}
26 changes: 22 additions & 4 deletions custom_components/hpprinter/parameters/data_points.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
"endpoint": "/DevMgmt/ConsumableConfigDyn.xml",
"path": "ConsumableConfigDyn.ConsumableInfo",
"device_type": "Consumable",
"identifier": "consumable_station",
"identifier": {
"key": "consumable_station",
"fallback": {
"key": "consumable_label_code"
}
},
"properties": {
"consumable_label_code": {
"path": "ConsumableLabelCode"
Expand All @@ -57,7 +62,7 @@
"path": "ConsumableTypeEnum",
"platform": "sensor",
"device_class": "enum",
"options": ["ink", "inkCartridge", "printhead", "toner"]
"options": ["ink", "inkcartridge", "printhead", "toner"]
},
"installation_date": {
"path": "Installation.Date",
Expand Down Expand Up @@ -113,7 +118,18 @@
"endpoint": "/DevMgmt/ProductUsageDyn.xml",
"path": "ProductUsageDyn.ConsumableSubunit.Consumable",
"device_type": "Consumable",
"identifier": "consumable_station",
"identifier": {
"key": "consumable_station",
"fallback": {
"key": "marker_color",
"mapping": {
"Cyan": "C",
"Yellow": "Y",
"Magenta": "M",
"Black": "K"
}
}
},
"properties": {
"consumable_station": {
"path": "ConsumableStation"
Expand Down Expand Up @@ -303,7 +319,9 @@
"endpoint": "/IoMgmt/Adapters",
"path": "Adapters.Adapter",
"device_type": "Main",
"identifier": "hardware_config_name",
"identifier": {
"key": "hardware_config_name"
},
"flat": true,
"properties": {
"hardware_config_name": {
Expand Down
3 changes: 3 additions & 0 deletions custom_components/hpprinter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def _set_value(self):
ts = datetime.fromisoformat(state).timestamp()
state = datetime.fromtimestamp(ts, tz=tz)

elif self.device_class == SensorDeviceClass.ENUM:
state = state.lower()

self._attr_native_value = state

def _handle_coordinator_update(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"name": "Type",
"state": {
"ink": "Ink",
"inkCartridge": "Ink",
"inkcartridge": "Ink",
"printhead": "Printhead",
"toner": "Toner"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "Typ",
"state": {
"ink": "Tinte",
"inkCartridge": "Tinte",
"inkcartridge": "Tinte",
"printhead": "Druckkopf",
"toner": "Toner"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/translations/dk.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "Type",
"state": {
"ink": "Bl\u00e6k",
"inkCartridge": "Bl\u00e6k",
"inkcartridge": "Bl\u00e6k",
"printhead": "Printhead",
"toner": "Toner"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "Type",
"state": {
"ink": "Ink",
"inkCartridge": "Ink",
"inkcartridge": "Ink",
"printhead": "Printhead",
"toner": "Toner"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "Tipo",
"state": {
"ink": "Tinta",
"inkCartridge": "Tinta",
"inkcartridge": "Tinta",
"printhead": "Cabezal",
"toner": "Virador"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "Taper",
"state": {
"ink": "Encre",
"inkCartridge": "Encre",
"inkcartridge": "Encre",
"printhead": "T\u00eate d'impression",
"toner": "Toner"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/translations/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "Type",
"state": {
"ink": "Blekk",
"inkCartridge": "Blekk",
"inkcartridge": "Blekk",
"printhead": "Skrivehode",
"toner": "Toner"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "Type",
"state": {
"ink": "Inkt",
"inkCartridge": "Inkt",
"inkcartridge": "Inkt",
"printhead": "Printhead",
"toner": "Toner"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "Typ",
"state": {
"ink": "Atrament",
"inkCartridge": "Atrament",
"inkcartridge": "Atrament",
"printhead": "Printhead",
"toner": "Toner"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "Tipo",
"state": {
"ink": "Tinta",
"inkCartridge": "Tinta",
"inkcartridge": "Tinta",
"printhead": "Cabe\u00e7ote de impress\u00e3o",
"toner": "Toner"
}
Expand Down

0 comments on commit 53c0350

Please sign in to comment.