Просмотр исходного кода

fix(light): avoid struct packing errors from being raised to HA

If the light returns badly formatted data, it currently raises an exception
from the struct.pack function which is not caught inside the integration,
leading to errors popping up in the HA UI.

This change catches those errors and logs them, returning None for current
hsv value, and doing nothing when asked to set dps in a way that requires
the current hsv value to be known.

Issue #6322
Jason Rumney 1 месяц назад
Родитель
Сommit
6f09a12fe1
1 измененных файлов с 70 добавлено и 57 удалено
  1. 70 57
      custom_components/tuya_local/light.py

+ 70 - 57
custom_components/tuya_local/light.py

@@ -4,6 +4,7 @@ Setup for different kinds of Tuya light devices
 
 import logging
 from struct import pack, unpack
+from struct import error as StructError
 
 import homeassistant.util.color as color_util
 from homeassistant.components.light import (
@@ -214,26 +215,35 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
             color = self._rgbhsv_dps.decoded_value(self._device)
             fmt = self._rgbhsv_dps.format
             if fmt and color:
-                vals = unpack(fmt.get("format"), color)
-                idx = 0
-                rgbhsv = {}
-                for v in vals:
-                    # HA range: s = 0-100, rgbv = 0-255, h = 0-360
-                    n = fmt["names"][idx]
-                    r = fmt["ranges"][idx]
-                    mx = r["max"]
-                    scale = 1
-                    if n == "h":
-                        scale = 360 / mx
-                    elif n == "s":
-                        scale = 100 / mx
-                    elif n in ["v", "r", "g", "b"]:
-                        scale = 255 / mx
-
-                    rgbhsv[n] = round(scale * v)
-                    idx += 1
-
-                return rgbhsv
+                try:
+                    vals = unpack(fmt.get("format"), color)
+                    idx = 0
+                    rgbhsv = {}
+                    for v in vals:
+                        # HA range: s = 0-100, rgbv = 0-255, h = 0-360
+                        n = fmt["names"][idx]
+                        r = fmt["ranges"][idx]
+                        mx = r["max"]
+                        scale = 1
+                        if n == "h":
+                            scale = 360 / mx
+                        elif n == "s":
+                            scale = 100 / mx
+                        elif n in ["v", "r", "g", "b"]:
+                            scale = 255 / mx
+
+                        rgbhsv[n] = round(scale * v)
+                        idx += 1
+
+                    return rgbhsv
+                except StructError as e:
+                    _LOGGER.warning(
+                        "%s/%s: Failed to unpack rgbhsv data: %s",
+                        self._config._device.config,
+                        self.name or "light",
+                        e,
+                    )
+
         elif self._named_color_dps:
             colour = self._named_color_dps.get_value(self._device)
             if colour:
@@ -384,44 +394,47 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                 )
 
                 current = self._unpacked_rgbhsv
-                ordered = []
-                idx = 0
-                for n in fmt["names"]:
-                    if n in rgbhsv:
-                        r = fmt["ranges"][idx]
-                        scale = 1
-                        if n == "s":
-                            scale = r["max"] / 100
-                        elif n == "h":
-                            scale = r["max"] / 360
+                if current:
+                    ordered = []
+                    idx = 0
+                    for n in fmt["names"]:
+                        if n in rgbhsv:
+                            r = fmt["ranges"][idx]
+                            scale = 1
+                            if n == "s":
+                                scale = r["max"] / 100
+                            elif n == "h":
+                                scale = r["max"] / 360
+                            else:
+                                scale = r["max"] / 255
+                            val = round(rgbhsv[n] * scale)
+                            if val < r["min"]:
+                                _LOGGER.warning(
+                                    "%s/%s: Color data %s=%d constrained to be above %d",
+                                    self._config._device.config,
+                                    self.name or "light",
+                                    n,
+                                    val,
+                                    r["min"],
+                                )
+                                val = r["min"]
                         else:
-                            scale = r["max"] / 255
-                        val = round(rgbhsv[n] * scale)
-                        if val < r["min"]:
-                            _LOGGER.warning(
-                                "%s/%s: Color data %s=%d constrained to be above %d",
-                                self._config._device.config,
-                                self.name or "light",
-                                n,
-                                val,
-                                r["min"],
-                            )
-                            val = r["min"]
-                    else:
-                        val = current[n]
-                    ordered.append(val)
-                    idx += 1
-                binary = pack(fmt["format"], *ordered)
-                encoded = self._rgbhsv_dps.encode_value(binary)
-                _LOGGER.info("%s setting color to %s", self._config.config_id, encoded)
-                settings = {
-                    **settings,
-                    **self._rgbhsv_dps.get_values_to_set(
-                        self._device,
-                        encoded,
-                        settings,
-                    ),
-                }
+                            val = current[n]
+                        ordered.append(val)
+                        idx += 1
+                    binary = pack(fmt["format"], *ordered)
+                    encoded = self._rgbhsv_dps.encode_value(binary)
+                    _LOGGER.info(
+                        "%s setting color to %s", self._config.config_id, encoded
+                    )
+                    settings = {
+                        **settings,
+                        **self._rgbhsv_dps.get_values_to_set(
+                            self._device,
+                            encoded,
+                            settings,
+                        ),
+                    }
         elif self._named_color_dps and ATTR_HS_COLOR in params:
             if self.color_mode != ColorMode.HS:
                 color_mode = ColorMode.HS