From 53c0350d53d6b87b286251b948f11c677ded02cb Mon Sep 17 00:00:00 2001
From: Elad Bar <elad.bar@hotmail.com>
Date: Fri, 17 May 2024 15:26:54 +0300
Subject: [PATCH] Add fallback mechanism for consumables, fix hassfest error

---
 CHANGELOG.md                                  |  6 +++++
 .../hpprinter/managers/rest_api.py            | 22 +++++++++++++---
 custom_components/hpprinter/manifest.json     |  2 +-
 .../hpprinter/parameters/data_points.json     | 26 ++++++++++++++++---
 custom_components/hpprinter/sensor.py         |  3 +++
 custom_components/hpprinter/strings.json      |  2 +-
 .../hpprinter/translations/de.json            |  2 +-
 .../hpprinter/translations/dk.json            |  2 +-
 .../hpprinter/translations/en.json            |  2 +-
 .../hpprinter/translations/es.json            |  2 +-
 .../hpprinter/translations/fr.json            |  2 +-
 .../hpprinter/translations/nb.json            |  2 +-
 .../hpprinter/translations/nl.json            |  2 +-
 .../hpprinter/translations/pl.json            |  2 +-
 .../hpprinter/translations/pt-BR.json         |  2 +-
 15 files changed, 61 insertions(+), 18 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3ab7fa9..6927cd7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
 
diff --git a/custom_components/hpprinter/managers/rest_api.py b/custom_components/hpprinter/managers/rest_api.py
index eb63468..9e929c0 100644
--- a/custom_components/hpprinter/managers/rest_api.py
+++ b/custom_components/hpprinter/managers/rest_api.py
@@ -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
diff --git a/custom_components/hpprinter/manifest.json b/custom_components/hpprinter/manifest.json
index ba1a6ab..cdd80ed 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.0b8"
+  "version": "2.0.0b9"
 }
diff --git a/custom_components/hpprinter/parameters/data_points.json b/custom_components/hpprinter/parameters/data_points.json
index e87da34..cdc1043 100644
--- a/custom_components/hpprinter/parameters/data_points.json
+++ b/custom_components/hpprinter/parameters/data_points.json
@@ -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"
@@ -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",
@@ -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"
@@ -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": {
diff --git a/custom_components/hpprinter/sensor.py b/custom_components/hpprinter/sensor.py
index 6be494d..b3c0cea 100644
--- a/custom_components/hpprinter/sensor.py
+++ b/custom_components/hpprinter/sensor.py
@@ -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:
diff --git a/custom_components/hpprinter/strings.json b/custom_components/hpprinter/strings.json
index 5be1608..bac87c5 100644
--- a/custom_components/hpprinter/strings.json
+++ b/custom_components/hpprinter/strings.json
@@ -45,7 +45,7 @@
         "name": "Type",
         "state": {
           "ink": "Ink",
-          "inkCartridge": "Ink",
+          "inkcartridge": "Ink",
           "printhead": "Printhead",
           "toner": "Toner"
         }
diff --git a/custom_components/hpprinter/translations/de.json b/custom_components/hpprinter/translations/de.json
index d84ecc5..2e9dbc0 100644
--- a/custom_components/hpprinter/translations/de.json
+++ b/custom_components/hpprinter/translations/de.json
@@ -51,7 +51,7 @@
         "name": "Typ",
         "state": {
           "ink": "Tinte",
-          "inkCartridge": "Tinte",
+          "inkcartridge": "Tinte",
           "printhead": "Druckkopf",
           "toner": "Toner"
         }
diff --git a/custom_components/hpprinter/translations/dk.json b/custom_components/hpprinter/translations/dk.json
index b83c377..437c2bb 100644
--- a/custom_components/hpprinter/translations/dk.json
+++ b/custom_components/hpprinter/translations/dk.json
@@ -51,7 +51,7 @@
         "name": "Type",
         "state": {
           "ink": "Bl\u00e6k",
-          "inkCartridge": "Bl\u00e6k",
+          "inkcartridge": "Bl\u00e6k",
           "printhead": "Printhead",
           "toner": "Toner"
         }
