test_strip_light.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. from unittest.mock import AsyncMock, MagicMock, patch
  2. import pytest
  3. from bleak.backends.device import BLEDevice
  4. from switchbot import SwitchBotAdvertisement, SwitchbotModel
  5. from switchbot.const.light import ColorMode
  6. from switchbot.devices import bulb, ceiling_light, light_strip
  7. from switchbot.devices.base_light import SwitchbotBaseLight
  8. from switchbot.devices.device import SwitchbotEncryptedDevice, SwitchbotOperationError
  9. from . import (
  10. CANDLE_WARMER_LAMP_INFO,
  11. FLOOR_LAMP_INFO,
  12. PERMANENT_OUTDOOR_LIGHT_INFO,
  13. RGBIC_NEON_LIGHT_INFO,
  14. RGBICWW_CEILING_LIGHT_INFO,
  15. RGBICWW_FLOOR_LAMP_INFO,
  16. RGBICWW_STRIP_LIGHT_INFO,
  17. STRIP_LIGHT_3_INFO,
  18. )
  19. from .test_adv_parser import AdvTestCase, generate_ble_device
  20. ALL_LIGHT_CASES = [
  21. (STRIP_LIGHT_3_INFO, light_strip.SwitchbotStripLight3),
  22. (FLOOR_LAMP_INFO, light_strip.SwitchbotStripLight3),
  23. (CANDLE_WARMER_LAMP_INFO, light_strip.SwitchbotCandleWarmerLamp),
  24. (RGBICWW_STRIP_LIGHT_INFO, light_strip.SwitchbotRgbicLight),
  25. (RGBICWW_FLOOR_LAMP_INFO, light_strip.SwitchbotRgbicLight),
  26. (PERMANENT_OUTDOOR_LIGHT_INFO, light_strip.SwitchbotPermanentOutdoorLight),
  27. (RGBIC_NEON_LIGHT_INFO, light_strip.SwitchbotRgbicNeonLight),
  28. (RGBICWW_CEILING_LIGHT_INFO, light_strip.SwitchbotRgbicwwCeilingLight),
  29. ]
  30. # RGB + color-temp devices. Excludes brightness-only lights (CWL) and
  31. # RGB-only lights (RGBIC Neon) whose color_modes differ from the rest.
  32. RGB_LIGHT_CASES = [
  33. case
  34. for case in ALL_LIGHT_CASES
  35. if case[1]
  36. not in (light_strip.SwitchbotCandleWarmerLamp, light_strip.SwitchbotRgbicNeonLight)
  37. ]
  38. @pytest.fixture(params=RGB_LIGHT_CASES)
  39. def device_case(request):
  40. return request.param
  41. @pytest.fixture
  42. def expected_effects(device_case):
  43. adv_info, _dev_cls = device_case
  44. EXPECTED = {
  45. SwitchbotModel.STRIP_LIGHT_3: ("christmas", "halloween", "sunset"),
  46. SwitchbotModel.FLOOR_LAMP: ("christmas", "halloween", "sunset"),
  47. SwitchbotModel.RGBICWW_STRIP_LIGHT: ("romance", "energy", "heartbeat"),
  48. SwitchbotModel.RGBICWW_FLOOR_LAMP: ("romance", "energy", "heartbeat"),
  49. SwitchbotModel.RGBICWW_CEILING_LIGHT: ("romance", "energy", "heartbeat"),
  50. SwitchbotModel.PERMANENT_OUTDOOR_LIGHT: ("romance", "energy", "heartbeat"),
  51. }
  52. return EXPECTED[adv_info.modelName]
  53. def create_device_for_command_testing(
  54. adv_info: AdvTestCase,
  55. dev_cls: type[SwitchbotBaseLight],
  56. init_data: dict | None = None,
  57. ):
  58. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  59. device = dev_cls(
  60. ble_device, "ff", "ffffffffffffffffffffffffffffffff", model=adv_info.modelName
  61. )
  62. device.update_from_advertisement(
  63. make_advertisement_data(ble_device, adv_info, init_data)
  64. )
  65. device._send_command = AsyncMock()
  66. device._check_command_result = MagicMock()
  67. device.update = AsyncMock()
  68. return device
  69. def make_advertisement_data(
  70. ble_device: BLEDevice, adv_info: AdvTestCase, init_data: dict | None = None
  71. ):
  72. """Set advertisement data with defaults."""
  73. if init_data is None:
  74. init_data = {}
  75. return SwitchBotAdvertisement(
  76. address="aa:bb:cc:dd:ee:ff",
  77. data={
  78. "rawAdvData": adv_info.service_data,
  79. "data": adv_info.data | init_data,
  80. "isEncrypted": False,
  81. "model": adv_info.model,
  82. "modelFriendlyName": adv_info.modelFriendlyName,
  83. "modelName": adv_info.modelName,
  84. },
  85. device=ble_device,
  86. rssi=-80,
  87. active=True,
  88. )
  89. @pytest.mark.asyncio
  90. async def test_default_info(device_case, expected_effects):
  91. """Test default initialization of the strip light."""
  92. adv_info, dev_cls = device_case
  93. device = create_device_for_command_testing(adv_info, dev_cls)
  94. assert device.rgb is None
  95. device._state = {"r": 30, "g": 0, "b": 0, "cw": 3200}
  96. assert device.is_on() is True
  97. assert device.on is True
  98. assert device.color_mode == ColorMode.RGB
  99. assert device.color_modes == {ColorMode.RGB, ColorMode.COLOR_TEMP}
  100. assert device.rgb == (30, 0, 0)
  101. assert device.color_temp == 3200
  102. assert device.brightness == adv_info.data["brightness"]
  103. assert device.min_temp == 2700
  104. assert device.max_temp == 6500
  105. # Check that effect list contains expected lowercase effect names
  106. effect_list = device.get_effect_list
  107. assert effect_list is not None
  108. assert all(effect.islower() for effect in effect_list)
  109. # Verify some known effects are present
  110. for effect in expected_effects:
  111. assert effect in effect_list
  112. @pytest.mark.asyncio
  113. async def test_rgbic_neon_light_info() -> None:
  114. """Test color_mode / color_modes on SwitchbotRgbicNeonLight (RGB only)."""
  115. adv_info, dev_cls = RGBIC_NEON_LIGHT_INFO, light_strip.SwitchbotRgbicNeonLight
  116. device = create_device_for_command_testing(adv_info, dev_cls)
  117. assert device.color_mode == ColorMode.RGB
  118. assert device.color_modes == {ColorMode.RGB}
  119. @pytest.mark.asyncio
  120. async def test_candle_warmer_lamp_info() -> None:
  121. """Test default initialization of the candle warmer lamp."""
  122. adv_info, dev_cls = CANDLE_WARMER_LAMP_INFO, light_strip.SwitchbotCandleWarmerLamp
  123. device = create_device_for_command_testing(adv_info, dev_cls)
  124. assert device.rgb is None
  125. assert device.is_on() is True
  126. assert device.on is True
  127. assert device.color_mode == ColorMode.BRIGHTNESS
  128. assert device.color_modes == {
  129. ColorMode.BRIGHTNESS,
  130. }
  131. assert device.brightness == adv_info.data["brightness"]
  132. assert device.get_effect_list is None
  133. @pytest.mark.asyncio
  134. async def test_candle_warmer_lamp_unsupported_operations() -> None:
  135. """Test that RGB/color-temp/effect operations are not supported on CWL."""
  136. device = create_device_for_command_testing(
  137. CANDLE_WARMER_LAMP_INFO, light_strip.SwitchbotCandleWarmerLamp
  138. )
  139. with pytest.raises(
  140. SwitchbotOperationError,
  141. match="does not support this functionality",
  142. ):
  143. await device.set_rgb(100, 255, 128, 64)
  144. with pytest.raises(
  145. SwitchbotOperationError,
  146. match="does not support this functionality",
  147. ):
  148. await device.set_color_temp(100, 4000)
  149. with pytest.raises(SwitchbotOperationError, match="not supported"):
  150. await device.set_effect("sunset")
  151. @pytest.mark.asyncio
  152. @pytest.mark.parametrize(
  153. ("basic_info", "version_info"), [(True, False), (False, True), (False, False)]
  154. )
  155. @pytest.mark.parametrize(("adv_info", "dev_cls"), ALL_LIGHT_CASES)
  156. async def test_get_basic_info_returns_none(
  157. basic_info, version_info, adv_info, dev_cls
  158. ) -> None:
  159. """Test that get_basic_info returns None if no data is available."""
  160. device = create_device_for_command_testing(adv_info, dev_cls)
  161. device._send_command = AsyncMock(side_effect=[version_info, basic_info])
  162. device._check_command_result = MagicMock(
  163. side_effect=[bool(version_info), bool(basic_info)]
  164. )
  165. assert await device.get_basic_info() is None
  166. @pytest.fixture(
  167. params=[
  168. pytest.param(
  169. (
  170. light_strip.SwitchbotRgbicNeonLight,
  171. bytes.fromhex("01 80 4e 4b ff 3a 00 19 64 ff 02"),
  172. ),
  173. id="strip-light",
  174. ),
  175. pytest.param(
  176. (bulb.SwitchbotBulb, bytes.fromhex("01 80 3b 00 00 00 0c 99 ff ff 01")),
  177. id="bulb",
  178. ),
  179. pytest.param(
  180. (
  181. light_strip.SwitchbotRgbicwwCeilingLight,
  182. bytes.fromhex("01 00 00 4b ff 3a 00 19 64"),
  183. ),
  184. id="rgbicww-ceiling-light",
  185. ),
  186. pytest.param(
  187. (ceiling_light.SwitchbotCeilingLight, bytes.fromhex("01 80 3d 0f a1")),
  188. id="ceiling-light",
  189. ),
  190. pytest.param(
  191. (light_strip.SwitchbotCandleWarmerLamp, bytes.fromhex("01 80 3d")),
  192. id="candle-warmer",
  193. ),
  194. ]
  195. )
  196. def light_status_reply(
  197. request: pytest.FixtureRequest,
  198. ) -> tuple[SwitchbotBaseLight, bytes]:
  199. device_type, status_reply = request.param
  200. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  201. if issubclass(device_type, SwitchbotEncryptedDevice):
  202. return device_type(ble_device, "ff", "ff" * 16), status_reply
  203. return device_type(ble_device), status_reply
  204. @pytest.mark.asyncio
  205. async def test_get_basic_info_status_reply_length_boundary(
  206. light_status_reply: tuple[SwitchbotBaseLight, bytes],
  207. ) -> None:
  208. device, status_reply = light_status_reply
  209. version_reply = b"\x01\x01\x0a"
  210. with patch.object(
  211. device,
  212. "_send_command",
  213. side_effect=[version_reply, status_reply[:-1], version_reply, status_reply],
  214. ):
  215. assert await device.get_basic_info() is None
  216. info = await device.get_basic_info()
  217. assert info is not None
  218. assert info["firmware"] == 1.0
  219. @pytest.mark.asyncio
  220. @pytest.mark.parametrize("version_reply", [b"\x01", b"\x01\x01"])
  221. async def test_get_basic_info_short_version_reply_returns_none(
  222. light_status_reply: tuple[SwitchbotBaseLight, bytes], version_reply: bytes
  223. ) -> None:
  224. device, status_reply = light_status_reply
  225. with patch.object(
  226. device, "_send_command", side_effect=[version_reply, status_reply]
  227. ):
  228. assert await device.get_basic_info() is None
  229. @pytest.mark.asyncio
  230. @pytest.mark.parametrize(
  231. "replies",
  232. [
  233. pytest.param([b""], id="version"),
  234. pytest.param([b"\x01\x01\x0a", b""], id="status"),
  235. ],
  236. )
  237. async def test_get_basic_info_empty_reply_raises(
  238. light_status_reply: tuple[SwitchbotBaseLight, bytes], replies: list[bytes]
  239. ) -> None:
  240. device, _ = light_status_reply
  241. with (
  242. patch.object(device, "_send_command", side_effect=replies),
  243. pytest.raises(SwitchbotOperationError, match="Sending command failed"),
  244. ):
  245. await device.get_basic_info()
  246. @pytest.mark.asyncio
  247. @pytest.mark.parametrize(
  248. "device_case",
  249. [
  250. case
  251. for case in RGB_LIGHT_CASES
  252. if case[1] is not light_strip.SwitchbotRgbicwwCeilingLight
  253. ],
  254. )
  255. @pytest.mark.parametrize(
  256. ("info_data", "result"),
  257. [
  258. (
  259. {
  260. "basic_info": b"\x01\x00<\xff\x00\xd8\x00\x19d\x00\x03",
  261. "version_info": b"\x01\x01\n",
  262. },
  263. [False, 60, 255, 0, 216, 6500, 3, 1.0],
  264. ),
  265. (
  266. {
  267. "basic_info": b"\x01\x80NK\xff:\x00\x19d\xff\x02",
  268. "version_info": b"\x01\x01\n",
  269. },
  270. [True, 78, 75, 255, 58, 6500, 2, 1.0],
  271. ),
  272. (
  273. {
  274. "basic_info": b"\x01\x80$K\xff:\x00\x13\xf9\xff\x06",
  275. "version_info": b"\x01\x01\n",
  276. },
  277. [True, 36, 75, 255, 58, 5113, 6, 1.0],
  278. ),
  279. ],
  280. )
  281. async def test_strip_light_get_basic_info(info_data, result, device_case):
  282. """Test getting basic info from the strip light."""
  283. adv_info, dev_cls = device_case
  284. device = create_device_for_command_testing(adv_info, dev_cls)
  285. device._send_command = AsyncMock(
  286. side_effect=[info_data["version_info"], info_data["basic_info"]]
  287. )
  288. device._check_command_result = MagicMock(side_effect=[True, True])
  289. info = await device.get_basic_info()
  290. assert info["isOn"] is result[0]
  291. assert info["brightness"] == result[1]
  292. assert info["r"] == result[2]
  293. assert info["g"] == result[3]
  294. assert info["b"] == result[4]
  295. assert info["cw"] == result[5]
  296. assert info["color_mode"] == result[6]
  297. assert info["firmware"] == result[7]
  298. @pytest.mark.asyncio
  299. async def test_set_color_temp(device_case):
  300. """Test setting color temperature."""
  301. adv_info, dev_cls = device_case
  302. device = create_device_for_command_testing(adv_info, dev_cls)
  303. await device.set_color_temp(50, 3000)
  304. device._send_command.assert_called_with(
  305. device._set_color_temp_command.format("320BB8")
  306. )
  307. @pytest.mark.asyncio
  308. async def test_turn_on(device_case):
  309. """Test turning on the strip light."""
  310. init_data = {"isOn": True}
  311. adv_info, dev_cls = device_case
  312. device = create_device_for_command_testing(adv_info, dev_cls, init_data)
  313. await device.turn_on()
  314. device._send_command.assert_called_with(device._turn_on_command)
  315. assert device.is_on() is True
  316. @pytest.mark.asyncio
  317. async def test_turn_off(device_case):
  318. """Test turning off the strip light."""
  319. init_data = {"isOn": False}
  320. adv_info, dev_cls = device_case
  321. device = create_device_for_command_testing(adv_info, dev_cls, init_data)
  322. await device.turn_off()
  323. device._send_command.assert_called_with(device._turn_off_command)
  324. assert device.is_on() is False
  325. @pytest.mark.asyncio
  326. async def test_set_brightness(device_case):
  327. """Test setting brightness."""
  328. adv_info, dev_cls = device_case
  329. device = create_device_for_command_testing(adv_info, dev_cls)
  330. await device.set_brightness(75)
  331. device._send_command.assert_called_with(device._set_brightness_command.format("4B"))
  332. @pytest.mark.asyncio
  333. async def test_set_rgb(device_case):
  334. """Test setting RGB values."""
  335. adv_info, dev_cls = device_case
  336. device = create_device_for_command_testing(adv_info, dev_cls)
  337. await device.set_rgb(100, 255, 128, 64)
  338. device._send_command.assert_called_with(device._set_rgb_command.format("64FF8040"))
  339. @pytest.mark.asyncio
  340. async def test_set_effect_with_invalid_effect(device_case):
  341. """Test setting an invalid effect."""
  342. adv_info, dev_cls = device_case
  343. device = create_device_for_command_testing(adv_info, dev_cls)
  344. with pytest.raises(
  345. SwitchbotOperationError, match="Effect invalid_effect not supported"
  346. ):
  347. await device.set_effect("invalid_effect")
  348. @pytest.mark.asyncio
  349. async def test_set_effect_with_valid_effect(device_case):
  350. """Test setting a valid effect."""
  351. adv_info, dev_cls = device_case
  352. device = create_device_for_command_testing(adv_info, dev_cls)
  353. device._send_multiple_commands = AsyncMock()
  354. await device.set_effect("christmas")
  355. device._send_multiple_commands.assert_called_with(device._effect_dict["christmas"])
  356. assert device.get_effect() == "christmas"
  357. @pytest.mark.asyncio
  358. async def test_effect_list_contains_lowercase_names(device_case, expected_effects):
  359. """Test that all effect names in get_effect_list are lowercase."""
  360. adv_info, dev_cls = device_case
  361. device = create_device_for_command_testing(adv_info, dev_cls)
  362. effect_list = device.get_effect_list
  363. assert effect_list is not None, "Effect list should not be None"
  364. # All effect names should be lowercase
  365. for effect_name in effect_list:
  366. assert effect_name.islower(), f"Effect name '{effect_name}' is not lowercase"
  367. # Verify some known effects are present
  368. for expected_effect in expected_effects:
  369. assert expected_effect in effect_list, (
  370. f"Expected effect '{expected_effect}' not found"
  371. )
  372. @pytest.mark.asyncio
  373. async def test_set_effect_normalizes_case(device_case):
  374. """Test that set_effect normalizes effect names to lowercase."""
  375. adv_info, dev_cls = device_case
  376. device = create_device_for_command_testing(adv_info, dev_cls)
  377. device._send_multiple_commands = AsyncMock()
  378. # Test various case combinations
  379. test_cases = ["CHRISTMAS", "Christmas", "ChRiStMaS", "christmas"]
  380. for test_effect in test_cases:
  381. await device.set_effect(test_effect)
  382. # Should always work regardless of case
  383. device._send_multiple_commands.assert_called()
  384. assert device.get_effect() == test_effect # Stored as provided
  385. @pytest.mark.parametrize(
  386. ("dev_cls", "expected_model"),
  387. [
  388. (light_strip.SwitchbotStripLight3, SwitchbotModel.STRIP_LIGHT_3),
  389. (light_strip.SwitchbotRgbicLight, SwitchbotModel.RGBICWW_STRIP_LIGHT),
  390. (
  391. light_strip.SwitchbotPermanentOutdoorLight,
  392. SwitchbotModel.PERMANENT_OUTDOOR_LIGHT,
  393. ),
  394. (light_strip.SwitchbotRgbicNeonLight, SwitchbotModel.RGBIC_NEON_ROPE_LIGHT),
  395. (light_strip.SwitchbotCandleWarmerLamp, SwitchbotModel.CANDLE_WARMER_LAMP),
  396. (
  397. light_strip.SwitchbotRgbicwwCeilingLight,
  398. SwitchbotModel.RGBICWW_CEILING_LIGHT,
  399. ),
  400. ],
  401. )
  402. def test_default_model_classvar(dev_cls, expected_model):
  403. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  404. device = dev_cls(ble_device, "ff", "ffffffffffffffffffffffffffffffff")
  405. assert device._model == expected_model
  406. def create_strip_light_device(init_data: dict | None = None):
  407. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  408. return light_strip.SwitchbotLightStrip(ble_device)
  409. @pytest.mark.asyncio
  410. async def test_strip_light_supported_color_modes():
  411. """Test that the strip light supports the expected color modes."""
  412. device = create_strip_light_device()
  413. assert device.color_modes == {
  414. ColorMode.RGB,
  415. }
  416. @pytest.mark.asyncio
  417. @pytest.mark.parametrize(
  418. ("commands", "results", "final_result"),
  419. [
  420. (("command1", "command2"), [(b"\x01", False), (None, False)], False),
  421. (("command1", "command2"), [(None, False), (b"\x01", True)], True),
  422. (("command1", "command2"), [(b"\x01", True), (b"\x01", False)], True),
  423. ],
  424. )
  425. async def test_send_multiple_commands(commands, results, final_result, device_case):
  426. """Test sending multiple commands."""
  427. adv_info, dev_cls = device_case
  428. device = create_device_for_command_testing(adv_info, dev_cls)
  429. device._send_command = AsyncMock(side_effect=[r[0] for r in results])
  430. device._check_command_result = MagicMock(side_effect=[r[1] for r in results])
  431. result = await device._send_multiple_commands(list(commands))
  432. assert result is final_result
  433. @pytest.mark.asyncio
  434. async def test_unimplemented_color_mode():
  435. class TestDevice(SwitchbotBaseLight):
  436. pass
  437. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  438. device = TestDevice(ble_device)
  439. with pytest.raises(NotImplementedError):
  440. _ = device.color_mode
  441. @pytest.mark.asyncio
  442. async def test_exception_with_wrong_model():
  443. class TestDevice(SwitchbotBaseLight):
  444. def __init__(self, device: BLEDevice, model: str = "unknown") -> None:
  445. super().__init__(device, model=model)
  446. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  447. device = TestDevice(ble_device)
  448. with pytest.raises(
  449. SwitchbotOperationError,
  450. match="Current device aa:bb:cc:dd:ee:ff does not support this functionality",
  451. ):
  452. await device.set_rgb(100, 255, 128, 64)
  453. @pytest.mark.asyncio
  454. @pytest.mark.parametrize(
  455. ("color_mode_value", "expected_color_mode"),
  456. [
  457. (1, ColorMode.EFFECT), # SEGMENTED
  458. (2, ColorMode.RGB),
  459. (3, ColorMode.EFFECT), # SCENE
  460. (4, ColorMode.EFFECT), # MUSIC
  461. (5, ColorMode.EFFECT), # CONTROLLER
  462. (6, ColorMode.COLOR_TEMP),
  463. (7, ColorMode.EFFECT), # EFFECT (RGBIC-specific)
  464. (10, ColorMode.OFF), # UNKNOWN
  465. ],
  466. )
  467. async def test_permanent_outdoor_light_color_mode(
  468. color_mode_value, expected_color_mode
  469. ):
  470. """Test that POL correctly handles all RGBICStripLightColorMode values including EFFECT (7)."""
  471. device = create_device_for_command_testing(
  472. PERMANENT_OUTDOOR_LIGHT_INFO,
  473. light_strip.SwitchbotPermanentOutdoorLight,
  474. init_data={"color_mode": color_mode_value},
  475. )
  476. assert device.color_mode == expected_color_mode
  477. @pytest.mark.asyncio
  478. async def test_rgbicww_ceiling_light_main_sub_light_state() -> None:
  479. """Test the main (warm-white) sub-light state surface."""
  480. device = create_device_for_command_testing(
  481. RGBICWW_CEILING_LIGHT_INFO, light_strip.SwitchbotRgbicwwCeilingLight
  482. )
  483. assert device.is_main_on is True
  484. assert device.main_brightness == 32
  485. @pytest.mark.asyncio
  486. @pytest.mark.parametrize(
  487. ("method", "expected_command_attr"),
  488. [
  489. ("turn_on_main", "_turn_on_main_command"),
  490. ("turn_off_main", "_turn_off_main_command"),
  491. ("turn_on_color", "_turn_on_color_command"),
  492. ("turn_off_color", "_turn_off_color_command"),
  493. ],
  494. )
  495. async def test_rgbicww_ceiling_light_sub_light_power(
  496. method: str, expected_command_attr: str
  497. ) -> None:
  498. """Test the per-sub-light power commands."""
  499. device = create_device_for_command_testing(
  500. RGBICWW_CEILING_LIGHT_INFO, light_strip.SwitchbotRgbicwwCeilingLight
  501. )
  502. await getattr(device, method)()
  503. device._send_command.assert_called_with(getattr(device, expected_command_attr))
  504. @pytest.mark.asyncio
  505. async def test_rgbicww_ceiling_light_set_main_brightness() -> None:
  506. """Test setting main (warm-white) sub-light brightness."""
  507. device = create_device_for_command_testing(
  508. RGBICWW_CEILING_LIGHT_INFO, light_strip.SwitchbotRgbicwwCeilingLight
  509. )
  510. await device.set_main_brightness(75)
  511. device._send_command.assert_called_with(
  512. device._set_main_brightness_command.format("4B")
  513. )
  514. @pytest.mark.asyncio
  515. async def test_rgbicww_ceiling_light_set_main_color_temp() -> None:
  516. """Test setting main (warm-white) sub-light color temperature."""
  517. device = create_device_for_command_testing(
  518. RGBICWW_CEILING_LIGHT_INFO, light_strip.SwitchbotRgbicwwCeilingLight
  519. )
  520. await device.set_main_color_temp(3000)
  521. device._send_command.assert_called_with(
  522. device._set_main_color_temp_command.format("0BB8")
  523. )
  524. @pytest.mark.asyncio
  525. async def test_rgbicww_ceiling_light_color_modes() -> None:
  526. """Test the supported color modes set."""
  527. device = create_device_for_command_testing(
  528. RGBICWW_CEILING_LIGHT_INFO, light_strip.SwitchbotRgbicwwCeilingLight
  529. )
  530. assert device.color_modes == {ColorMode.RGB, ColorMode.COLOR_TEMP}
  531. @pytest.mark.asyncio
  532. @pytest.mark.parametrize(
  533. ("color_mode_value", "expected_color_mode"),
  534. [
  535. (1, ColorMode.EFFECT), # SEGMENTED
  536. (2, ColorMode.RGB), # COLOR
  537. (3, ColorMode.EFFECT), # SCENE
  538. (4, ColorMode.EFFECT), # MUSIC
  539. (5, ColorMode.EFFECT), # CONTROLLER
  540. (6, ColorMode.COLOR_TEMP), # WARMWHITE
  541. (7, ColorMode.EFFECT), # EFFECT
  542. (10, ColorMode.OFF), # UNKNOWN
  543. ],
  544. )
  545. async def test_rgbicww_ceiling_light_color_mode(
  546. color_mode_value, expected_color_mode
  547. ) -> None:
  548. """Test color_mode mapping for the ceiling light."""
  549. device = create_device_for_command_testing(
  550. RGBICWW_CEILING_LIGHT_INFO,
  551. light_strip.SwitchbotRgbicwwCeilingLight,
  552. init_data={"color_mode": color_mode_value},
  553. )
  554. assert device.color_mode == expected_color_mode
  555. @pytest.mark.asyncio
  556. async def test_rgbicww_ceiling_light_get_basic_info_with_main_state() -> None:
  557. """
  558. Test that the ceiling light's get_basic_info reads RGB over GATT.
  559. Power/brightness/mode/color-temp come from the advertisement, not GATT,
  560. so they are not part of the get_basic_info result.
  561. """
  562. device = create_device_for_command_testing(
  563. RGBICWW_CEILING_LIGHT_INFO, light_strip.SwitchbotRgbicwwCeilingLight
  564. )
  565. device._send_command = AsyncMock(
  566. side_effect=[b"\x01\x01\n", b"\x01\x80NK\xff:\xa0\x19d\xff\x02"]
  567. )
  568. device._check_command_result = MagicMock(side_effect=[True, True])
  569. info = await device.get_basic_info()
  570. assert info["r"] == 75
  571. assert info["g"] == 255
  572. assert info["b"] == 58
  573. assert info["firmware"] == 1.0
  574. # Power state must not be sourced from GATT (advertisement is authoritative).
  575. assert "isOn" not in info
  576. assert "main_isOn" not in info
  577. @pytest.mark.asyncio
  578. @pytest.mark.parametrize(
  579. ("color_on", "main_on", "expected"),
  580. [
  581. (False, False, False),
  582. (False, True, True),
  583. (True, False, True),
  584. (True, True, True),
  585. (None, None, None),
  586. (None, False, None),
  587. (False, None, None),
  588. (None, True, True),
  589. (True, None, True),
  590. ],
  591. )
  592. async def test_ceiling_any_light_on(
  593. color_on: bool | None, main_on: bool | None, expected: bool | None
  594. ) -> None:
  595. device = create_device_for_command_testing(
  596. RGBICWW_CEILING_LIGHT_INFO,
  597. light_strip.SwitchbotRgbicwwCeilingLight,
  598. init_data={"isOn": color_on, "main_isOn": main_on},
  599. )
  600. assert device.is_any_light_on is expected
  601. assert device.on is color_on
  602. assert device.is_main_on is main_on
  603. @pytest.mark.asyncio
  604. @pytest.mark.parametrize(
  605. ("method", "command"), [("turn_on", "570F490101"), ("turn_off", "570F490102")]
  606. )
  607. async def test_ceiling_master_power_command(method: str, command: str) -> None:
  608. device = create_device_for_command_testing(
  609. RGBICWW_CEILING_LIGHT_INFO, light_strip.SwitchbotRgbicwwCeilingLight
  610. )
  611. await getattr(device, method)()
  612. device._send_command.assert_awaited_once_with(command)