Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion switchbot/devices/ceiling_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
39 changes: 39 additions & 0 deletions tests/test_ceiling_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
Loading