浏览代码

Return the snake_case style for led effects (#362)

Co-authored-by: J. Nick Koston <nick@koston.org>
Retha Runolfsson 3 天之前
父节点
当前提交
fc26e86fd7
共有 5 个文件被更改,包括 95 次插入29 次删除
  1. 2 2
      switchbot/devices/base_light.py
  2. 3 3
      switchbot/devices/bulb.py
  3. 16 16
      switchbot/devices/light_strip.py
  4. 31 4
      tests/test_bulb.py
  5. 43 4
      tests/test_strip_light.py

+ 2 - 2
switchbot/devices/base_light.py

@@ -65,7 +65,7 @@ class SwitchbotBaseLight(SwitchbotDevice):
     @property
     def get_effect_list(self) -> list[str] | None:
         """Return the list of supported effects."""
-        return list(self._effect_dict.keys()) if self._effect_dict else None
+        return list(self._effect_dict) if self._effect_dict else None
 
     def is_on(self) -> bool | None:
         """Return bulb state from cache."""
@@ -111,7 +111,7 @@ class SwitchbotBaseLight(SwitchbotDevice):
     @update_after_operation
     async def set_effect(self, effect: str) -> bool:
         """Set effect."""
-        effect_template = self._effect_dict.get(effect)
+        effect_template = self._effect_dict.get(effect.lower())
         if not effect_template:
             raise SwitchbotOperationError(f"Effect {effect} not supported")
         result = await self._send_multiple_commands(effect_template)

+ 3 - 3
switchbot/devices/bulb.py

@@ -25,9 +25,9 @@ class SwitchbotBulb(SwitchbotSequenceBaseLight):
     _set_brightness_command = f"{COLOR_BULB_CONTROL_HEADER}14{{}}"
     _get_basic_info_command = ["570003", "570f4801"]
     _effect_dict = {
-        "Colorful": ["570F4701010300"],
-        "Flickering": ["570F4701010301"],
-        "Breathing": ["570F4701010302"],
+        "colorful": ["570F4701010300"],
+        "flickering": ["570F4701010301"],
+        "breathing": ["570F4701010302"],
     }
 
     @property

+ 16 - 16
switchbot/devices/light_strip.py

@@ -20,78 +20,78 @@ _STRIP_LIGHT_COLOR_MODE_MAP = {
 }
 LIGHT_STRIP_CONTROL_HEADER = "570F4901"
 COMMON_EFFECTS = {
-    "Christmas": [
+    "christmas": [
         "570F49070200033C01",
         "570F490701000600009902006D0EFF0021",
         "570F490701000603009902006D0EFF0021",
     ],
-    "Halloween": ["570F49070200053C04", "570F490701000300FF6A009E00ED00EA0F"],
-    "Sunset": [
+    "halloween": ["570F49070200053C04", "570F490701000300FF6A009E00ED00EA0F"],
+    "sunset": [
         "570F49070200033C3C",
         "570F490701000900FF9000ED8C04DD5800",
         "570F490701000903FF2E008E0B004F0500",
         "570F4907010009063F0010270056140033",
     ],
-    "Vitality": [
+    "vitality": [
         "570F49070200053C02",
         "570F490701000600C5003FD9530AEC9800",
         "570F490701000603FFDF0000895500468B",
     ],
-    "Flashing": [
+    "flashing": [
         "570F49070200053C02",
         "570F4907010006000000FF00FF00FF0000",
         "570F490701000603FFFF0000FFFFA020F0",
     ],
-    "Strobe": ["570F49070200043C02", "570F490701000300FF00E19D70FFFF0515"],
-    "Fade": [
+    "strobe": ["570F49070200043C02", "570F490701000300FF00E19D70FFFF0515"],
+    "fade": [
         "570F49070200043C04",
         "570F490701000500FF5481FF00E19D70FF",
         "570F490701000503FF0515FF7FEB",
     ],
-    "Smooth": [
+    "smooth": [
         "570F49070200033C02",
         "570F4907010007000036FC00F6FF00ED13",
         "570F490701000703F6FF00FF8300FF0800",
         "570F490701000706FF00E1",
     ],
-    "Forest": [
+    "forest": [
         "570F49070200033C06",
         "570F490701000400006400228B223CB371",
         "570F49070100040390EE90",
     ],
-    "Ocean": [
+    "ocean": [
         "570F49070200033C06",
         "570F4907010007004400FF0061FF007BFF",
         "570F490701000703009DFF00B2FF00CBFF",
         "570F49070100070600E9FF",
     ],
-    "Autumn": [
+    "autumn": [
         "570F49070200043C05",
         "570F490701000700D10035922D13A16501",
         "570F490701000703AB9100DD8C00F4AA29",
         "570F490701000706E8D000",
     ],
-    "Cool": [
+    "cool": [
         "570F49070200043C04",
         "570F490701000600001A63006C9A00468B",
         "570F490701000603009DA50089BE4378B6",
     ],
-    "Flow": [
+    "flow": [
         "570F49070200033C02",
         "570F490701000600FF00D8E100FFAA00FF",
         "570F4907010006037F00FF5000FF1900FF",
     ],
-    "Relax": [
+    "relax": [
         "570F49070200033C03",
         "570F490701000400FF8C00FF7200FF1D00",
         "570F490701000403FF5500",
     ],
-    "Modern": [
+    "modern": [
         "570F49070200043C03",
         "570F49070100060089231A5F8969829E5A",
         "570F490701000603BCB05EEDBE5AFF9D60",
     ],
-    "Rose": [
+    "rose": [
         "570F49070200043C04",
         "570F490701000500FF1969BC215F7C0225",
         "570F490701000503600C2B35040C",

+ 31 - 4
tests/test_bulb.py

@@ -72,7 +72,7 @@ async def test_default_info():
     assert device.brightness == 1
     assert device.min_temp == 2700
     assert device.max_temp == 6500
-    assert device.get_effect_list == list(device._effect_dict.keys())
+    assert device.get_effect_list == ["colorful", "flickering", "breathing"]
 
 
 @pytest.mark.asyncio
@@ -217,8 +217,35 @@ async def test_set_effect_with_valid_effect():
     """Test setting a valid effect."""
     device = create_device_for_command_testing()
 
-    await device.set_effect("Colorful")
+    await device.set_effect("colorful")
 
-    device._send_command.assert_called_with(device._effect_dict["Colorful"][0])
+    device._send_command.assert_called_with(device._effect_dict["colorful"][0])
 
-    assert device.get_effect() == "Colorful"
+    assert device.get_effect() == "colorful"
+
+
+def test_effect_list_contains_lowercase_names():
+    """Test that all effect names in get_effect_list are lowercase."""
+    ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
+    device = bulb.SwitchbotBulb(ble_device)
+    effect_list = device.get_effect_list
+
+    assert effect_list is not None, "Effect list should not be None"
+    assert effect_list == ["colorful", "flickering", "breathing"]
+    for effect_name in effect_list:
+        assert effect_name.islower(), f"Effect name '{effect_name}' is not lowercase"
+
+
+@pytest.mark.asyncio
+async def test_set_effect_normalizes_case():
+    """Test that set_effect normalizes effect names to lowercase."""
+    device = create_device_for_command_testing()
+
+    # Test various case combinations
+    test_cases = ["COLORFUL", "Colorful", "CoLoRfUl", "colorful"]
+
+    for test_effect in test_cases:
+        await device.set_effect(test_effect)
+        # Should always work regardless of case
+        device._send_command.assert_called()
+        assert device.get_effect() == test_effect  # Stored as provided

+ 43 - 4
tests/test_strip_light.py

@@ -77,7 +77,14 @@ async def test_default_info():
     assert device.brightness == 30
     assert device.min_temp == 2700
     assert device.max_temp == 6500
-    assert device.get_effect_list == list(device._effect_dict.keys())
+    # Check that effect list contains expected lowercase effect names
+    effect_list = device.get_effect_list
+    assert effect_list is not None
+    assert all(effect.islower() for effect in effect_list)
+    # Verify some known effects are present
+    assert "christmas" in effect_list
+    assert "halloween" in effect_list
+    assert "sunset" in effect_list
 
 
 @pytest.mark.asyncio
@@ -223,11 +230,43 @@ async def test_set_effect_with_valid_effect():
     device = create_device_for_command_testing()
     device._send_multiple_commands = AsyncMock()
 
-    await device.set_effect("Christmas")
+    await device.set_effect("christmas")
 
-    device._send_multiple_commands.assert_called_with(device._effect_dict["Christmas"])
+    device._send_multiple_commands.assert_called_with(device._effect_dict["christmas"])
 
-    assert device.get_effect() == "Christmas"
+    assert device.get_effect() == "christmas"
+
+
+def test_effect_list_contains_lowercase_names():
+    """Test that all effect names in get_effect_list are lowercase."""
+    ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
+    device = light_strip.SwitchbotLightStrip(ble_device)
+    effect_list = device.get_effect_list
+
+    assert effect_list is not None, "Effect list should not be None"
+    # All effect names should be lowercase
+    for effect_name in effect_list:
+        assert effect_name.islower(), f"Effect name '{effect_name}' is not lowercase"
+    # Verify some known effects are present
+    assert "christmas" in effect_list
+    assert "halloween" in effect_list
+    assert "sunset" in effect_list
+
+
+@pytest.mark.asyncio
+async def test_set_effect_normalizes_case():
+    """Test that set_effect normalizes effect names to lowercase."""
+    device = create_device_for_command_testing()
+    device._send_multiple_commands = AsyncMock()
+
+    # Test various case combinations
+    test_cases = ["CHRISTMAS", "Christmas", "ChRiStMaS", "christmas"]
+
+    for test_effect in test_cases:
+        await device.set_effect(test_effect)
+        # Should always work regardless of case
+        device._send_multiple_commands.assert_called()
+        assert device.get_effect() == test_effect  # Stored as provided
 
 
 @pytest.mark.asyncio