test_meter_pro_settings.py 34 KB

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