1
0

test_meter_pro_settings.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. """Display and calibration settings of the Meter Pro (and Meter Pro CO2)."""
  2. from unittest.mock import AsyncMock
  3. import pytest
  4. from bleak.backends.device import BLEDevice
  5. from switchbot import (
  6. MeterProAlarmMode,
  7. MeterProAlarmVolume,
  8. MeterProCO2AlarmMode,
  9. MeterProCO2AlarmStatus,
  10. MeterProComfortStatus,
  11. MeterProDateFormat,
  12. MeterProDisplaySource,
  13. MeterProDisplayZone,
  14. MeterProTemperatureHumidityAlarmMode,
  15. MeterProTemperatureHumidityAlarmStatus,
  16. MeterProZoneIcon,
  17. SwitchbotOperationError,
  18. )
  19. from switchbot.devices.meter_pro import SwitchbotMeterPro, SwitchbotMeterProCO2
  20. def create_device(cls=SwitchbotMeterPro):
  21. ble_device = BLEDevice(
  22. address="aa:bb:cc:dd:ee:ff", name="any", details={"rssi": -80}
  23. )
  24. device = cls(ble_device)
  25. device._send_command = AsyncMock(return_value=bytes.fromhex("01"))
  26. return device
  27. # Captured from a Meter Pro (firmware 1.9): middle zone shows this device with
  28. # the indoor icon, bottom zone shows an Indoor/Outdoor Meter with the outdoor
  29. # icon, data update every 600 s, DD/MM, battery icon always shown.
  30. DISPLAY_SETTINGS = "011253000000000000d4e5f6a1b2c30258010101"
  31. @pytest.mark.asyncio
  32. async def test_get_display_settings():
  33. device = create_device()
  34. device._send_command.return_value = bytes.fromhex(DISPLAY_SETTINGS)
  35. settings = await device.get_display_settings()
  36. device._send_command.assert_called_with("570f690701")
  37. assert settings == {
  38. "middle": {
  39. "icon": MeterProZoneIcon.INDOOR,
  40. "source": MeterProDisplaySource.METER_PRO,
  41. "source_mac": None,
  42. },
  43. "bottom": {
  44. "icon": MeterProZoneIcon.OUTDOOR,
  45. "source": MeterProDisplaySource.INDOOR_OUTDOOR_METER,
  46. "source_mac": "D4:E5:F6:A1:B2:C3",
  47. },
  48. "data_update_interval": 600,
  49. "date_format": MeterProDateFormat.DAY_MONTH,
  50. "battery_icon_always": True,
  51. }
  52. @pytest.mark.asyncio
  53. async def test_get_display_settings_factory_state():
  54. """Factory layout: unused bottom zone, MM/DD, battery icon only when low."""
  55. device = create_device()
  56. device._send_command.return_value = bytes.fromhex(
  57. "0110500000000000000000000000000258000100"
  58. )
  59. settings = await device.get_display_settings()
  60. assert settings["bottom"] == {
  61. "icon": None,
  62. "source": MeterProDisplaySource.NONE,
  63. "source_mac": None,
  64. }
  65. assert settings["date_format"] is MeterProDateFormat.MONTH_DAY
  66. assert settings["battery_icon_always"] is False
  67. @pytest.mark.asyncio
  68. @pytest.mark.parametrize(
  69. ("date_code", "battery_code", "date_format", "battery_icon_always"),
  70. [
  71. (0x02, 0x00, None, False),
  72. (0x00, 0xFE, MeterProDateFormat.MONTH_DAY, None),
  73. (0xFF, 0xFF, None, None),
  74. ],
  75. )
  76. async def test_get_display_settings_unknown_values(
  77. date_code: int,
  78. battery_code: int,
  79. date_format: MeterProDateFormat | None,
  80. battery_icon_always: bool | None,
  81. ):
  82. device = create_device()
  83. response = bytearray.fromhex(DISPLAY_SETTINGS)
  84. response[17] = date_code
  85. response[19] = battery_code
  86. device._send_command.return_value = bytes(response)
  87. settings = await device.get_display_settings()
  88. assert settings["date_format"] is date_format
  89. assert settings["battery_icon_always"] is battery_icon_always
  90. assert settings["data_update_interval"] == 600
  91. @pytest.mark.asyncio
  92. async def test_get_display_settings_short_response():
  93. """Older firmware may stop after the MAC addresses."""
  94. device = create_device()
  95. device._send_command.return_value = bytes.fromhex(DISPLAY_SETTINGS[:30])
  96. settings = await device.get_display_settings()
  97. assert settings["bottom"]["source_mac"] == "D4:E5:F6:A1:B2:C3"
  98. assert settings["data_update_interval"] is None
  99. assert settings["date_format"] is None
  100. assert settings["battery_icon_always"] is None
  101. @pytest.mark.asyncio
  102. async def test_get_display_settings_unknown_source_kept_raw():
  103. device = create_device()
  104. device._send_command.return_value = bytes.fromhex(
  105. "011257000000000000aabbccddeeff0258010101"
  106. )
  107. settings = await device.get_display_settings()
  108. assert settings["bottom"]["source"] == 7
  109. @pytest.mark.asyncio
  110. async def test_get_display_settings_weather_station_and_hub3():
  111. device = create_device()
  112. device._send_command.return_value = bytes.fromhex(
  113. "011289aabbccddee01aabbccddee020258010101"
  114. )
  115. settings = await device.get_display_settings()
  116. assert settings["middle"]["source"] is MeterProDisplaySource.WEATHER_STATION
  117. assert settings["bottom"]["source"] is MeterProDisplaySource.HUB3
  118. @pytest.mark.asyncio
  119. @pytest.mark.parametrize(
  120. "response",
  121. ["02", "011253000000"], # error status, then response too short
  122. )
  123. async def test_get_display_settings_failure(response: str):
  124. device = create_device()
  125. device._send_command.return_value = bytes.fromhex(response)
  126. with pytest.raises(SwitchbotOperationError):
  127. await device.get_display_settings()
  128. @pytest.mark.asyncio
  129. @pytest.mark.parametrize(
  130. ("zone", "source", "mac", "icon", "expected_payload"),
  131. [
  132. (
  133. MeterProDisplayZone.BOTTOM,
  134. MeterProDisplaySource.INDOOR_OUTDOOR_METER,
  135. "D4:E5:F6:A1:B2:C3",
  136. MeterProZoneIcon.OUTDOOR,
  137. "570f680701020203d4e5f6a1b2c3",
  138. ),
  139. (
  140. MeterProDisplayZone.BOTTOM,
  141. MeterProDisplaySource.NETWORK,
  142. None,
  143. MeterProZoneIcon.OUTDOOR,
  144. "570f680701020206000000000000",
  145. ),
  146. (
  147. MeterProDisplayZone.BOTTOM,
  148. MeterProDisplaySource.NONE,
  149. None,
  150. None,
  151. "570f68070102ff00000000000000",
  152. ),
  153. (
  154. MeterProDisplayZone.MIDDLE,
  155. MeterProDisplaySource.METER_PRO,
  156. None,
  157. MeterProZoneIcon.INDOOR,
  158. "570f680701010105000000000000",
  159. ),
  160. (
  161. MeterProDisplayZone.MIDDLE,
  162. MeterProDisplaySource.METER_PLUS,
  163. "aa-bb-cc-dd-ee-ff",
  164. None,
  165. "570f68070101ff02aabbccddeeff",
  166. ),
  167. ],
  168. )
  169. async def test_set_display_zone(
  170. zone: MeterProDisplayZone,
  171. source: MeterProDisplaySource,
  172. mac: str | None,
  173. icon: MeterProZoneIcon | None,
  174. expected_payload: str,
  175. ):
  176. device = create_device()
  177. await device.set_display_zone(zone, source, mac, icon)
  178. device._send_command.assert_called_with(expected_payload)
  179. @pytest.mark.asyncio
  180. @pytest.mark.parametrize(
  181. ("source", "mac"),
  182. [
  183. (MeterProDisplaySource.METER, None), # needs a MAC
  184. (MeterProDisplaySource.NETWORK, "aa:bb:cc:dd:ee:ff"), # takes none
  185. (MeterProDisplaySource.NONE, "aa:bb:cc:dd:ee:ff"), # takes none
  186. (MeterProDisplaySource.METER, "aa:bb:cc"), # malformed
  187. (MeterProDisplaySource.METER, "zz:bb:cc:dd:ee:ff"), # malformed
  188. ],
  189. )
  190. async def test_set_display_zone_invalid(source: MeterProDisplaySource, mac: str):
  191. device = create_device()
  192. with pytest.raises(SwitchbotOperationError):
  193. await device.set_display_zone(MeterProDisplayZone.BOTTOM, source, mac)
  194. device._send_command.assert_not_called()
  195. @pytest.mark.asyncio
  196. @pytest.mark.parametrize(
  197. ("middle", "bottom", "expected_payload"),
  198. [
  199. (MeterProZoneIcon.INDOOR, MeterProZoneIcon.OUTDOOR, "570f680701030102"),
  200. (None, MeterProZoneIcon.INDOOR, "570f680701030f01"),
  201. (MeterProZoneIcon.OUTDOOR, None, "570f68070103020f"),
  202. ],
  203. )
  204. async def test_set_zone_icons(
  205. middle: MeterProZoneIcon | None,
  206. bottom: MeterProZoneIcon | None,
  207. expected_payload: str,
  208. ):
  209. device = create_device()
  210. await device.set_zone_icons(middle, bottom)
  211. device._send_command.assert_called_with(expected_payload)
  212. @pytest.mark.asyncio
  213. @pytest.mark.parametrize(
  214. ("seconds", "expected_payload"),
  215. [(300, "570f68070105012c"), (600, "570f680701050258"), (1800, "570f680701050708")],
  216. )
  217. async def test_set_data_update_interval(seconds: int, expected_payload: str):
  218. device = create_device()
  219. await device.set_data_update_interval(seconds)
  220. device._send_command.assert_called_with(expected_payload)
  221. @pytest.mark.asyncio
  222. @pytest.mark.parametrize("seconds", [0, -1, 0x10000, True, 300.0, "300"])
  223. async def test_set_data_update_interval_invalid(seconds: object):
  224. device = create_device()
  225. with pytest.raises(SwitchbotOperationError):
  226. await device.set_data_update_interval(seconds)
  227. device._send_command.assert_not_called()
  228. @pytest.mark.asyncio
  229. @pytest.mark.parametrize(
  230. ("date_format", "expected_payload"),
  231. [
  232. (MeterProDateFormat.MONTH_DAY, "570f6807010700"),
  233. (MeterProDateFormat.DAY_MONTH, "570f6807010701"),
  234. ],
  235. )
  236. async def test_set_date_format(date_format: MeterProDateFormat, expected_payload: str):
  237. device = create_device()
  238. await device.set_date_format(date_format)
  239. device._send_command.assert_called_with(expected_payload)
  240. @pytest.mark.asyncio
  241. @pytest.mark.parametrize(
  242. ("always", "expected_payload"),
  243. [(True, "570f6807010801"), (False, "570f6807010800")],
  244. )
  245. async def test_set_battery_icon(always: bool, expected_payload: str):
  246. device = create_device()
  247. await device.set_battery_icon(always)
  248. device._send_command.assert_called_with(expected_payload)
  249. @pytest.mark.asyncio
  250. @pytest.mark.parametrize(
  251. ("fahrenheit", "expected_payload"),
  252. [(True, "570f6807010901"), (False, "570f6807010900")],
  253. )
  254. async def test_set_temperature_unit(fahrenheit: bool, expected_payload: str):
  255. device = create_device()
  256. await device.set_temperature_unit(fahrenheit)
  257. device._send_command.assert_called_with(expected_payload)
  258. @pytest.mark.asyncio
  259. @pytest.mark.parametrize(
  260. ("response", "expected"),
  261. [
  262. ("010101", {"unit_switch": False, "source_switch": True}),
  263. ("010000", {"unit_switch": True, "source_switch": False}),
  264. ("01ff01", {"unit_switch": None, "source_switch": True}),
  265. ("0100ff", {"unit_switch": True, "source_switch": None}),
  266. ("01ffff", {"unit_switch": None, "source_switch": None}),
  267. ("010202", {"unit_switch": None, "source_switch": None}),
  268. ],
  269. )
  270. async def test_get_button_functions(response: str, expected: dict[str, bool | None]):
  271. device = create_device()
  272. device._send_command.return_value = bytes.fromhex(response)
  273. assert await device.get_button_functions() == expected
  274. device._send_command.assert_called_with("570f690704")
  275. @pytest.mark.asyncio
  276. @pytest.mark.parametrize(
  277. ("unit_switch", "source_switch", "expected_payload"),
  278. [
  279. (True, False, "570f680701060000"),
  280. (False, True, "570f680701060101"),
  281. (None, True, "570f68070106ff01"),
  282. (False, None, "570f6807010601ff"),
  283. ],
  284. )
  285. async def test_set_button_functions(
  286. unit_switch: bool | None, source_switch: bool | None, expected_payload: str
  287. ):
  288. device = create_device()
  289. await device.set_button_functions(unit_switch, source_switch)
  290. device._send_command.assert_called_with(expected_payload)
  291. @pytest.mark.asyncio
  292. @pytest.mark.parametrize(
  293. ("response", "temperature", "humidity"),
  294. [
  295. ("01000000", 0.0, 0), # factory state: sign bits cleared
  296. ("01808000", 0.0, 0),
  297. ("01018305", -1.5, 3),
  298. ("01820a03", 2.3, -10),
  299. ("01800005", 0.5, 0),
  300. ],
  301. )
  302. async def test_get_calibration(response: str, temperature: float, humidity: int):
  303. device = create_device()
  304. device._send_command.return_value = bytes.fromhex(response)
  305. calibration = await device.get_calibration()
  306. device._send_command.assert_called_with("570f6903")
  307. assert calibration == {"temperature": temperature, "humidity": humidity}
  308. assert str(calibration["temperature"]) != "-0.0"
  309. @pytest.mark.asyncio
  310. @pytest.mark.parametrize("response", ["0180800a", "0180e400", "01806400"])
  311. async def test_get_calibration_rejects_invalid_values(response: str):
  312. device = create_device()
  313. device._send_command.return_value = bytes.fromhex(response)
  314. with pytest.raises(SwitchbotOperationError, match="Invalid calibration response"):
  315. await device.get_calibration()
  316. @pytest.mark.asyncio
  317. async def test_get_calibration_short_response():
  318. device = create_device()
  319. device._send_command.return_value = bytes.fromhex("0101")
  320. with pytest.raises(SwitchbotOperationError):
  321. await device.get_calibration()
  322. @pytest.mark.asyncio
  323. @pytest.mark.parametrize(
  324. ("temperature", "humidity", "expected_payload"),
  325. [
  326. (0, 0, "570f6803808000"),
  327. (-1.5, 3, "570f6803018305"),
  328. (2.3, -10, "570f6803820a03"),
  329. (0.5, 0, "570f6803808005"), # positive below one degree keeps its sign
  330. (-0.5, 0, "570f6803008005"),
  331. (1.25, 0, "570f6803818003"), # rounded to one decimal
  332. ],
  333. )
  334. async def test_set_calibration(
  335. temperature: float, humidity: int, expected_payload: str
  336. ):
  337. device = create_device()
  338. await device.set_calibration(temperature, humidity)
  339. device._send_command.assert_called_with(expected_payload)
  340. @pytest.mark.asyncio
  341. @pytest.mark.parametrize(
  342. ("temperature", "humidity"),
  343. [(128, 0), (-128, 0), (0, 100), (0, -100), (0, True), (0, 0.5), (0, "1")],
  344. )
  345. async def test_set_calibration_invalid(temperature: float, humidity: object):
  346. device = create_device()
  347. with pytest.raises(SwitchbotOperationError):
  348. await device.set_calibration(temperature, humidity)
  349. device._send_command.assert_not_called()
  350. @pytest.mark.asyncio
  351. @pytest.mark.parametrize(
  352. ("method", "args"),
  353. [
  354. ("set_calibration", (1.0, 1)),
  355. ("set_display_zone", (MeterProDisplayZone.BOTTOM, MeterProDisplaySource.NONE)),
  356. ("set_zone_icons", (MeterProZoneIcon.INDOOR,)),
  357. ("set_data_update_interval", (600,)),
  358. ("set_date_format", (MeterProDateFormat.DAY_MONTH,)),
  359. ("set_battery_icon", (True,)),
  360. ("set_temperature_unit", (False,)),
  361. ("set_button_functions", (True, True)),
  362. ],
  363. )
  364. async def test_setters_raise_on_device_error(method: str, args: tuple):
  365. device = create_device()
  366. device._send_command.return_value = bytes.fromhex("05") # not supported
  367. with pytest.raises(SwitchbotOperationError):
  368. await getattr(device, method)(*args)
  369. @pytest.mark.asyncio
  370. @pytest.mark.parametrize(
  371. ("method", "args", "expected_payload"),
  372. [
  373. ("set_calibration", (1.0, 1), "570f6803818100"),
  374. (
  375. "set_display_zone",
  376. (MeterProDisplayZone.BOTTOM, MeterProDisplaySource.NETWORK),
  377. "570f68070102ff06000000000000",
  378. ),
  379. ("set_zone_icons", (MeterProZoneIcon.INDOOR,), "570f68070103010f"),
  380. ("set_data_update_interval", (600,), "570f680701050258"),
  381. ("set_date_format", (MeterProDateFormat.DAY_MONTH,), "570f6807010701"),
  382. ("set_battery_icon", (True,), "570f6807010801"),
  383. ("set_temperature_unit", (False,), "570f6807010900"),
  384. ("set_button_functions", (True, True), "570f680701060001"),
  385. ],
  386. )
  387. async def test_co2_variant_shares_display_settings(
  388. method: str, args: tuple, expected_payload: str
  389. ):
  390. device = create_device(SwitchbotMeterProCO2)
  391. await getattr(device, method)(*args)
  392. device._send_command.assert_called_with(expected_payload)
  393. @pytest.mark.asyncio
  394. @pytest.mark.parametrize(
  395. ("device_class", "response", "expected"),
  396. [
  397. (
  398. SwitchbotMeterPro,
  399. "01009c4600921e00",
  400. {
  401. "custom": False,
  402. "temperature_low_c": 18.0,
  403. "temperature_high_c": 28.0,
  404. "humidity_low": 30,
  405. "humidity_high": 70,
  406. "status": MeterProComfortStatus.COMFORTABLE,
  407. },
  408. ),
  409. (
  410. SwitchbotMeterProCO2,
  411. "010180e3730080fe",
  412. {
  413. "custom": True,
  414. "temperature_low_c": -0.3,
  415. "temperature_high_c": 0.7,
  416. "humidity_low": 0,
  417. "humidity_high": 99,
  418. "status": MeterProComfortStatus.WET,
  419. },
  420. ),
  421. ],
  422. )
  423. async def test_get_comfort_level(
  424. device_class: type[SwitchbotMeterPro],
  425. response: str,
  426. expected: dict[str, float | int | bool | MeterProComfortStatus],
  427. ):
  428. device = create_device(device_class)
  429. device._send_command.return_value = bytes.fromhex(response)
  430. assert await device.get_comfort_level() == expected
  431. device._send_command.assert_awaited_once_with("570f69020101")
  432. @pytest.mark.asyncio
  433. @pytest.mark.parametrize(
  434. ("limits", "custom", "expected"),
  435. [
  436. ((0.5, 22.34, 40, 82), True, "889652358028"),
  437. ((-0.5, 0.66, 0, 99), True, "888063750000"),
  438. ((18.0, 28.0, 30, 70), False, "809c4600921e"),
  439. ],
  440. )
  441. async def test_set_comfort_level(
  442. limits: tuple[float, float, int, int], custom: bool, expected: str
  443. ):
  444. device = create_device()
  445. await device.set_comfort_level(*limits, custom=custom)
  446. device._send_command.assert_awaited_once_with("570f680201" + expected)
  447. @pytest.mark.asyncio
  448. @pytest.mark.parametrize(
  449. ("limits", "custom", "message"),
  450. [
  451. ((-128, 28.0, 30, 70), True, "Temperature must be"),
  452. ((True, 28.0, 30, 70), True, "Temperature must be"),
  453. ((18.01, 18.04, 30, 70), True, "Lower comfort limits"),
  454. ((18, 28, 30, 100), True, "Humidity limits"),
  455. ((18, 28, True, 70), True, "Humidity limits"),
  456. ((18, 28, 70, 70), True, "Lower comfort limits"),
  457. ((18, 28, 30, 70), 1, "custom must be a boolean"),
  458. ],
  459. )
  460. async def test_set_comfort_level_rejects_invalid_input(
  461. limits: tuple[float, float, int, int], custom: bool, message: str
  462. ):
  463. device = create_device()
  464. with pytest.raises(SwitchbotOperationError, match=message):
  465. await device.set_comfort_level(*limits, custom=custom)
  466. device._send_command.assert_not_awaited()
  467. @pytest.mark.asyncio
  468. @pytest.mark.parametrize(
  469. "response", ["02", "01", "01029c4600921e00", "01009c460a921e00", "01009c6400921e00"]
  470. )
  471. async def test_get_comfort_level_rejects_invalid_response(response: str):
  472. device = create_device()
  473. device._send_command.return_value = bytes.fromhex(response)
  474. with pytest.raises(SwitchbotOperationError):
  475. await device.get_comfort_level()
  476. @pytest.mark.asyncio
  477. async def test_set_comfort_level_rejects_device_error():
  478. device = create_device()
  479. device._send_command.return_value = b"\x02"
  480. with pytest.raises(SwitchbotOperationError, match="set_comfort_level"):
  481. await device.set_comfort_level(18, 28, 30, 70)
  482. @pytest.mark.asyncio
  483. @pytest.mark.parametrize(
  484. ("device_class", "response", "mode", "volume"),
  485. [
  486. (SwitchbotMeterPro, "010000", MeterProAlarmMode.DISABLED, None),
  487. (
  488. SwitchbotMeterProCO2,
  489. "010402",
  490. MeterProAlarmMode.SOUND_AND_FLASH,
  491. MeterProAlarmVolume.HIGH,
  492. ),
  493. (SwitchbotMeterPro, "01ff01", MeterProAlarmMode.FLASH_ONLY, None),
  494. ],
  495. )
  496. async def test_get_alarm_settings(
  497. device_class: type[SwitchbotMeterPro],
  498. response: str,
  499. mode: MeterProAlarmMode,
  500. volume: MeterProAlarmVolume | None,
  501. ):
  502. device = create_device(device_class)
  503. device._send_command.return_value = bytes.fromhex(response)
  504. assert await device.get_alarm_settings() == {"mode": mode, "volume": volume}
  505. device._send_command.assert_awaited_once_with("570f690204")
  506. @pytest.mark.asyncio
  507. @pytest.mark.parametrize(
  508. ("mode", "volume", "payload"),
  509. [
  510. (MeterProAlarmMode.DISABLED, None, "ff00"),
  511. (MeterProAlarmMode.FLASH_ONLY, MeterProAlarmVolume.OFF, "0101"),
  512. (MeterProAlarmMode.SOUND_AND_FLASH, MeterProAlarmVolume.LOW, "0202"),
  513. (MeterProAlarmMode.SOUND_AND_FLASH, MeterProAlarmVolume.MEDIUM, "0302"),
  514. (MeterProAlarmMode.SOUND_AND_FLASH, MeterProAlarmVolume.HIGH, "0402"),
  515. ],
  516. )
  517. async def test_set_alarm_settings(
  518. mode: MeterProAlarmMode, volume: MeterProAlarmVolume | None, payload: str
  519. ):
  520. device = create_device()
  521. await device.set_alarm_settings(mode, volume)
  522. device._send_command.assert_awaited_once_with("570f680204" + payload)
  523. @pytest.mark.asyncio
  524. @pytest.mark.parametrize("response", ["02", "0100", "010403"])
  525. async def test_get_alarm_settings_rejects_invalid_response(response: str):
  526. device = create_device()
  527. device._send_command.return_value = bytes.fromhex(response)
  528. with pytest.raises(SwitchbotOperationError):
  529. await device.get_alarm_settings()
  530. @pytest.mark.asyncio
  531. @pytest.mark.parametrize(
  532. ("mode", "volume", "message"),
  533. [(True, None, "mode must be"), (MeterProAlarmMode.DISABLED, 5, "volume must be")],
  534. )
  535. async def test_set_alarm_settings_rejects_invalid_input(
  536. mode: object, volume: object, message: str
  537. ):
  538. device = create_device()
  539. with pytest.raises(SwitchbotOperationError, match=message):
  540. await device.set_alarm_settings(mode, volume)
  541. device._send_command.assert_not_awaited()
  542. @pytest.mark.asyncio
  543. async def test_set_alarm_settings_rejects_device_error():
  544. device = create_device()
  545. device._send_command.return_value = b"\x02"
  546. with pytest.raises(SwitchbotOperationError, match="set_alarm_settings"):
  547. await device.set_alarm_settings(MeterProAlarmMode.DISABLED)
  548. @pytest.mark.asyncio
  549. @pytest.mark.parametrize(
  550. ("response", "mode", "status"),
  551. [
  552. ("01000578019000", MeterProCO2AlarmMode.DISABLED, MeterProCO2AlarmStatus.NONE),
  553. (
  554. "010b05780190fe",
  555. MeterProCO2AlarmMode.CUSTOM
  556. | MeterProCO2AlarmMode.LOW
  557. | MeterProCO2AlarmMode.HIGH,
  558. MeterProCO2AlarmStatus.HIGH,
  559. ),
  560. (
  561. "01840578019003",
  562. MeterProCO2AlarmMode(0x84),
  563. MeterProCO2AlarmStatus.IN_RANGE,
  564. ),
  565. ],
  566. )
  567. async def test_get_co2_alarm(
  568. response: str, mode: MeterProCO2AlarmMode, status: MeterProCO2AlarmStatus
  569. ):
  570. device = create_device(SwitchbotMeterProCO2)
  571. device._send_command.return_value = bytes.fromhex(response)
  572. assert await device.get_co2_alarm() == {
  573. "mode": mode,
  574. "low_ppm": 400,
  575. "high_ppm": 1400,
  576. "status": status,
  577. }
  578. device._send_command.assert_awaited_once_with("570f69020301")
  579. @pytest.mark.asyncio
  580. @pytest.mark.parametrize(
  581. ("mode", "low", "high", "payload"),
  582. [
  583. (MeterProCO2AlarmMode.DISABLED, 400, 1400, "0005780190"),
  584. (MeterProCO2AlarmMode.LOW, 401, 1101, "01044d0191"),
  585. (MeterProCO2AlarmMode.HIGH, 0, 0xFFFF, "02ffff0000"),
  586. (
  587. MeterProCO2AlarmMode.LOW | MeterProCO2AlarmMode.HIGH,
  588. 425,
  589. 2000,
  590. "0307d001a9",
  591. ),
  592. (MeterProCO2AlarmMode.IN_RANGE, 1200, 1200, "0404b004b0"),
  593. (
  594. MeterProCO2AlarmMode.HIGH | MeterProCO2AlarmMode.CUSTOM,
  595. 425,
  596. 2000,
  597. "0a07d001a9",
  598. ),
  599. ],
  600. )
  601. async def test_set_co2_alarm(
  602. mode: MeterProCO2AlarmMode, low: int, high: int, payload: str
  603. ):
  604. device = create_device(SwitchbotMeterProCO2)
  605. await device.set_co2_alarm(mode, low, high)
  606. device._send_command.assert_awaited_once_with("570f68020301" + payload)
  607. @pytest.mark.asyncio
  608. @pytest.mark.parametrize("response", ["02", "010005780190"])
  609. async def test_get_co2_alarm_rejects_invalid_response(response: str):
  610. device = create_device(SwitchbotMeterProCO2)
  611. device._send_command.return_value = bytes.fromhex(response)
  612. with pytest.raises(SwitchbotOperationError, match="get_co2_alarm"):
  613. await device.get_co2_alarm()
  614. @pytest.mark.asyncio
  615. @pytest.mark.parametrize(
  616. ("mode", "low", "high", "message"),
  617. [
  618. (True, 400, 1400, "Invalid CO2 alarm mode"),
  619. (MeterProCO2AlarmMode(0x10), 400, 1400, "Invalid CO2 alarm mode"),
  620. (MeterProCO2AlarmMode.LOW, True, 1400, "unsigned 16-bit integers"),
  621. (MeterProCO2AlarmMode.LOW, -1, 1400, "unsigned 16-bit integers"),
  622. (MeterProCO2AlarmMode.LOW, 400, 0x10000, "unsigned 16-bit integers"),
  623. (MeterProCO2AlarmMode.LOW, 400, 1400.0, "unsigned 16-bit integers"),
  624. (MeterProCO2AlarmMode.IN_RANGE, 1400, 400, "Lower CO2 alarm limit"),
  625. ],
  626. )
  627. async def test_set_co2_alarm_rejects_invalid_input(
  628. mode: object, low: object, high: object, message: str
  629. ):
  630. device = create_device(SwitchbotMeterProCO2)
  631. with pytest.raises(SwitchbotOperationError, match=message):
  632. await device.set_co2_alarm(mode, low, high)
  633. device._send_command.assert_not_awaited()
  634. @pytest.mark.asyncio
  635. async def test_set_co2_alarm_rejects_device_error():
  636. device = create_device(SwitchbotMeterProCO2)
  637. device._send_command.return_value = b"\x02"
  638. with pytest.raises(SwitchbotOperationError, match="set_co2_alarm"):
  639. await device.set_co2_alarm(MeterProCO2AlarmMode.DISABLED, 400, 1400)
  640. def test_co2_alarm_is_only_available_on_the_co2_model():
  641. device = create_device()
  642. assert not hasattr(device, "get_co2_alarm")
  643. assert not hasattr(device, "set_co2_alarm")
  644. @pytest.mark.asyncio
  645. @pytest.mark.parametrize(
  646. ("device_class", "response", "expected"),
  647. [
  648. (
  649. SwitchbotMeterPro,
  650. "0100000000000000",
  651. {
  652. "mode": MeterProTemperatureHumidityAlarmMode.DISABLED,
  653. "temperature_low_c": 0.0,
  654. "temperature_high_c": 0.0,
  655. "humidity_low": 0,
  656. "humidity_high": 0,
  657. "temperature_status": MeterProTemperatureHumidityAlarmStatus.NONE,
  658. "humidity_status": MeterProTemperatureHumidityAlarmStatus.NONE,
  659. },
  660. ),
  661. (
  662. SwitchbotMeterProCO2,
  663. "014297a745009423",
  664. {
  665. "mode": (
  666. MeterProTemperatureHumidityAlarmMode.TEMPERATURE_HIGH
  667. | MeterProTemperatureHumidityAlarmMode.HUMIDITY_IN_RANGE
  668. ),
  669. "temperature_low_c": -0.5,
  670. "temperature_high_c": 23.4,
  671. "humidity_low": 20,
  672. "humidity_high": 39,
  673. "temperature_status": MeterProTemperatureHumidityAlarmStatus.HIGH,
  674. "humidity_status": MeterProTemperatureHumidityAlarmStatus.IN_RANGE,
  675. },
  676. ),
  677. ],
  678. )
  679. async def test_get_temperature_humidity_alarm(
  680. device_class: type[SwitchbotMeterPro], response: str, expected: dict[str, object]
  681. ):
  682. device = create_device(device_class)
  683. device._send_command.return_value = bytes.fromhex(response)
  684. assert await device.get_temperature_humidity_alarm() == expected
  685. device._send_command.assert_awaited_once_with("570f69020100")
  686. @pytest.mark.asyncio
  687. @pytest.mark.parametrize(
  688. ("response", "temperature_status", "humidity_status"),
  689. [
  690. (
  691. "0122963695000a20",
  692. MeterProTemperatureHumidityAlarmStatus.HIGH,
  693. MeterProTemperatureHumidityAlarmStatus.NONE,
  694. ),
  695. (
  696. "0122a32795000a02",
  697. MeterProTemperatureHumidityAlarmStatus.NONE,
  698. MeterProTemperatureHumidityAlarmStatus.HIGH,
  699. ),
  700. (
  701. "0122972735001822",
  702. MeterProTemperatureHumidityAlarmStatus.HIGH,
  703. MeterProTemperatureHumidityAlarmStatus.HIGH,
  704. ),
  705. (
  706. "0122963695000aec",
  707. MeterProTemperatureHumidityAlarmStatus.HIGH,
  708. MeterProTemperatureHumidityAlarmStatus.NONE,
  709. ),
  710. (
  711. "0122a32795000ace",
  712. MeterProTemperatureHumidityAlarmStatus.NONE,
  713. MeterProTemperatureHumidityAlarmStatus.HIGH,
  714. ),
  715. (
  716. "014297a7450094ff",
  717. MeterProTemperatureHumidityAlarmStatus.IN_RANGE,
  718. MeterProTemperatureHumidityAlarmStatus.IN_RANGE,
  719. ),
  720. ],
  721. )
  722. async def test_get_temperature_humidity_alarm_status(
  723. response: str,
  724. temperature_status: MeterProTemperatureHumidityAlarmStatus,
  725. humidity_status: MeterProTemperatureHumidityAlarmStatus,
  726. ):
  727. device = create_device()
  728. device._send_command.return_value = bytes.fromhex(response)
  729. result = await device.get_temperature_humidity_alarm()
  730. assert result["temperature_status"] is temperature_status
  731. assert result["humidity_status"] is humidity_status
  732. @pytest.mark.asyncio
  733. @pytest.mark.parametrize(
  734. ("mode", "limits", "payload"),
  735. [
  736. (MeterProTemperatureHumidityAlarmMode.DISABLED, (0, 0, 0, 0), "008000008000"),
  737. (
  738. MeterProTemperatureHumidityAlarmMode.TEMPERATURE_HIGH
  739. | MeterProTemperatureHumidityAlarmMode.HUMIDITY_HIGH,
  740. (-0.5, 23.34, 20, 39),
  741. "229727350014",
  742. ),
  743. (
  744. MeterProTemperatureHumidityAlarmMode.TEMPERATURE_IN_RANGE
  745. | MeterProTemperatureHumidityAlarmMode.HUMIDITY_IN_RANGE,
  746. (0.5, 0.5, 0, 99),
  747. "448063558000",
  748. ),
  749. ],
  750. )
  751. async def test_set_temperature_humidity_alarm(
  752. mode: MeterProTemperatureHumidityAlarmMode,
  753. limits: tuple[float, float, int, int],
  754. payload: str,
  755. ):
  756. device = create_device()
  757. await device.set_temperature_humidity_alarm(mode, *limits)
  758. device._send_command.assert_awaited_once_with("570f680201" + payload)
  759. @pytest.mark.asyncio
  760. @pytest.mark.parametrize(
  761. ("mode", "limits", "message"),
  762. [
  763. (True, (18, 28, 30, 70), "Invalid temperature and humidity alarm mode"),
  764. (
  765. MeterProTemperatureHumidityAlarmMode(0x88),
  766. (18, 28, 30, 70),
  767. "Invalid temperature and humidity alarm mode",
  768. ),
  769. (
  770. MeterProTemperatureHumidityAlarmMode(0x08),
  771. (18, 28, 30, 70),
  772. "Invalid temperature and humidity alarm mode",
  773. ),
  774. (
  775. MeterProTemperatureHumidityAlarmMode.DISABLED,
  776. (-128, 28, 30, 70),
  777. "Temperature must be",
  778. ),
  779. (
  780. MeterProTemperatureHumidityAlarmMode.DISABLED,
  781. (18, 28, True, 70),
  782. "Humidity limits",
  783. ),
  784. (
  785. MeterProTemperatureHumidityAlarmMode.DISABLED,
  786. (18, 28, 30, 100),
  787. "Humidity limits",
  788. ),
  789. (
  790. MeterProTemperatureHumidityAlarmMode.TEMPERATURE_IN_RANGE,
  791. (28, 18, 30, 70),
  792. "Lower in-range alarm limits",
  793. ),
  794. (
  795. MeterProTemperatureHumidityAlarmMode.HUMIDITY_IN_RANGE,
  796. (18, 28, 70, 30),
  797. "Lower in-range alarm limits",
  798. ),
  799. ],
  800. )
  801. async def test_set_temperature_humidity_alarm_rejects_invalid_inputs(
  802. mode: object, limits: tuple[float, float, int, int], message: str
  803. ):
  804. device = create_device()
  805. with pytest.raises(SwitchbotOperationError, match=message):
  806. await device.set_temperature_humidity_alarm(mode, *limits)
  807. device._send_command.assert_not_awaited()
  808. @pytest.mark.asyncio
  809. @pytest.mark.parametrize(
  810. "response",
  811. [
  812. "02",
  813. "01000000000000",
  814. "01009c460a921e00",
  815. "01009c6400921e00",
  816. ],
  817. )
  818. async def test_get_temperature_humidity_alarm_rejects_invalid_response(response: str):
  819. device = create_device()
  820. device._send_command.return_value = bytes.fromhex(response)
  821. with pytest.raises(SwitchbotOperationError):
  822. await device.get_temperature_humidity_alarm()
  823. @pytest.mark.asyncio
  824. async def test_set_temperature_humidity_alarm_rejects_device_error():
  825. device = create_device()
  826. device._send_command.return_value = b"\x02"
  827. with pytest.raises(SwitchbotOperationError, match="set_temperature_humidity_alarm"):
  828. await device.set_temperature_humidity_alarm(
  829. MeterProTemperatureHumidityAlarmMode.DISABLED, 0, 0, 0, 0
  830. )