From f5f6880e37dd4a3f5b00fab273b6ccd0aacc9750 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Wed, 26 Aug 2026 14:22:35 +0900 Subject: [PATCH] fix(ceiling-light): ignore invalid color temperature --- switchbot/devices/ceiling_light.py | 6 ++++- tests/test_ceiling_light.py | 39 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/switchbot/devices/ceiling_light.py b/switchbot/devices/ceiling_light.py index 07c291ef..26a45437 100644 --- a/switchbot/devices/ceiling_light.py +++ b/switchbot/devices/ceiling_light.py @@ -60,7 +60,11 @@ async def get_basic_info(self) -> dict[str, Any] | None: return None _version_info, _data = res - self._state["cw"] = int.from_bytes(_data[3:5], "big") + color_temp = int.from_bytes(_data[3:5], "big") + if self.min_temp <= color_temp <= self.max_temp: + self._state["cw"] = color_temp + else: + self._state.setdefault("cw", DEFAULT_COLOR_TEMP) return { "isOn": bool(_data[1] & 0b10000000), diff --git a/tests/test_ceiling_light.py b/tests/test_ceiling_light.py index b3416a9a..378f42d8 100644 --- a/tests/test_ceiling_light.py +++ b/tests/test_ceiling_light.py @@ -135,6 +135,45 @@ async def test_get_basic_info(info_data, result): assert info["firmware"] == result[4] +@pytest.mark.asyncio +async def test_get_basic_info_ignores_invalid_color_temp() -> None: + """Test retaining the last color temp when basic info reports a placeholder.""" + device = create_device_for_command_testing() + device._state["cw"] = 3625 + device._send_command = AsyncMock( + side_effect=[ + b"\x01d\x18\x0e\x00\x00\x00\x00\x00\x00\x00\x0c\x01", + b"\x01\x80F\xff\x00\x01\x00", + ] + ) + device._check_command_result = MagicMock(side_effect=[True, True]) + + info = await device.get_basic_info() + + assert info is not None + assert info["cw"] == 3625 + assert device.color_temp == 3625 + + +@pytest.mark.asyncio +async def test_get_basic_info_uses_default_for_initial_invalid_color_temp() -> None: + """Test using the default color temp for an initial placeholder.""" + device = create_device_for_command_testing() + device._send_command = AsyncMock( + side_effect=[ + b"\x01d\x18\x0e\x00\x00\x00\x00\x00\x00\x00\x0c\x01", + b"\x01\x80F\xff\x00\x01\x00", + ] + ) + device._check_command_result = MagicMock(side_effect=[True, True]) + + info = await device.get_basic_info() + + assert info is not None + assert info["cw"] == 4001 + assert device.color_temp == 4001 + + @pytest.mark.asyncio async def test_set_color_temp(): """Test setting color temperature."""