test_meter_pro_settings.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. MeterProDateFormat,
  7. MeterProDisplaySource,
  8. MeterProDisplayZone,
  9. MeterProZoneIcon,
  10. SwitchbotOperationError,
  11. )
  12. from switchbot.devices.meter_pro import SwitchbotMeterPro, SwitchbotMeterProCO2
  13. def create_device(cls=SwitchbotMeterPro):
  14. ble_device = BLEDevice(
  15. address="aa:bb:cc:dd:ee:ff", name="any", details={"rssi": -80}
  16. )
  17. device = cls(ble_device)
  18. device._send_command = AsyncMock(return_value=bytes.fromhex("01"))
  19. return device
  20. # Captured from a Meter Pro (firmware 1.9): middle zone shows this device with
  21. # the indoor icon, bottom zone shows an Indoor/Outdoor Meter with the outdoor
  22. # icon, data update every 600 s, DD/MM, battery icon always shown.
  23. DISPLAY_SETTINGS = "011253000000000000d4e5f6a1b2c30258010101"
  24. @pytest.mark.asyncio
  25. async def test_get_display_settings():
  26. device = create_device()
  27. device._send_command.return_value = bytes.fromhex(DISPLAY_SETTINGS)
  28. settings = await device.get_display_settings()
  29. device._send_command.assert_called_with("570f690701")
  30. assert settings == {
  31. "middle": {
  32. "icon": MeterProZoneIcon.INDOOR,
  33. "source": MeterProDisplaySource.METER_PRO,
  34. "source_mac": None,
  35. },
  36. "bottom": {
  37. "icon": MeterProZoneIcon.OUTDOOR,
  38. "source": MeterProDisplaySource.INDOOR_OUTDOOR_METER,
  39. "source_mac": "D4:E5:F6:A1:B2:C3",
  40. },
  41. "data_update_interval": 600,
  42. "date_format": MeterProDateFormat.DAY_MONTH,
  43. "battery_icon_always": True,
  44. }
  45. @pytest.mark.asyncio
  46. async def test_get_display_settings_factory_state():
  47. """Factory layout: unused bottom zone, MM/DD, battery icon only when low."""
  48. device = create_device()
  49. device._send_command.return_value = bytes.fromhex(
  50. "0110500000000000000000000000000258000100"
  51. )
  52. settings = await device.get_display_settings()
  53. assert settings["bottom"] == {
  54. "icon": None,
  55. "source": MeterProDisplaySource.NONE,
  56. "source_mac": None,
  57. }
  58. assert settings["date_format"] is MeterProDateFormat.MONTH_DAY
  59. assert settings["battery_icon_always"] is False
  60. @pytest.mark.asyncio
  61. @pytest.mark.parametrize(
  62. ("date_code", "battery_code", "date_format", "battery_icon_always"),
  63. [
  64. (0x02, 0x00, None, False),
  65. (0x00, 0xFE, MeterProDateFormat.MONTH_DAY, None),
  66. (0xFF, 0xFF, None, None),
  67. ],
  68. )
  69. async def test_get_display_settings_unknown_values(
  70. date_code: int,
  71. battery_code: int,
  72. date_format: MeterProDateFormat | None,
  73. battery_icon_always: bool | None,
  74. ):
  75. device = create_device()
  76. response = bytearray.fromhex(DISPLAY_SETTINGS)
  77. response[17] = date_code
  78. response[19] = battery_code
  79. device._send_command.return_value = bytes(response)
  80. settings = await device.get_display_settings()
  81. assert settings["date_format"] is date_format
  82. assert settings["battery_icon_always"] is battery_icon_always
  83. assert settings["data_update_interval"] == 600
  84. @pytest.mark.asyncio
  85. async def test_get_display_settings_short_response():
  86. """Older firmware may stop after the MAC addresses."""
  87. device = create_device()
  88. device._send_command.return_value = bytes.fromhex(DISPLAY_SETTINGS[:30])
  89. settings = await device.get_display_settings()
  90. assert settings["bottom"]["source_mac"] == "D4:E5:F6:A1:B2:C3"
  91. assert settings["data_update_interval"] is None
  92. assert settings["date_format"] is None
  93. assert settings["battery_icon_always"] is None
  94. @pytest.mark.asyncio
  95. async def test_get_display_settings_unknown_source_kept_raw():
  96. device = create_device()
  97. device._send_command.return_value = bytes.fromhex(
  98. "011257000000000000aabbccddeeff0258010101"
  99. )
  100. settings = await device.get_display_settings()
  101. assert settings["bottom"]["source"] == 7
  102. @pytest.mark.asyncio
  103. async def test_get_display_settings_weather_station_and_hub3():
  104. device = create_device()
  105. device._send_command.return_value = bytes.fromhex(
  106. "011289aabbccddee01aabbccddee020258010101"
  107. )
  108. settings = await device.get_display_settings()
  109. assert settings["middle"]["source"] is MeterProDisplaySource.WEATHER_STATION
  110. assert settings["bottom"]["source"] is MeterProDisplaySource.HUB3
  111. @pytest.mark.asyncio
  112. @pytest.mark.parametrize(
  113. "response",
  114. ["02", "011253000000"], # error status, then response too short
  115. )
  116. async def test_get_display_settings_failure(response: str):
  117. device = create_device()
  118. device._send_command.return_value = bytes.fromhex(response)
  119. with pytest.raises(SwitchbotOperationError):
  120. await device.get_display_settings()
  121. @pytest.mark.asyncio
  122. @pytest.mark.parametrize(
  123. ("zone", "source", "mac", "icon", "expected_payload"),
  124. [
  125. (
  126. MeterProDisplayZone.BOTTOM,
  127. MeterProDisplaySource.INDOOR_OUTDOOR_METER,
  128. "D4:E5:F6:A1:B2:C3",
  129. MeterProZoneIcon.OUTDOOR,
  130. "570f680701020203d4e5f6a1b2c3",
  131. ),
  132. (
  133. MeterProDisplayZone.BOTTOM,
  134. MeterProDisplaySource.NETWORK,
  135. None,
  136. MeterProZoneIcon.OUTDOOR,
  137. "570f680701020206000000000000",
  138. ),
  139. (
  140. MeterProDisplayZone.BOTTOM,
  141. MeterProDisplaySource.NONE,
  142. None,
  143. None,
  144. "570f68070102ff00000000000000",
  145. ),
  146. (
  147. MeterProDisplayZone.MIDDLE,
  148. MeterProDisplaySource.METER_PRO,
  149. None,
  150. MeterProZoneIcon.INDOOR,
  151. "570f680701010105000000000000",
  152. ),
  153. (
  154. MeterProDisplayZone.MIDDLE,
  155. MeterProDisplaySource.METER_PLUS,
  156. "aa-bb-cc-dd-ee-ff",
  157. None,
  158. "570f68070101ff02aabbccddeeff",
  159. ),
  160. ],
  161. )
  162. async def test_set_display_zone(
  163. zone: MeterProDisplayZone,
  164. source: MeterProDisplaySource,
  165. mac: str | None,
  166. icon: MeterProZoneIcon | None,
  167. expected_payload: str,
  168. ):
  169. device = create_device()
  170. await device.set_display_zone(zone, source, mac, icon)
  171. device._send_command.assert_called_with(expected_payload)
  172. @pytest.mark.asyncio
  173. @pytest.mark.parametrize(
  174. ("source", "mac"),
  175. [
  176. (MeterProDisplaySource.METER, None), # needs a MAC
  177. (MeterProDisplaySource.NETWORK, "aa:bb:cc:dd:ee:ff"), # takes none
  178. (MeterProDisplaySource.NONE, "aa:bb:cc:dd:ee:ff"), # takes none
  179. (MeterProDisplaySource.METER, "aa:bb:cc"), # malformed
  180. (MeterProDisplaySource.METER, "zz:bb:cc:dd:ee:ff"), # malformed
  181. ],
  182. )
  183. async def test_set_display_zone_invalid(source: MeterProDisplaySource, mac: str):
  184. device = create_device()
  185. with pytest.raises(SwitchbotOperationError):
  186. await device.set_display_zone(MeterProDisplayZone.BOTTOM, source, mac)
  187. device._send_command.assert_not_called()
  188. @pytest.mark.asyncio
  189. @pytest.mark.parametrize(
  190. ("middle", "bottom", "expected_payload"),
  191. [
  192. (MeterProZoneIcon.INDOOR, MeterProZoneIcon.OUTDOOR, "570f680701030102"),
  193. (None, MeterProZoneIcon.INDOOR, "570f680701030f01"),
  194. (MeterProZoneIcon.OUTDOOR, None, "570f68070103020f"),
  195. ],
  196. )
  197. async def test_set_zone_icons(
  198. middle: MeterProZoneIcon | None,
  199. bottom: MeterProZoneIcon | None,
  200. expected_payload: str,
  201. ):
  202. device = create_device()
  203. await device.set_zone_icons(middle, bottom)
  204. device._send_command.assert_called_with(expected_payload)
  205. @pytest.mark.asyncio
  206. @pytest.mark.parametrize(
  207. ("seconds", "expected_payload"),
  208. [(300, "570f68070105012c"), (600, "570f680701050258"), (1800, "570f680701050708")],
  209. )
  210. async def test_set_data_update_interval(seconds: int, expected_payload: str):
  211. device = create_device()
  212. await device.set_data_update_interval(seconds)
  213. device._send_command.assert_called_with(expected_payload)
  214. @pytest.mark.asyncio
  215. @pytest.mark.parametrize("seconds", [0, -1, 0x10000, True, 300.0, "300"])
  216. async def test_set_data_update_interval_invalid(seconds: object):
  217. device = create_device()
  218. with pytest.raises(SwitchbotOperationError):
  219. await device.set_data_update_interval(seconds)
  220. device._send_command.assert_not_called()
  221. @pytest.mark.asyncio
  222. @pytest.mark.parametrize(
  223. ("date_format", "expected_payload"),
  224. [
  225. (MeterProDateFormat.MONTH_DAY, "570f6807010700"),
  226. (MeterProDateFormat.DAY_MONTH, "570f6807010701"),
  227. ],
  228. )
  229. async def test_set_date_format(date_format: MeterProDateFormat, expected_payload: str):
  230. device = create_device()
  231. await device.set_date_format(date_format)
  232. device._send_command.assert_called_with(expected_payload)
  233. @pytest.mark.asyncio
  234. @pytest.mark.parametrize(
  235. ("always", "expected_payload"),
  236. [(True, "570f6807010801"), (False, "570f6807010800")],
  237. )
  238. async def test_set_battery_icon(always: bool, expected_payload: str):
  239. device = create_device()
  240. await device.set_battery_icon(always)
  241. device._send_command.assert_called_with(expected_payload)
  242. @pytest.mark.asyncio
  243. @pytest.mark.parametrize(
  244. ("fahrenheit", "expected_payload"),
  245. [(True, "570f6807010901"), (False, "570f6807010900")],
  246. )
  247. async def test_set_temperature_unit(fahrenheit: bool, expected_payload: str):
  248. device = create_device()
  249. await device.set_temperature_unit(fahrenheit)
  250. device._send_command.assert_called_with(expected_payload)
  251. @pytest.mark.asyncio
  252. @pytest.mark.parametrize(
  253. ("response", "expected"),
  254. [
  255. ("010101", {"unit_switch": False, "source_switch": True}),
  256. ("010000", {"unit_switch": True, "source_switch": False}),
  257. ("01ff01", {"unit_switch": None, "source_switch": True}),
  258. ("0100ff", {"unit_switch": True, "source_switch": None}),
  259. ("01ffff", {"unit_switch": None, "source_switch": None}),
  260. ("010202", {"unit_switch": None, "source_switch": None}),
  261. ],
  262. )
  263. async def test_get_button_functions(response: str, expected: dict[str, bool | None]):
  264. device = create_device()
  265. device._send_command.return_value = bytes.fromhex(response)
  266. assert await device.get_button_functions() == expected
  267. device._send_command.assert_called_with("570f690704")
  268. @pytest.mark.asyncio
  269. @pytest.mark.parametrize(
  270. ("unit_switch", "source_switch", "expected_payload"),
  271. [
  272. (True, False, "570f680701060000"),
  273. (False, True, "570f680701060101"),
  274. (None, True, "570f68070106ff01"),
  275. (False, None, "570f6807010601ff"),
  276. ],
  277. )
  278. async def test_set_button_functions(
  279. unit_switch: bool | None, source_switch: bool | None, expected_payload: str
  280. ):
  281. device = create_device()
  282. await device.set_button_functions(unit_switch, source_switch)
  283. device._send_command.assert_called_with(expected_payload)
  284. @pytest.mark.asyncio
  285. @pytest.mark.parametrize(
  286. ("response", "temperature", "humidity"),
  287. [
  288. ("01000000", 0.0, 0), # factory state: sign bits cleared
  289. ("01808000", 0.0, 0),
  290. ("01018305", -1.5, 3),
  291. ("01820a03", 2.3, -10),
  292. ("01800005", 0.5, 0),
  293. ],
  294. )
  295. async def test_get_calibration(response: str, temperature: float, humidity: int):
  296. device = create_device()
  297. device._send_command.return_value = bytes.fromhex(response)
  298. calibration = await device.get_calibration()
  299. device._send_command.assert_called_with("570f6903")
  300. assert calibration == {"temperature": temperature, "humidity": humidity}
  301. assert str(calibration["temperature"]) != "-0.0"
  302. @pytest.mark.asyncio
  303. @pytest.mark.parametrize("response", ["0180800a", "0180e400", "01806400"])
  304. async def test_get_calibration_rejects_invalid_values(response: str):
  305. device = create_device()
  306. device._send_command.return_value = bytes.fromhex(response)
  307. with pytest.raises(SwitchbotOperationError, match="Invalid calibration response"):
  308. await device.get_calibration()
  309. @pytest.mark.asyncio
  310. async def test_get_calibration_short_response():
  311. device = create_device()
  312. device._send_command.return_value = bytes.fromhex("0101")
  313. with pytest.raises(SwitchbotOperationError):
  314. await device.get_calibration()
  315. @pytest.mark.asyncio
  316. @pytest.mark.parametrize(
  317. ("temperature", "humidity", "expected_payload"),
  318. [
  319. (0, 0, "570f6803808000"),
  320. (-1.5, 3, "570f6803018305"),
  321. (2.3, -10, "570f6803820a03"),
  322. (0.5, 0, "570f6803808005"), # positive below one degree keeps its sign
  323. (-0.5, 0, "570f6803008005"),
  324. (1.25, 0, "570f6803818003"), # rounded to one decimal
  325. ],
  326. )
  327. async def test_set_calibration(
  328. temperature: float, humidity: int, expected_payload: str
  329. ):
  330. device = create_device()
  331. await device.set_calibration(temperature, humidity)
  332. device._send_command.assert_called_with(expected_payload)
  333. @pytest.mark.asyncio
  334. @pytest.mark.parametrize(
  335. ("temperature", "humidity"),
  336. [(128, 0), (-128, 0), (0, 100), (0, -100), (0, True), (0, 0.5), (0, "1")],
  337. )
  338. async def test_set_calibration_invalid(temperature: float, humidity: object):
  339. device = create_device()
  340. with pytest.raises(SwitchbotOperationError):
  341. await device.set_calibration(temperature, humidity)
  342. device._send_command.assert_not_called()
  343. @pytest.mark.asyncio
  344. @pytest.mark.parametrize(
  345. ("method", "args"),
  346. [
  347. ("set_calibration", (1.0, 1)),
  348. ("set_display_zone", (MeterProDisplayZone.BOTTOM, MeterProDisplaySource.NONE)),
  349. ("set_zone_icons", (MeterProZoneIcon.INDOOR,)),
  350. ("set_data_update_interval", (600,)),
  351. ("set_date_format", (MeterProDateFormat.DAY_MONTH,)),
  352. ("set_battery_icon", (True,)),
  353. ("set_temperature_unit", (False,)),
  354. ("set_button_functions", (True, True)),
  355. ],
  356. )
  357. async def test_setters_raise_on_device_error(method: str, args: tuple):
  358. device = create_device()
  359. device._send_command.return_value = bytes.fromhex("05") # not supported
  360. with pytest.raises(SwitchbotOperationError):
  361. await getattr(device, method)(*args)
  362. @pytest.mark.asyncio
  363. @pytest.mark.parametrize(
  364. ("method", "args", "expected_payload"),
  365. [
  366. ("set_calibration", (1.0, 1), "570f6803818100"),
  367. (
  368. "set_display_zone",
  369. (MeterProDisplayZone.BOTTOM, MeterProDisplaySource.NETWORK),
  370. "570f68070102ff06000000000000",
  371. ),
  372. ("set_zone_icons", (MeterProZoneIcon.INDOOR,), "570f68070103010f"),
  373. ("set_data_update_interval", (600,), "570f680701050258"),
  374. ("set_date_format", (MeterProDateFormat.DAY_MONTH,), "570f6807010701"),
  375. ("set_battery_icon", (True,), "570f6807010801"),
  376. ("set_temperature_unit", (False,), "570f6807010900"),
  377. ("set_button_functions", (True, True), "570f680701060001"),
  378. ],
  379. )
  380. async def test_co2_variant_shares_display_settings(
  381. method: str, args: tuple, expected_payload: str
  382. ):
  383. device = create_device(SwitchbotMeterProCO2)
  384. await getattr(device, method)(*args)
  385. device._send_command.assert_called_with(expected_payload)