test_lock.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. @pytest.mark.asyncio
  331. @pytest.mark.parametrize(
  332. "model",
  333. [
  334. SwitchbotModel.LOCK,
  335. SwitchbotModel.LOCK_LITE,
  336. SwitchbotModel.LOCK_PRO,
  337. SwitchbotModel.LOCK_ULTRA,
  338. ],
  339. )
  340. async def test_disable_notifications(model: str):
  341. """Test _disable_notifications method."""
  342. device = create_device_for_command_testing(model)
  343. device._notifications_enabled = True
  344. with patch.object(device, "_send_command", return_value=b"\x01\x00"):
  345. result = await device._disable_notifications()
  346. assert result is True
  347. assert device._notifications_enabled is False
  348. @pytest.mark.asyncio
  349. @pytest.mark.parametrize(
  350. "model",
  351. [
  352. SwitchbotModel.LOCK,
  353. SwitchbotModel.LOCK_LITE,
  354. SwitchbotModel.LOCK_PRO,
  355. SwitchbotModel.LOCK_ULTRA,
  356. ],
  357. )
  358. async def test_disable_notifications_already_disabled(model: str):
  359. """Test _disable_notifications when already disabled."""
  360. device = create_device_for_command_testing(model)
  361. device._notifications_enabled = False
  362. with patch.object(device, "_send_command") as mock_send:
  363. result = await device._disable_notifications()
  364. assert result is True
  365. mock_send.assert_not_called()
  366. @pytest.mark.parametrize(
  367. "model",
  368. [
  369. SwitchbotModel.LOCK,
  370. SwitchbotModel.LOCK_LITE,
  371. SwitchbotModel.LOCK_PRO,
  372. SwitchbotModel.LOCK_ULTRA,
  373. ],
  374. )
  375. def test_notification_handler(model: str):
  376. """Test _notification_handler method."""
  377. device = create_device_for_command_testing(model)
  378. device._notifications_enabled = True
  379. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  380. with patch.object(device, "_update_lock_status") as mock_update:
  381. device._notification_handler(0, data)
  382. mock_update.assert_called_once_with(data)
  383. @pytest.mark.parametrize(
  384. "model",
  385. [
  386. SwitchbotModel.LOCK,
  387. SwitchbotModel.LOCK_LITE,
  388. SwitchbotModel.LOCK_PRO,
  389. SwitchbotModel.LOCK_ULTRA,
  390. ],
  391. )
  392. def test_notification_handler_not_enabled(model: str):
  393. """Test _notification_handler when notifications not enabled."""
  394. device = create_device_for_command_testing(model)
  395. device._notifications_enabled = False
  396. data = bytearray(b"\x01\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  397. with (
  398. patch.object(device, "_update_lock_status") as mock_update,
  399. patch.object(
  400. device.__class__.__bases__[0], "_notification_handler"
  401. ) as mock_super,
  402. ):
  403. device._notification_handler(0, data)
  404. mock_update.assert_not_called()
  405. mock_super.assert_called_once()
  406. @pytest.mark.parametrize(
  407. "model",
  408. [
  409. SwitchbotModel.LOCK,
  410. SwitchbotModel.LOCK_LITE,
  411. SwitchbotModel.LOCK_PRO,
  412. SwitchbotModel.LOCK_ULTRA,
  413. ],
  414. )
  415. def test_notification_handler_during_disconnect(
  416. model: str, caplog: pytest.LogCaptureFixture
  417. ) -> None:
  418. """Test _notification_handler during expected disconnect."""
  419. device = create_device_for_command_testing(model)
  420. device._notifications_enabled = True
  421. device._expected_disconnect = True
  422. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  423. with (
  424. patch.object(device, "_update_lock_status") as mock_update,
  425. caplog.at_level(logging.DEBUG),
  426. ):
  427. device._notification_handler(0, data)
  428. # Should not update lock status during disconnect
  429. mock_update.assert_not_called()
  430. # Should log debug message
  431. assert "Ignoring lock notification during expected disconnect" in caplog.text
  432. @pytest.mark.parametrize(
  433. "model",
  434. [
  435. SwitchbotModel.LOCK,
  436. SwitchbotModel.LOCK_LITE,
  437. SwitchbotModel.LOCK_PRO,
  438. SwitchbotModel.LOCK_ULTRA,
  439. ],
  440. )
  441. def test_update_lock_status(model: str):
  442. """Test _update_lock_status method."""
  443. device = create_device_for_command_testing(model)
  444. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  445. with (
  446. patch.object(device, "_decrypt", return_value=b"\x80\x00\x00\x00\x00\x00"),
  447. patch.object(device, "_update_parsed_data", return_value=True),
  448. patch.object(device, "_reset_disconnect_timer"),
  449. patch.object(device, "_fire_callbacks"),
  450. ):
  451. device._update_lock_status(data)
  452. @pytest.mark.parametrize(
  453. ("model", "data", "expected"),
  454. [
  455. (
  456. SwitchbotModel.LOCK,
  457. b"\x80\x00\x00\x00\x00\x00",
  458. {
  459. "calibration": True,
  460. "status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
  461. "door_open": False,
  462. "unclosed_alarm": False,
  463. "unlocked_alarm": False,
  464. },
  465. ),
  466. (
  467. SwitchbotModel.LOCK_LITE,
  468. b"\x80\x00\x00\x00\x00\x00",
  469. {
  470. "calibration": True,
  471. "status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
  472. "unlocked_alarm": False,
  473. },
  474. ),
  475. (
  476. SwitchbotModel.LOCK_PRO,
  477. b"\x80\x00\x00\x00\x00\x00",
  478. {
  479. "calibration": True,
  480. "status": LockStatus.LOCKED, # (0x80 & 0b01111000) >> 3 = 0 = LOCKED
  481. "door_open": False,
  482. "unclosed_alarm": False,
  483. "unlocked_alarm": False,
  484. },
  485. ),
  486. (
  487. SwitchbotModel.LOCK_ULTRA,
  488. b"\x88\x10\x00\x00\x00\xc0",
  489. {
  490. "calibration": True,
  491. "status": LockStatus.UNLOCKED, # (0x88 & 0b01111000) >> 3 = 0x08 >> 3 = 1 = UNLOCKED
  492. "door_open": True,
  493. "unclosed_alarm": True,
  494. "unlocked_alarm": True,
  495. },
  496. ),
  497. ],
  498. )
  499. def test_parse_lock_data(model: str, data: bytes, expected: dict):
  500. """Test _parse_lock_data static method."""
  501. result = lock.SwitchbotLock._parse_lock_data(data, model)
  502. assert result == expected
  503. @pytest.mark.parametrize(
  504. ("model", "data", "expected"),
  505. [
  506. # Test LOCK with different status bits and flags
  507. (
  508. SwitchbotModel.LOCK,
  509. b"\x94\x00\x00\x00\x00\x00", # Unlocked status (0x10 >> 4 = 1) with door open
  510. {
  511. "calibration": True,
  512. "status": LockStatus.UNLOCKED,
  513. "door_open": True,
  514. "unclosed_alarm": False,
  515. "unlocked_alarm": False,
  516. },
  517. ),
  518. # Test LOCK_LITE without door_open field
  519. (
  520. SwitchbotModel.LOCK_LITE,
  521. b"\x90\x10\x00\x00\x00\x00", # Unlocked with unlocked alarm
  522. {
  523. "calibration": True,
  524. "status": LockStatus.UNLOCKED,
  525. "unlocked_alarm": True,
  526. },
  527. ),
  528. # Test LOCK_PRO with new bit positions
  529. (
  530. SwitchbotModel.LOCK_PRO,
  531. b"\x90\x10\x00\x00\x00\xc0", # New format: status bits 3-6, door open bit 4 of byte 1
  532. {
  533. "calibration": True,
  534. "status": LockStatus.LOCKING, # (0x90 & 0b01111000) >> 3 = 0x10 >> 3 = 2 (LOCKING)
  535. "door_open": True, # bit 4 of byte 1 (0x10)
  536. "unclosed_alarm": True, # bit 7 of byte 5 (0xc0)
  537. "unlocked_alarm": True, # bit 6 of byte 5 (0xc0)
  538. },
  539. ),
  540. # Test LOCK_ULTRA with same format as PRO
  541. (
  542. SwitchbotModel.LOCK_ULTRA,
  543. b"\x88\x00\x00\x00\x00\x40", # Unlocked with unlocked alarm only
  544. {
  545. "calibration": True,
  546. "status": LockStatus.UNLOCKED, # (0x88 & 0b01111000) >> 3 = 0x08 >> 3 = 1
  547. "door_open": False,
  548. "unclosed_alarm": False,
  549. "unlocked_alarm": True, # bit 6 of byte 5
  550. },
  551. ),
  552. ],
  553. )
  554. def test_parse_lock_data_new_formats(model: str, data: bytes, expected: dict):
  555. """Test _parse_lock_data with new format changes."""
  556. result = lock.SwitchbotLock._parse_lock_data(data, model)
  557. assert result == expected
  558. @pytest.mark.asyncio
  559. @pytest.mark.parametrize(
  560. "model",
  561. [
  562. SwitchbotModel.LOCK,
  563. SwitchbotModel.LOCK_LITE,
  564. SwitchbotModel.LOCK_PRO,
  565. SwitchbotModel.LOCK_ULTRA,
  566. ],
  567. )
  568. async def test_lock_with_update(model: str):
  569. """Test lock method with status update."""
  570. device = create_device_for_command_testing(model)
  571. device._get_adv_value = Mock(side_effect=[None, LockStatus.UNLOCKED])
  572. with (
  573. patch.object(device, "update", new_callable=AsyncMock),
  574. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  575. patch.object(device, "_enable_notifications", return_value=True),
  576. patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
  577. ):
  578. result = await device.lock()
  579. assert result is True
  580. @pytest.mark.asyncio
  581. @pytest.mark.parametrize(
  582. ("model", "status"),
  583. [
  584. (SwitchbotModel.LOCK, LockStatus.LOCKED),
  585. (SwitchbotModel.LOCK_LITE, LockStatus.LOCKING),
  586. (SwitchbotModel.LOCK_PRO, LockStatus.LOCKED),
  587. (SwitchbotModel.LOCK_ULTRA, LockStatus.LOCKING),
  588. ],
  589. )
  590. async def test_lock_already_locked(model: str, status: LockStatus):
  591. """Test lock method when already locked."""
  592. device = create_device_for_command_testing(model)
  593. device._get_adv_value = Mock(return_value=status)
  594. with patch.object(device, "_send_command") as mock_send:
  595. result = await device.lock()
  596. assert result is True
  597. mock_send.assert_not_called()
  598. @pytest.mark.asyncio
  599. @pytest.mark.parametrize(
  600. "model",
  601. [
  602. SwitchbotModel.LOCK,
  603. SwitchbotModel.LOCK_LITE,
  604. SwitchbotModel.LOCK_PRO,
  605. SwitchbotModel.LOCK_ULTRA,
  606. ],
  607. )
  608. async def test_lock_with_invalid_basic_data(model: str):
  609. """Test lock method with invalid basic data."""
  610. device = create_device_for_command_testing(model)
  611. device._get_adv_value = Mock(return_value=LockStatus.UNLOCKED)
  612. with (
  613. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  614. patch.object(device, "_enable_notifications", return_value=True),
  615. patch.object(device, "_get_basic_info", return_value=b"\x00"),
  616. ):
  617. result = await device.lock()
  618. assert result is True