Prechádzať zdrojové kódy

tests(light): add tests for invalid format rgbhsv fields

Jason Rumney 19 hodín pred
rodič
commit
105fc9328f
2 zmenil súbory, kde vykonal 46 pridanie a 1 odobranie
  1. 1 1
      custom_components/tuya_local/light.py
  2. 45 0
      tests/test_light.py

+ 1 - 1
custom_components/tuya_local/light.py

@@ -3,8 +3,8 @@ Setup for different kinds of Tuya light devices
 """
 """
 
 
 import logging
 import logging
-from struct import pack, unpack
 from struct import error as StructError
 from struct import error as StructError
+from struct import pack, unpack
 
 
 import homeassistant.util.color as color_util
 import homeassistant.util.color as color_util
 from homeassistant.components.light import (
 from homeassistant.components.light import (

+ 45 - 0
tests/test_light.py

@@ -358,3 +358,48 @@ async def test_is_off_when_off_by_brightness():
     light = TuyaLocalLight(mock_device, config)
     light = TuyaLocalLight(mock_device, config)
     assert light.is_on is False
     assert light.is_on is False
     assert light.brightness == 0
     assert light.brightness == 0
+
+
+@pytest.mark.parametrize(
+    ("rawtype", "invalid_values", "valid_value"),
+    [
+        ("hex", ("ff  00", "ff 0 0"), "ff0000"),
+        ("base64", ("a",), "/wAA"),
+    ],
+)
+@pytest.mark.asyncio
+async def test_rgbhsv_invalid_colour_does_not_break_state_or_turn_on(
+    rawtype, invalid_values, valid_value
+):
+    """Ignore malformed colour data without losing the switch"""
+    mock_device = AsyncMock()
+    mock_device.get_property = Mock()
+    dps = {"1": True, "2": valid_value}
+    mock_device.get_property.side_effect = lambda arg: dps[arg]
+    config = TuyaEntityConfig(
+        Mock(),
+        {
+            "entity": "light",
+            "dps": [
+                {"id": "1", "name": "switch", "type": "boolean"},
+                {
+                    "id": "2",
+                    "name": "rgbhsv",
+                    "type": rawtype,
+                    "format": [{"name": name, "bytes": 1} for name in ("r", "g", "b")],
+                },
+            ],
+        },
+    )
+    light = TuyaLocalLight(mock_device, config)
+
+    assert light.hs_color == (0.0, 100.0)
+    for invalid_value in invalid_values:
+        dps["2"] = invalid_value
+        assert light.hs_color is None
+        assert light.is_on is True
+        await light.async_turn_on(hs_color=(0, 100), brightness=255)
+        mock_device.async_set_properties.assert_not_called()
+        dps["1"] = False
+        await light.async_turn_on(hs_color=(0, 100), brightness=255)
+        mock_device.async_set_properties.assert_called_once_with({"1": True})