diff --git a/custom_components/hpprinter/translations/en.json b/custom_components/hpprinter/translations/en.json
index 6ba6737..b7f3090 100644
--- a/custom_components/hpprinter/translations/en.json
+++ b/custom_components/hpprinter/translations/en.json
@@ -51,7 +51,7 @@
         "name": "Type",
         "state": {
           "ink": "Ink",
-          "inkCartridge": "Ink",
+          "inkcartridge": "Ink",
           "printhead": "Printhead",
           "toner": "Toner"
         }
diff --git a/custom_components/hpprinter/translations/es.json b/custom_components/hpprinter/translations/es.json
index a278203..536ab21 100644
--- a/custom_components/hpprinter/translations/es.json
+++ b/custom_components/hpprinter/translations/es.json
@@ -51,7 +51,7 @@
         "name": "Tipo",
         "state": {
           "ink": "Tinta",
-          "inkCartridge": "Tinta",
+          "inkcartridge": "Tinta",
           "printhead": "Cabezal",
           "toner": "Virador"
         }
diff --git a/custom_components/hpprinter/translations/fr.json b/custom_components/hpprinter/translations/fr.json
index 52ee58d..51619d3 100644
--- a/custom_components/hpprinter/translations/fr.json
+++ b/custom_components/hpprinter/translations/fr.json
@@ -51,7 +51,7 @@
         "name": "Taper",
         "state": {
           "ink": "Encre",
-          "inkCartridge": "Encre",
+          "inkcartridge": "Encre",
           "printhead": "T\u00eate d'impression",
           "toner": "Toner"
         }
diff --git a/custom_components/hpprinter/translations/nb.json b/custom_components/hpprinter/translations/nb.json
index 5fdb47f..9da6742 100644
--- a/custom_components/hpprinter/translations/nb.json
+++ b/custom_components/hpprinter/translations/nb.json
@@ -51,7 +51,7 @@
         "name": "Type",
         "state": {
           "ink": "Blekk",
-          "inkCartridge": "Blekk",
+          "inkcartridge": "Blekk",
           "printhead": "Skrivehode",
           "toner": "Toner"
         }
diff --git a/custom_components/hpprinter/translations/nl.json b/custom_components/hpprinter/translations/nl.json
index 8539ee4..3697915 100644
--- a/custom_components/hpprinter/translations/nl.json
+++ b/custom_components/hpprinter/translations/nl.json
@@ -51,7 +51,7 @@
         "name": "Type",
         "state": {
           "ink": "Inkt",
-          "inkCartridge": "Inkt",
+          "inkcartridge": "Inkt",
           "printhead": "Printhead",
           "toner": "Toner"
         }
diff --git a/custom_components/hpprinter/translations/pl.json b/custom_components/hpprinter/translations/pl.json
index be8d1ee..f184312 100644
--- a/custom_components/hpprinter/translations/pl.json
+++ b/custom_components/hpprinter/translations/pl.json
@@ -51,7 +51,7 @@
         "name": "Typ",
         "state": {
           "ink": "Atrament",
-          "inkCartridge": "Atrament",
+          "inkcartridge": "Atrament",
           "printhead": "Printhead",
           "toner": "Toner"
         }
diff --git a/custom_components/hpprinter/translations/pt-BR.json b/custom_components/hpprinter/translations/pt-BR.json
index 482dcd3..5a8db2e 100644
--- a/custom_components/hpprinter/translations/pt-BR.json
+++ b/custom_components/hpprinter/translations/pt-BR.json
@@ -51,7 +51,7 @@
         "name": "Tipo",
         "state": {
           "ink": "Tinta",
-          "inkCartridge": "Tinta",
+          "inkcartridge": "Tinta",
           "printhead": "Cabe\u00e7ote de impress\u00e3o",
           "toner": "Toner"
         }