Browse Source

fix ceiling light color mode (#391)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Retha Runolfsson 1 month ago
parent
commit
3f3a7a31fd
2 changed files with 23 additions and 2 deletions
  1. 3 1
      switchbot/devices/ceiling_light.py
  2. 20 1
      tests/test_ceiling_light.py

+ 3 - 1
switchbot/devices/ceiling_light.py

@@ -37,7 +37,9 @@ class SwitchbotCeilingLight(SwitchbotSequenceBaseLight):
     @property
     def color_mode(self) -> ColorMode:
         """Return the current color mode."""
-        device_mode = CeilingLightColorMode(self._get_adv_value("color_mode") or 10)
+        device_mode = CeilingLightColorMode(
+            value if (value := self._get_adv_value("color_mode")) is not None else 10
+        )
         return _CEILING_LIGHT_COLOR_MODE_MAP.get(device_mode, ColorMode.OFF)
 
     @update_after_operation

+ 20 - 1
tests/test_ceiling_light.py

@@ -1,4 +1,4 @@
-from unittest.mock import AsyncMock, MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
 
 import pytest
 from bleak.backends.device import BLEDevice
@@ -183,3 +183,22 @@ async def test_set_brightness():
     device._send_command.assert_called_with(
         device._set_brightness_command.format("4B0FA1")
     )
+
+
+@pytest.mark.asyncio
+@pytest.mark.parametrize(
+    ("adv_value", "expected_color_mode"),
+    [
+        (0, ColorMode.COLOR_TEMP),
+        (1, ColorMode.COLOR_TEMP),
+        (4, ColorMode.EFFECT),
+        (10, ColorMode.OFF),
+        (None, ColorMode.OFF),
+    ],
+)
+async def test_get_color_mode(adv_value, expected_color_mode):
+    """Test getting color mode."""
+    device = create_device_for_command_testing()
+
+    with patch.object(device, "_get_adv_value", return_value=adv_value):
+        assert device.color_mode == expected_color_mode