فهرست منبع

Handle encoding errors in hex and base64 devices.

Rather than passing along the exception, catch it and return None as
the decoded value.

Issue #264
Jason Rumney 3 سال پیش
والد
کامیت
fff4b383b8
2فایلهای تغییر یافته به همراه23 افزوده شده و 2 حذف شده
  1. 15 2
      custom_components/tuya_local/helpers/device_config.py
  2. 8 0
      tests/devices/test_rgbcw_lightbulb.py

+ 15 - 2
custom_components/tuya_local/helpers/device_config.py

@@ -332,9 +332,22 @@ class TuyaDpsConfig:
     def decoded_value(self, device):
         v = self.get_value(device)
         if self.rawtype == "hex" and isinstance(v, str):
-            return bytes.fromhex(v)
+            try:
+                return bytes.fromhex(v)
+            except ValueError:
+                _LOGGER.warning(
+                    f"{device.name} sent invalid hex '{v}' for {self.name}",
+                )
+                return None
+
         elif self.rawtype == "base64":
-            return b64decode(v)
+            try:
+                return b64decode(v)
+            except ValueError:
+                _LOGGER.warning(
+                    f"{device.name} sent invalid base64 '{v}' for {self.name}",
+                )
+                return None
         else:
             return v
 

+ 8 - 0
tests/devices/test_rgbcw_lightbulb.py

@@ -72,6 +72,14 @@ class TestRGBCWLightbulb(BasicNumberTests, TuyaDeviceTestCase):
             (255, 255, 0, 255),
         )
 
+    # Lights have been observed to return N, O and P mixed in with the hex
+    # number.  Maybe it has some special meaning, but since it is undocumented,
+    # we just want to reject such values without an exception.
+    def test_invalid_rgbw_color(self):
+        self.dps[HSV_DPS] = "0010001000OP"
+        self.dps[BRIGHTNESS_DPS] = 1000
+        self.assertIsNone(self.subject.rgbw_color)
+
     def test_effect_list(self):
         self.assertCountEqual(
             self.subject.effect_list,