test_lock.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. import logging
  2. from unittest.mock import AsyncMock, Mock, patch
  3. import pytest
  4. from switchbot import SwitchbotModel
  5. from switchbot.const.lock import LockStatus
  6. from switchbot.devices import lock
  7. from .test_adv_parser import generate_ble_device
  8. def create_device_for_command_testing(model: str):
  9. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  10. return lock.SwitchbotLock(
  11. ble_device, "ff", "ffffffffffffffffffffffffffffffff", model=model
  12. )
  13. @pytest.mark.parametrize(
  14. "model",
  15. [
  16. SwitchbotModel.LOCK,
  17. SwitchbotModel.LOCK_LITE,
  18. SwitchbotModel.LOCK_PRO,
  19. SwitchbotModel.LOCK_ULTRA,
  20. ],
  21. )
  22. def test_lock_init(model: str):
  23. """Test the initialization of the lock device."""
  24. device = create_device_for_command_testing(model)
  25. assert device._model == model
  26. @pytest.mark.parametrize(
  27. "model",
  28. [
  29. SwitchbotModel.AIR_PURIFIER,
  30. ],
  31. )
  32. def test_lock_init_with_invalid_model(model: str):
  33. """Test that initializing with an invalid model raises ValueError."""
  34. with pytest.raises(
  35. ValueError, match="initializing SwitchbotLock with a non-lock model"
  36. ):
  37. create_device_for_command_testing(model)
  38. @pytest.mark.asyncio
  39. @pytest.mark.parametrize(
  40. "model",
  41. [
  42. SwitchbotModel.LOCK,
  43. SwitchbotModel.LOCK_LITE,
  44. SwitchbotModel.LOCK_PRO,
  45. SwitchbotModel.LOCK_ULTRA,
  46. ],
  47. )
  48. async def test_verify_encryption_key(model: str):
  49. """Test verify_encryption_key method."""
  50. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  51. with patch("switchbot.devices.lock.super") as mock_super:
  52. mock_super().verify_encryption_key = AsyncMock(return_value=True)
  53. result = await lock.SwitchbotLock.verify_encryption_key(
  54. ble_device, "key_id", "encryption_key", model
  55. )
  56. assert result is True
  57. @pytest.mark.asyncio
  58. @pytest.mark.parametrize(
  59. ("model", "command"),
  60. [
  61. (SwitchbotModel.LOCK, b"W\x0fN\x01\x01\x10\x80"),
  62. (SwitchbotModel.LOCK_LITE, b"W\x0fN\x01\x01\x10\x81"),
  63. (SwitchbotModel.LOCK_PRO, b"W\x0fN\x01\x01\x10\x85"),
  64. (SwitchbotModel.LOCK_ULTRA, b"W\x0fN\x01\x01\x10\x86"),
  65. ],
  66. )
  67. async def test_lock(model: str, command: bytes):
  68. """Test lock method."""
  69. device = create_device_for_command_testing(model)
  70. device._get_adv_value = Mock(return_value=LockStatus.UNLOCKED)
  71. with (
  72. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  73. patch.object(device, "_enable_notifications", return_value=True),
  74. patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
  75. ):
  76. result = await device.lock()
  77. assert result is True
  78. @pytest.mark.asyncio
  79. @pytest.mark.parametrize(
  80. ("model", "command"),
  81. [
  82. (SwitchbotModel.LOCK, b"W\x0fN\x01\x01\x10\x80"),
  83. (SwitchbotModel.LOCK_LITE, b"W\x0fN\x01\x01\x10\x81"),
  84. (SwitchbotModel.LOCK_PRO, b"W\x0fN\x01\x01\x10\x84"),
  85. (SwitchbotModel.LOCK_ULTRA, b"W\x0fN\x01\x01\x10\x83"),
  86. ],
  87. )
  88. async def test_unlock(model: str, command: bytes):
  89. """Test unlock method."""
  90. device = create_device_for_command_testing(model)
  91. device._get_adv_value = Mock(return_value=LockStatus.LOCKED)
  92. with (
  93. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  94. patch.object(device, "_enable_notifications", return_value=True),
  95. patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
  96. ):
  97. result = await device.unlock()
  98. assert result is True
  99. @pytest.mark.asyncio
  100. @pytest.mark.parametrize(
  101. "model",
  102. [
  103. SwitchbotModel.LOCK,
  104. SwitchbotModel.LOCK_LITE,
  105. SwitchbotModel.LOCK_PRO,
  106. SwitchbotModel.LOCK_ULTRA,
  107. ],
  108. )
  109. async def test_unlock_without_unlatch(model: str):
  110. """Test unlock_without_unlatch method."""
  111. device = create_device_for_command_testing(model)
  112. device._get_adv_value = Mock(return_value=LockStatus.LOCKED)
  113. with (
  114. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  115. patch.object(device, "_enable_notifications", return_value=True),
  116. patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
  117. ):
  118. result = await device.unlock_without_unlatch()
  119. assert result is True
  120. @pytest.mark.asyncio
  121. @pytest.mark.parametrize(
  122. "model",
  123. [
  124. SwitchbotModel.LOCK,
  125. SwitchbotModel.LOCK_LITE,
  126. SwitchbotModel.LOCK_PRO,
  127. SwitchbotModel.LOCK_ULTRA,
  128. ],
  129. )
  130. async def test_get_basic_info(model: str):
  131. """Test get_basic_info method."""
  132. device = create_device_for_command_testing(model)
  133. lock_data = b"\x00\x80\x00\x00\x00\x00\x00\x00"
  134. basic_data = b"\x00\x64\x01"
  135. with (
  136. patch.object(device, "_get_lock_info", return_value=lock_data),
  137. patch.object(device, "_get_basic_info", return_value=basic_data),
  138. ):
  139. result = await device.get_basic_info()
  140. assert result is not None
  141. assert "battery" in result
  142. assert "firmware" in result
  143. assert "calibration" in result
  144. assert "status" in result
  145. @pytest.mark.asyncio
  146. @pytest.mark.parametrize(
  147. "model",
  148. [
  149. SwitchbotModel.LOCK,
  150. SwitchbotModel.LOCK_LITE,
  151. SwitchbotModel.LOCK_PRO,
  152. SwitchbotModel.LOCK_ULTRA,
  153. ],
  154. )
  155. async def test_get_basic_info_no_lock_data(model: str):
  156. """Test get_basic_info when no lock data is returned."""
  157. device = create_device_for_command_testing(model)
  158. with patch.object(device, "_get_lock_info", return_value=None):
  159. result = await device.get_basic_info()
  160. assert result is None
  161. @pytest.mark.asyncio
  162. @pytest.mark.parametrize(
  163. "model",
  164. [
  165. SwitchbotModel.LOCK,
  166. SwitchbotModel.LOCK_LITE,
  167. SwitchbotModel.LOCK_PRO,
  168. SwitchbotModel.LOCK_ULTRA,
  169. ],
  170. )
  171. async def test_get_basic_info_no_basic_data(model: str):
  172. """Test get_basic_info when no basic data is returned."""
  173. device = create_device_for_command_testing(model)
  174. lock_data = b"\x00\x80\x00\x00\x00\x00\x00\x00"
  175. with (
  176. patch.object(device, "_get_lock_info", return_value=lock_data),
  177. patch.object(device, "_get_basic_info", return_value=None),
  178. ):
  179. result = await device.get_basic_info()
  180. assert result is None
  181. def test_parse_basic_data():
  182. """Test _parse_basic_data method."""
  183. device = create_device_for_command_testing(SwitchbotModel.LOCK)
  184. basic_data = b"\x00\x64\x01"
  185. result = device._parse_basic_data(basic_data)
  186. assert result["battery"] == 100
  187. assert result["firmware"] == 0.1
  188. @pytest.mark.parametrize(
  189. "model",
  190. [
  191. SwitchbotModel.LOCK,
  192. SwitchbotModel.LOCK_LITE,
  193. SwitchbotModel.LOCK_PRO,
  194. SwitchbotModel.LOCK_ULTRA,
  195. ],
  196. )
  197. def test_is_calibrated(model: str):
  198. """Test is_calibrated method."""
  199. device = create_device_for_command_testing(model)
  200. device._get_adv_value = Mock(return_value=True)
  201. assert device.is_calibrated() is True
  202. @pytest.mark.parametrize(
  203. "model",
  204. [
  205. SwitchbotModel.LOCK,
  206. SwitchbotModel.LOCK_LITE,
  207. SwitchbotModel.LOCK_PRO,
  208. SwitchbotModel.LOCK_ULTRA,
  209. ],
  210. )
  211. def test_get_lock_status(model: str):
  212. """Test get_lock_status method."""
  213. device = create_device_for_command_testing(model)
  214. device._get_adv_value = Mock(return_value=LockStatus.LOCKED)
  215. assert device.get_lock_status() == LockStatus.LOCKED
  216. @pytest.mark.parametrize(
  217. "model",
  218. [
  219. SwitchbotModel.LOCK,
  220. SwitchbotModel.LOCK_PRO,
  221. SwitchbotModel.LOCK_ULTRA,
  222. ],
  223. )
  224. def test_is_door_open(model: str):
  225. """Test is_door_open method."""
  226. device = create_device_for_command_testing(model)
  227. device._get_adv_value = Mock(return_value=True)
  228. assert device.is_door_open() is True
  229. @pytest.mark.parametrize(
  230. "model",
  231. [
  232. SwitchbotModel.LOCK,
  233. SwitchbotModel.LOCK_PRO,
  234. SwitchbotModel.LOCK_ULTRA,
  235. ],
  236. )
  237. def test_is_unclosed_alarm_on(model: str):
  238. """Test is_unclosed_alarm_on method."""
  239. device = create_device_for_command_testing(model)
  240. device._get_adv_value = Mock(return_value=True)
  241. assert device.is_unclosed_alarm_on() is True
  242. @pytest.mark.parametrize(
  243. "model",
  244. [
  245. SwitchbotModel.LOCK,
  246. SwitchbotModel.LOCK_LITE,
  247. SwitchbotModel.LOCK_PRO,
  248. SwitchbotModel.LOCK_ULTRA,
  249. ],
  250. )
  251. def test_is_unlocked_alarm_on(model: str):
  252. """Test is_unlocked_alarm_on method."""
  253. device = create_device_for_command_testing(model)
  254. device._get_adv_value = Mock(return_value=True)
  255. assert device.is_unlocked_alarm_on() is True
  256. @pytest.mark.parametrize(
  257. "model",
  258. [
  259. SwitchbotModel.LOCK,
  260. ],
  261. )
  262. def test_is_auto_lock_paused(model: str):
  263. """Test is_auto_lock_paused method."""
  264. device = create_device_for_command_testing(model)
  265. device._get_adv_value = Mock(return_value=True)
  266. assert device.is_auto_lock_paused() is True
  267. @pytest.mark.parametrize(
  268. "model",
  269. [
  270. SwitchbotModel.LOCK,
  271. SwitchbotModel.LOCK_LITE,
  272. SwitchbotModel.LOCK_PRO,
  273. SwitchbotModel.LOCK_ULTRA,
  274. ],
  275. )
  276. def test_is_night_latch_enabled(model: str):
  277. """Test is_night_latch_enabled method."""
  278. device = create_device_for_command_testing(model)
  279. device._get_adv_value = Mock(return_value=True)
  280. assert device.is_night_latch_enabled() is True
  281. @pytest.mark.asyncio
  282. @pytest.mark.parametrize(
  283. "model",
  284. [
  285. SwitchbotModel.LOCK,
  286. SwitchbotModel.LOCK_LITE,
  287. SwitchbotModel.LOCK_PRO,
  288. SwitchbotModel.LOCK_ULTRA,
  289. ],
  290. )
  291. async def test_get_lock_info(model: str):
  292. """Test _get_lock_info method."""
  293. device = create_device_for_command_testing(model)
  294. expected_data = b"\x01\x00\x80\x00\x00\x00\x00\x00"
  295. with patch.object(device, "_send_command", return_value=expected_data):
  296. result = await device._get_lock_info()
  297. assert result == expected_data
  298. @pytest.mark.asyncio
  299. @pytest.mark.parametrize(
  300. "model",
  301. [
  302. SwitchbotModel.LOCK,
  303. SwitchbotModel.LOCK_LITE,
  304. SwitchbotModel.LOCK_PRO,
  305. SwitchbotModel.LOCK_ULTRA,
  306. ],
  307. )
  308. async def test_get_lock_info_failure(model: str):
  309. """Test _get_lock_info method when command fails."""
  310. device = create_device_for_command_testing(model)
  311. with patch.object(device, "_send_command", return_value=b"\x00\x00"):
  312. result = await device._get_lock_info()
  313. assert result is None
  314. @pytest.mark.asyncio
  315. @pytest.mark.parametrize(
  316. "model",
  317. [
  318. SwitchbotModel.LOCK,
  319. SwitchbotModel.LOCK_LITE,
  320. SwitchbotModel.LOCK_PRO,
  321. SwitchbotModel.LOCK_ULTRA,
  322. ],
  323. )
  324. async def test_enable_notifications(model: str):
  325. """Test _enable_notifications method."""
  326. device = create_device_for_command_testing(model)
  327. with patch.object(device, "_send_command", return_value=b"\x01\x00"):
  328. result = await device._enable_notifications()
  329. assert result is True
  330. assert device._notifications_enabled is True
  331. @pytest.mark.asyncio
  332. @pytest.mark.parametrize(
  333. "model",
  334. [
  335. SwitchbotModel.LOCK,
  336. SwitchbotModel.LOCK_LITE,
  337. SwitchbotModel.LOCK_PRO,
  338. SwitchbotModel.LOCK_ULTRA,
  339. ],
  340. )
  341. async def test_enable_notifications_already_enabled(model: str):
  342. """Test _enable_notifications when already enabled."""
  343. device = create_device_for_command_testing(model)
  344. device._notifications_enabled = True
  345. with patch.object(device, "_send_command") as mock_send:
  346. result = await device._enable_notifications()
  347. assert result is True
  348. mock_send.assert_not_called()
  349. @pytest.mark.asyncio
  350. @pytest.mark.parametrize(
  351. "model",
  352. [
  353. SwitchbotModel.LOCK,
  354. SwitchbotModel.LOCK_LITE,
  355. SwitchbotModel.LOCK_PRO,
  356. SwitchbotModel.LOCK_ULTRA,
  357. ],
  358. )
  359. async def test_disable_notifications(model: str):
  360. """Test _disable_notifications method."""
  361. device = create_device_for_command_testing(model)
  362. device._notifications_enabled = True
  363. with patch.object(device, "_send_command", return_value=b"\x01\x00"):
  364. result = await device._disable_notifications()
  365. assert result is True
  366. assert device._notifications_enabled is False
  367. @pytest.mark.asyncio
  368. @pytest.mark.parametrize(
  369. "model",
  370. [
  371. SwitchbotModel.LOCK,
  372. SwitchbotModel.LOCK_LITE,
  373. SwitchbotModel.LOCK_PRO,
  374. SwitchbotModel.LOCK_ULTRA,
  375. ],
  376. )
  377. async def test_disable_notifications_already_disabled(model: str):
  378. """Test _disable_notifications when already disabled."""
  379. device = create_device_for_command_testing(model)
  380. device._notifications_enabled = False
  381. with patch.object(device, "_send_command") as mock_send:
  382. result = await device._disable_notifications()
  383. assert result is True
  384. mock_send.assert_not_called()
  385. @pytest.mark.parametrize(
  386. "model",
  387. [
  388. SwitchbotModel.LOCK,
  389. SwitchbotModel.LOCK_LITE,
  390. SwitchbotModel.LOCK_PRO,
  391. SwitchbotModel.LOCK_ULTRA,
  392. ],
  393. )
  394. def test_notification_handler(model: str):
  395. """Test _notification_handler method."""
  396. device = create_device_for_command_testing(model)
  397. device._notifications_enabled = True
  398. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  399. with patch.object(device, "_update_lock_status") as mock_update:
  400. device._notification_handler(0, data)
  401. mock_update.assert_called_once_with(data)
  402. @pytest.mark.parametrize(
  403. "model",
  404. [
  405. SwitchbotModel.LOCK,
  406. SwitchbotModel.LOCK_LITE,
  407. SwitchbotModel.LOCK_PRO,
  408. SwitchbotModel.LOCK_ULTRA,
  409. ],
  410. )
  411. def test_notification_handler_not_enabled(model: str):
  412. """Test _notification_handler when notifications not enabled."""
  413. device = create_device_for_command_testing(model)
  414. device._notifications_enabled = False
  415. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  416. with (
  417. patch.object(device, "_update_lock_status") as mock_update,
  418. patch.object(
  419. device.__class__.__bases__[0], "_notification_handler"
  420. ) as mock_super,
  421. ):
  422. device._notification_handler(0, data)
  423. mock_update.assert_not_called()
  424. mock_super.assert_called_once()
  425. @pytest.mark.parametrize(
  426. "model",
  427. [
  428. SwitchbotModel.LOCK,
  429. SwitchbotModel.LOCK_LITE,
  430. SwitchbotModel.LOCK_PRO,
  431. SwitchbotModel.LOCK_ULTRA,
  432. ],
  433. )
  434. def test_notification_handler_during_disconnect(
  435. model: str, caplog: pytest.LogCaptureFixture
  436. ) -> None:
  437. """Test _notification_handler during expected disconnect."""
  438. device = create_device_for_command_testing(model)
  439. device._notifications_enabled = True
  440. device._expected_disconnect = True
  441. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  442. with (
  443. patch.object(device, "_update_lock_status") as mock_update,
  444. caplog.at_level(logging.DEBUG),
  445. ):
  446. device._notification_handler(0, data)
  447. # Should not update lock status during disconnect
  448. mock_update.assert_not_called()
  449. # Should log debug message
  450. assert "Ignoring lock notification during expected disconnect" in caplog.text
  451. @pytest.mark.parametrize(
  452. "model",
  453. [
  454. SwitchbotModel.LOCK,
  455. SwitchbotModel.LOCK_LITE,
  456. SwitchbotModel.LOCK_PRO,
  457. SwitchbotModel.LOCK_ULTRA,
  458. ],
  459. )
  460. def test_update_lock_status(model: str):
  461. """Test _update_lock_status method."""
  462. device = create_device_for_command_testing(model)
  463. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  464. with (
  465. patch.object(device, "_decrypt", return_value=b"\x80\x00\x00\x00\x00\x00"),
  466. patch.object(device, "_update_parsed_data", return_value=True),
  467. patch.object(device, "_reset_disconnect_timer"),
  468. patch.object(device, "_fire_callbacks"),
  469. ):
  470. device._update_lock_status(data)
  471. @pytest.mark.parametrize(
  472. ("model", "data", "expected"),
  473. [
  474. (
  475. SwitchbotModel.LOCK,
  476. b"\x80\x00\x00\x00\x00\x00",
  477. {
  478. "calibration": True,
  479. "status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
  480. "door_open": False,
  481. "unclosed_alarm": False,
  482. "unlocked_alarm": False,
  483. },
  484. ),
  485. (
  486. SwitchbotModel.LOCK_LITE,
  487. b"\x80\x00\x00\x00\x00\x00",
  488. {
  489. "calibration": True,
  490. "status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
  491. "unlocked_alarm": False,
  492. },
  493. ),
  494. (
  495. SwitchbotModel.LOCK_PRO,
  496. b"\x80\x00\x00\x00\x00\x00",
  497. {
  498. "calibration": True,
  499. "status": LockStatus.LOCKED, # (0x80 & 0b01111000) >> 3 = 0 = LOCKED
  500. "door_open": False,
  501. "unclosed_alarm": False,
  502. "unlocked_alarm": False,
  503. },
  504. ),
  505. (
  506. SwitchbotModel.LOCK_ULTRA,
  507. b"\x88\x10\x00\x00\x00\xc0",
  508. {
  509. "calibration": True,
  510. "status": LockStatus.UNLOCKED, # (0x88 & 0b01111000) >> 3 = 0x08 >> 3 = 1 = UNLOCKED
  511. "door_open": True,
  512. "unclosed_alarm": True,
  513. "unlocked_alarm": True,
  514. },
  515. ),
  516. ],
  517. )
  518. def test_parse_lock_data(model: str, data: bytes, expected: dict):
  519. """Test _parse_lock_data static method."""
  520. result = lock.SwitchbotLock._parse_lock_data(data, model)
  521. assert result == expected
  522. @pytest.mark.parametrize(
  523. ("model", "data", "expected"),
  524. [
  525. # Test LOCK with different status bits and flags
  526. (
  527. SwitchbotModel.LOCK,
  528. b"\x94\x00\x00\x00\x00\x00", # Unlocked status (0x10 >> 4 = 1) with door open
  529. {
  530. "calibration": True,
  531. "status": LockStatus.UNLOCKED,
  532. "door_open": True,
  533. "unclosed_alarm": False,
  534. "unlocked_alarm": False,
  535. },
  536. ),
  537. # Test LOCK_LITE without door_open field
  538. (
  539. SwitchbotModel.LOCK_LITE,
  540. b"\x90\x10\x00\x00\x00\x00", # Unlocked with unlocked alarm
  541. {
  542. "calibration": True,
  543. "status": LockStatus.UNLOCKED,
  544. "unlocked_alarm": True,
  545. },
  546. ),
  547. # Test LOCK_PRO with new bit positions
  548. (
  549. SwitchbotModel.LOCK_PRO,
  550. b"\x90\x10\x00\x00\x00\xc0", # New format: status bits 3-6, door open bit 4 of byte 1
  551. {
  552. "calibration": True,
  553. "status": LockStatus.LOCKING, # (0x90 & 0b01111000) >> 3 = 0x10 >> 3 = 2 (LOCKING)
  554. "door_open": True, # bit 4 of byte 1 (0x10)
  555. "unclosed_alarm": True, # bit 7 of byte 5 (0xc0)
  556. "unlocked_alarm": True, # bit 6 of byte 5 (0xc0)
  557. },
  558. ),
  559. # Test LOCK_ULTRA with same format as PRO
  560. (
  561. SwitchbotModel.LOCK_ULTRA,
  562. b"\x88\x00\x00\x00\x00\x40", # Unlocked with unlocked alarm only
  563. {
  564. "calibration": True,
  565. "status": LockStatus.UNLOCKED, # (0x88 & 0b01111000) >> 3 = 0x08 >> 3 = 1
  566. "door_open": False,
  567. "unclosed_alarm": False,
  568. "unlocked_alarm": True, # bit 6 of byte 5
  569. },
  570. ),
  571. ],
  572. )
  573. def test_parse_lock_data_new_formats(model: str, data: bytes, expected: dict):
  574. """Test _parse_lock_data with new format changes."""
  575. result = lock.SwitchbotLock._parse_lock_data(data, model)
  576. assert result == expected
  577. @pytest.mark.asyncio
  578. @pytest.mark.parametrize(
  579. "model",
  580. [
  581. SwitchbotModel.LOCK,
  582. SwitchbotModel.LOCK_LITE,
  583. SwitchbotModel.LOCK_PRO,
  584. SwitchbotModel.LOCK_ULTRA,
  585. ],
  586. )
  587. async def test_lock_with_update(model: str):
  588. """Test lock method with status update."""
  589. device = create_device_for_command_testing(model)
  590. device._get_adv_value = Mock(side_effect=[None, LockStatus.UNLOCKED])
  591. with (
  592. patch.object(device, "update", new_callable=AsyncMock),
  593. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  594. patch.object(device, "_enable_notifications", return_value=True),
  595. patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
  596. ):
  597. result = await device.lock()
  598. assert result is True
  599. @pytest.mark.asyncio
  600. @pytest.mark.parametrize(
  601. ("model", "status"),
  602. [
  603. (SwitchbotModel.LOCK, LockStatus.LOCKED),
  604. (SwitchbotModel.LOCK_LITE, LockStatus.LOCKING),
  605. (SwitchbotModel.LOCK_PRO, LockStatus.LOCKED),
  606. (SwitchbotModel.LOCK_ULTRA, LockStatus.LOCKING),
  607. ],
  608. )
  609. async def test_lock_already_locked(model: str, status: LockStatus):
  610. """Test lock method when already locked."""
  611. device = create_device_for_command_testing(model)
  612. device._get_adv_value = Mock(return_value=status)
  613. with patch.object(device, "_send_command") as mock_send:
  614. result = await device.lock()
  615. assert result is True
  616. mock_send.assert_not_called()
  617. @pytest.mark.asyncio
  618. @pytest.mark.parametrize(
  619. "model",
  620. [
  621. SwitchbotModel.LOCK,
  622. SwitchbotModel.LOCK_LITE,
  623. SwitchbotModel.LOCK_PRO,
  624. SwitchbotModel.LOCK_ULTRA,
  625. ],
  626. )
  627. async def test_lock_with_invalid_basic_data(model: str):
  628. """Test lock method with invalid basic data."""
  629. device = create_device_for_command_testing(model)
  630. device._get_adv_value = Mock(return_value=LockStatus.UNLOCKED)
  631. with (
  632. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  633. patch.object(device, "_enable_notifications", return_value=True),
  634. patch.object(device, "_get_basic_info", return_value=b"\x00"),
  635. ):
  636. result = await device.lock()
  637. assert result is True