Jelajahi Sumber

light: lights with only brightness control should appear off at 0

Since we started using HA's brightness scaling functions, this has not
been working, as those functions explicitly avoid reporting a
brightness of 0, since they are not expected to be used when the light
is off.  Change the condition to avoid scaling when the value is 0.

Add a test case for this, to catch any future unintentional changes.

Issue #2589
Jason Rumney 1 tahun lalu
induk
melakukan
c6c0a76adc
2 mengubah file dengan 28 tambahan dan 1 penghapusan
  1. 1 1
      custom_components/tuya_local/light.py
  2. 27 0
      tests/test_light.py

+ 1 - 1
custom_components/tuya_local/light.py

@@ -172,7 +172,7 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
         if self._brightness_dps:
             r = self._brightness_dps.range(self._device)
             val = self._brightness_dps.get_value(self._device)
-            if r and val is not None:
+            if r and val:
                 val = color_util.value_to_brightness(r, val)
             return val
 

+ 27 - 0
tests/test_light.py

@@ -150,3 +150,30 @@ async def test_async_turn_on_with_white_param():
     light = TuyaLocalLight(mock_device, config)
     await light.async_turn_on(white=128)
     mock_device.async_set_properties.assert_called_once_with({"2": "white", "3": 506})
+
+
+@pytest.mark.asyncio
+async def test_is_off_when_off_by_brightness():
+    """Test that the light appears off when turned off by brightness."""
+    mock_device = AsyncMock()
+    mock_device.get_property = Mock()
+    dps = {"1": 0}
+    mock_device.get_property.side_effect = lambda arg: dps[arg]
+    mock_config = Mock()
+    config = TuyaEntityConfig(
+        mock_config,
+        {
+            "entity": "light",
+            "dps": [
+                {
+                    "id": "1",
+                    "name": "brightness",
+                    "type": "integer",
+                    "range": {"min": 0, "max": 100},
+                },
+            ],
+        },
+    )
+    light = TuyaLocalLight(mock_device, config)
+    assert light.is_on is False
+    assert light.brightness == 0