test_lock.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 switchbot.devices.device import SwitchbotOperationError
  8. from .test_adv_parser import generate_ble_device
  9. def create_device_for_command_testing(model: str):
  10. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  11. return lock.SwitchbotLock(
  12. ble_device, "ff", "ffffffffffffffffffffffffffffffff", model=model
  13. )
  14. @pytest.mark.parametrize(
  15. "model",
  16. [
  17. SwitchbotModel.LOCK,
  18. SwitchbotModel.LOCK_LITE,
  19. SwitchbotModel.LOCK_PRO,
  20. SwitchbotModel.LOCK_ULTRA,
  21. SwitchbotModel.LOCK_VISION,
  22. SwitchbotModel.LOCK_VISION_PRO,
  23. SwitchbotModel.LOCK_PRO_WIFI,
  24. ],
  25. )
  26. def test_lock_init(model: str):
  27. """Test the initialization of the lock device."""
  28. device = create_device_for_command_testing(model)
  29. assert device._model == model
  30. @pytest.mark.parametrize(
  31. "model",
  32. [
  33. SwitchbotModel.AIR_PURIFIER_JP,
  34. ],
  35. )
  36. def test_lock_init_with_invalid_model(model: str):
  37. """Test that initializing with an invalid model raises ValueError."""
  38. with pytest.raises(
  39. ValueError, match="initializing SwitchbotLock with a non-lock model"
  40. ):
  41. create_device_for_command_testing(model)
  42. @pytest.mark.asyncio
  43. @pytest.mark.parametrize(
  44. "model",
  45. [
  46. SwitchbotModel.LOCK,
  47. SwitchbotModel.LOCK_LITE,
  48. SwitchbotModel.LOCK_PRO,
  49. SwitchbotModel.LOCK_ULTRA,
  50. SwitchbotModel.LOCK_VISION,
  51. SwitchbotModel.LOCK_VISION_PRO,
  52. SwitchbotModel.LOCK_PRO_WIFI,
  53. ],
  54. )
  55. async def test_verify_encryption_key(model: str):
  56. """Test verify_encryption_key method."""
  57. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  58. with patch("switchbot.devices.lock.super") as mock_super:
  59. mock_super().verify_encryption_key = AsyncMock(return_value=True)
  60. result = await lock.SwitchbotLock.verify_encryption_key(
  61. ble_device, "key_id", "encryption_key", model
  62. )
  63. assert result is True
  64. @pytest.mark.asyncio
  65. @pytest.mark.parametrize(
  66. ("model", "command"),
  67. [
  68. (SwitchbotModel.LOCK, b"W\x0fN\x01\x01\x10\x80"),
  69. (SwitchbotModel.LOCK_LITE, b"W\x0fN\x01\x01\x10\x81"),
  70. (SwitchbotModel.LOCK_PRO, b"W\x0fN\x01\x01\x10\x85"),
  71. (SwitchbotModel.LOCK_ULTRA, b"W\x0fN\x01\x01\x10\x86"),
  72. (SwitchbotModel.LOCK_VISION, b"W\x0fN\x01\x01\x00\x80"),
  73. (SwitchbotModel.LOCK_VISION_PRO, b"W\x0fN\x01\x01\x00\x80"),
  74. (SwitchbotModel.LOCK_PRO_WIFI, b"W\x0fN\x01\x01\x10\x82"),
  75. ],
  76. )
  77. async def test_lock(model: str, command: bytes):
  78. """Test lock method."""
  79. device = create_device_for_command_testing(model)
  80. device._get_adv_value = Mock(return_value=LockStatus.UNLOCKED)
  81. with (
  82. patch.object(
  83. device, "_send_command", return_value=b"\x01\x00"
  84. ) as mock_send_command,
  85. patch.object(device, "_enable_notifications", return_value=True),
  86. patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
  87. ):
  88. result = await device.lock()
  89. assert result is True
  90. mock_send_command.assert_any_call(lock.COMMAND_LOCK[model])
  91. @pytest.mark.asyncio
  92. @pytest.mark.parametrize(
  93. ("model", "command"),
  94. [
  95. (SwitchbotModel.LOCK, b"W\x0fN\x01\x01\x10\x80"),
  96. (SwitchbotModel.LOCK_LITE, b"W\x0fN\x01\x01\x10\x81"),
  97. (SwitchbotModel.LOCK_PRO, b"W\x0fN\x01\x01\x10\x84"),
  98. (SwitchbotModel.LOCK_ULTRA, b"W\x0fN\x01\x01\x10\x83"),
  99. (SwitchbotModel.LOCK_VISION, b"W\x0fN\x01\x01\x00\x80"),
  100. (SwitchbotModel.LOCK_VISION_PRO, b"W\x0fN\x01\x01\x00\x80"),
  101. (SwitchbotModel.LOCK_PRO_WIFI, b"W\x0fN\x01\x01\x10\x81"),
  102. ],
  103. )
  104. async def test_unlock(model: str, command: bytes):
  105. """Test unlock method."""
  106. device = create_device_for_command_testing(model)
  107. device._get_adv_value = Mock(return_value=LockStatus.LOCKED)
  108. with (
  109. patch.object(
  110. device, "_send_command", return_value=b"\x01\x00"
  111. ) as mock_send_command,
  112. patch.object(device, "_enable_notifications", return_value=True),
  113. patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
  114. ):
  115. result = await device.unlock()
  116. assert result is True
  117. mock_send_command.assert_any_call(lock.COMMAND_UNLOCK[model])
  118. @pytest.mark.asyncio
  119. @pytest.mark.parametrize(
  120. "model",
  121. [
  122. SwitchbotModel.LOCK,
  123. SwitchbotModel.LOCK_LITE,
  124. SwitchbotModel.LOCK_PRO,
  125. SwitchbotModel.LOCK_ULTRA,
  126. SwitchbotModel.LOCK_VISION,
  127. SwitchbotModel.LOCK_VISION_PRO,
  128. SwitchbotModel.LOCK_PRO_WIFI,
  129. ],
  130. )
  131. async def test_unlock_without_unlatch(model: str):
  132. """Test unlock_without_unlatch method."""
  133. device = create_device_for_command_testing(model)
  134. device._get_adv_value = Mock(return_value=LockStatus.LOCKED)
  135. with (
  136. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  137. patch.object(device, "_enable_notifications", return_value=True),
  138. patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
  139. ):
  140. result = await device.unlock_without_unlatch()
  141. assert result is True
  142. @pytest.mark.asyncio
  143. @pytest.mark.parametrize(
  144. "model",
  145. [
  146. SwitchbotModel.LOCK,
  147. SwitchbotModel.LOCK_LITE,
  148. SwitchbotModel.LOCK_PRO,
  149. SwitchbotModel.LOCK_ULTRA,
  150. SwitchbotModel.LOCK_VISION,
  151. SwitchbotModel.LOCK_VISION_PRO,
  152. SwitchbotModel.LOCK_PRO_WIFI,
  153. ],
  154. )
  155. async def test_get_basic_info(model: str):
  156. """Test get_basic_info method."""
  157. device = create_device_for_command_testing(model)
  158. lock_data = b"\x00\x80\x00\x00\x00\x00\x00\x00"
  159. basic_data = b"\x00\x64\x01"
  160. with (
  161. patch.object(device, "_get_lock_info", return_value=lock_data),
  162. patch.object(device, "_get_basic_info", return_value=basic_data),
  163. ):
  164. result = await device.get_basic_info()
  165. assert result is not None
  166. assert "battery" in result
  167. assert "firmware" in result
  168. assert "calibration" in result
  169. assert "status" in result
  170. @pytest.mark.asyncio
  171. @pytest.mark.parametrize(
  172. "model",
  173. [
  174. SwitchbotModel.LOCK,
  175. SwitchbotModel.LOCK_LITE,
  176. SwitchbotModel.LOCK_PRO,
  177. SwitchbotModel.LOCK_ULTRA,
  178. SwitchbotModel.LOCK_VISION,
  179. SwitchbotModel.LOCK_VISION_PRO,
  180. SwitchbotModel.LOCK_PRO_WIFI,
  181. ],
  182. )
  183. async def test_get_basic_info_no_lock_data(model: str):
  184. """Test get_basic_info when no lock data is returned."""
  185. device = create_device_for_command_testing(model)
  186. with patch.object(device, "_get_lock_info", return_value=None):
  187. result = await device.get_basic_info()
  188. assert result is None
  189. @pytest.mark.asyncio
  190. @pytest.mark.parametrize(
  191. "model",
  192. [
  193. SwitchbotModel.LOCK,
  194. SwitchbotModel.LOCK_LITE,
  195. SwitchbotModel.LOCK_PRO,
  196. SwitchbotModel.LOCK_ULTRA,
  197. SwitchbotModel.LOCK_VISION,
  198. SwitchbotModel.LOCK_VISION_PRO,
  199. SwitchbotModel.LOCK_PRO_WIFI,
  200. ],
  201. )
  202. async def test_get_basic_info_no_basic_data(model: str):
  203. """Test get_basic_info when no basic data is returned."""
  204. device = create_device_for_command_testing(model)
  205. lock_data = b"\x00\x80\x00\x00\x00\x00\x00\x00"
  206. with (
  207. patch.object(device, "_get_lock_info", return_value=lock_data),
  208. patch.object(device, "_get_basic_info", return_value=None),
  209. ):
  210. result = await device.get_basic_info()
  211. assert result is None
  212. def test_parse_basic_data():
  213. """Test _parse_basic_data method."""
  214. device = create_device_for_command_testing(SwitchbotModel.LOCK)
  215. basic_data = b"\x00\x64\x01"
  216. result = device._parse_basic_data(basic_data)
  217. assert result["battery"] == 100
  218. assert result["firmware"] == 0.1
  219. @pytest.mark.parametrize(
  220. "model",
  221. [
  222. SwitchbotModel.LOCK,
  223. SwitchbotModel.LOCK_LITE,
  224. SwitchbotModel.LOCK_PRO,
  225. SwitchbotModel.LOCK_ULTRA,
  226. SwitchbotModel.LOCK_VISION,
  227. SwitchbotModel.LOCK_VISION_PRO,
  228. SwitchbotModel.LOCK_PRO_WIFI,
  229. ],
  230. )
  231. def test_is_calibrated(model: str):
  232. """Test is_calibrated method."""
  233. device = create_device_for_command_testing(model)
  234. device._get_adv_value = Mock(return_value=True)
  235. assert device.is_calibrated() is True
  236. @pytest.mark.parametrize(
  237. "model",
  238. [
  239. SwitchbotModel.LOCK,
  240. SwitchbotModel.LOCK_LITE,
  241. SwitchbotModel.LOCK_PRO,
  242. SwitchbotModel.LOCK_ULTRA,
  243. SwitchbotModel.LOCK_VISION,
  244. SwitchbotModel.LOCK_VISION_PRO,
  245. SwitchbotModel.LOCK_PRO_WIFI,
  246. ],
  247. )
  248. def test_get_lock_status(model: str):
  249. """Test get_lock_status method."""
  250. device = create_device_for_command_testing(model)
  251. device._get_adv_value = Mock(return_value=LockStatus.LOCKED)
  252. assert device.get_lock_status() == LockStatus.LOCKED
  253. @pytest.mark.parametrize(
  254. "model",
  255. [
  256. SwitchbotModel.LOCK,
  257. SwitchbotModel.LOCK_PRO,
  258. SwitchbotModel.LOCK_ULTRA,
  259. SwitchbotModel.LOCK_VISION_PRO,
  260. SwitchbotModel.LOCK_PRO_WIFI,
  261. ],
  262. )
  263. def test_is_door_open(model: str):
  264. """Test is_door_open method."""
  265. device = create_device_for_command_testing(model)
  266. device._get_adv_value = Mock(return_value=True)
  267. assert device.is_door_open() is True
  268. @pytest.mark.parametrize(
  269. "model",
  270. [
  271. SwitchbotModel.LOCK,
  272. SwitchbotModel.LOCK_PRO,
  273. SwitchbotModel.LOCK_ULTRA,
  274. SwitchbotModel.LOCK_VISION_PRO,
  275. SwitchbotModel.LOCK_PRO_WIFI,
  276. ],
  277. )
  278. def test_is_unclosed_alarm_on(model: str):
  279. """Test is_unclosed_alarm_on method."""
  280. device = create_device_for_command_testing(model)
  281. device._get_adv_value = Mock(return_value=True)
  282. assert device.is_unclosed_alarm_on() is True
  283. @pytest.mark.parametrize(
  284. "model",
  285. [
  286. SwitchbotModel.LOCK,
  287. SwitchbotModel.LOCK_LITE,
  288. SwitchbotModel.LOCK_PRO,
  289. SwitchbotModel.LOCK_ULTRA,
  290. SwitchbotModel.LOCK_VISION,
  291. SwitchbotModel.LOCK_VISION_PRO,
  292. SwitchbotModel.LOCK_PRO_WIFI,
  293. ],
  294. )
  295. def test_is_unlocked_alarm_on(model: str):
  296. """Test is_unlocked_alarm_on method."""
  297. device = create_device_for_command_testing(model)
  298. device._get_adv_value = Mock(return_value=True)
  299. assert device.is_unlocked_alarm_on() is True
  300. @pytest.mark.parametrize(
  301. "model",
  302. [
  303. SwitchbotModel.LOCK,
  304. ],
  305. )
  306. def test_is_auto_lock_paused(model: str):
  307. """Test is_auto_lock_paused method."""
  308. device = create_device_for_command_testing(model)
  309. device._get_adv_value = Mock(return_value=True)
  310. assert device.is_auto_lock_paused() is True
  311. @pytest.mark.parametrize(
  312. "model",
  313. [
  314. SwitchbotModel.LOCK,
  315. SwitchbotModel.LOCK_LITE,
  316. SwitchbotModel.LOCK_PRO,
  317. SwitchbotModel.LOCK_ULTRA,
  318. SwitchbotModel.LOCK_VISION,
  319. SwitchbotModel.LOCK_VISION_PRO,
  320. SwitchbotModel.LOCK_PRO_WIFI,
  321. ],
  322. )
  323. def test_is_night_latch_enabled(model: str):
  324. """Test is_night_latch_enabled method."""
  325. device = create_device_for_command_testing(model)
  326. device._get_adv_value = Mock(return_value=True)
  327. assert device.is_night_latch_enabled() is True
  328. @pytest.mark.asyncio
  329. @pytest.mark.parametrize(
  330. "model",
  331. [
  332. SwitchbotModel.LOCK,
  333. SwitchbotModel.LOCK_LITE,
  334. SwitchbotModel.LOCK_PRO,
  335. SwitchbotModel.LOCK_ULTRA,
  336. SwitchbotModel.LOCK_VISION,
  337. SwitchbotModel.LOCK_VISION_PRO,
  338. SwitchbotModel.LOCK_PRO_WIFI,
  339. ],
  340. )
  341. async def test_get_lock_info(model: str):
  342. """Test _get_lock_info method."""
  343. device = create_device_for_command_testing(model)
  344. expected_data = b"\x01\x00\x80\x00\x00\x00\x00\x00"
  345. with patch.object(device, "_send_command", return_value=expected_data):
  346. result = await device._get_lock_info()
  347. assert result == expected_data
  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. SwitchbotModel.LOCK_VISION,
  357. SwitchbotModel.LOCK_VISION_PRO,
  358. SwitchbotModel.LOCK_PRO_WIFI,
  359. ],
  360. )
  361. async def test_get_lock_info_failure(model: str):
  362. """Test _get_lock_info method when command fails."""
  363. device = create_device_for_command_testing(model)
  364. with patch.object(device, "_send_command", return_value=b"\x00\x00"):
  365. result = await device._get_lock_info()
  366. assert result is None
  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. SwitchbotModel.LOCK_VISION,
  376. SwitchbotModel.LOCK_VISION_PRO,
  377. SwitchbotModel.LOCK_PRO_WIFI,
  378. ],
  379. )
  380. async def test_enable_notifications(model: str):
  381. """Test _enable_notifications method."""
  382. device = create_device_for_command_testing(model)
  383. with patch.object(device, "_send_command", return_value=b"\x01\x00"):
  384. result = await device._enable_notifications()
  385. assert result is True
  386. @pytest.mark.asyncio
  387. @pytest.mark.parametrize(
  388. "model",
  389. [
  390. SwitchbotModel.LOCK,
  391. SwitchbotModel.LOCK_LITE,
  392. SwitchbotModel.LOCK_PRO,
  393. SwitchbotModel.LOCK_ULTRA,
  394. SwitchbotModel.LOCK_VISION,
  395. SwitchbotModel.LOCK_VISION_PRO,
  396. SwitchbotModel.LOCK_PRO_WIFI,
  397. ],
  398. )
  399. async def test_disable_notifications(model: str):
  400. """Test _disable_notifications method."""
  401. device = create_device_for_command_testing(model)
  402. device._notifications_enabled = True
  403. with patch.object(device, "_send_command", return_value=b"\x01\x00"):
  404. result = await device._disable_notifications()
  405. assert result is True
  406. assert device._notifications_enabled is False
  407. @pytest.mark.asyncio
  408. @pytest.mark.parametrize(
  409. "model",
  410. [
  411. SwitchbotModel.LOCK,
  412. SwitchbotModel.LOCK_LITE,
  413. SwitchbotModel.LOCK_PRO,
  414. SwitchbotModel.LOCK_ULTRA,
  415. SwitchbotModel.LOCK_VISION,
  416. SwitchbotModel.LOCK_VISION_PRO,
  417. SwitchbotModel.LOCK_PRO_WIFI,
  418. ],
  419. )
  420. async def test_disable_notifications_already_disabled(model: str):
  421. """Test _disable_notifications when already disabled."""
  422. device = create_device_for_command_testing(model)
  423. device._notifications_enabled = False
  424. with patch.object(device, "_send_command") as mock_send:
  425. result = await device._disable_notifications()
  426. assert result is True
  427. mock_send.assert_not_called()
  428. @pytest.mark.parametrize(
  429. "model",
  430. [
  431. SwitchbotModel.LOCK,
  432. SwitchbotModel.LOCK_LITE,
  433. SwitchbotModel.LOCK_PRO,
  434. SwitchbotModel.LOCK_ULTRA,
  435. SwitchbotModel.LOCK_VISION,
  436. SwitchbotModel.LOCK_VISION_PRO,
  437. SwitchbotModel.LOCK_PRO_WIFI,
  438. ],
  439. )
  440. def test_notification_handler(model: str):
  441. """Test _notification_handler method."""
  442. device = create_device_for_command_testing(model)
  443. device._notifications_enabled = True
  444. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  445. with patch.object(device, "_update_lock_status") as mock_update:
  446. device._notification_handler(0, data)
  447. mock_update.assert_called_once_with(data)
  448. @pytest.mark.parametrize(
  449. "model",
  450. [
  451. SwitchbotModel.LOCK,
  452. SwitchbotModel.LOCK_LITE,
  453. SwitchbotModel.LOCK_PRO,
  454. SwitchbotModel.LOCK_ULTRA,
  455. SwitchbotModel.LOCK_VISION,
  456. SwitchbotModel.LOCK_VISION_PRO,
  457. SwitchbotModel.LOCK_PRO_WIFI,
  458. ],
  459. )
  460. def test_notification_handler_not_enabled(model: str):
  461. """Test _notification_handler when notifications not enabled."""
  462. device = create_device_for_command_testing(model)
  463. device._notifications_enabled = False
  464. data = bytearray(b"\x01\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  465. with (
  466. patch.object(device, "_update_lock_status") as mock_update,
  467. patch.object(
  468. device.__class__.__bases__[0], "_notification_handler"
  469. ) as mock_super,
  470. ):
  471. device._notification_handler(0, data)
  472. mock_update.assert_not_called()
  473. mock_super.assert_called_once()
  474. @pytest.mark.parametrize(
  475. "model",
  476. [
  477. SwitchbotModel.LOCK,
  478. SwitchbotModel.LOCK_LITE,
  479. SwitchbotModel.LOCK_PRO,
  480. SwitchbotModel.LOCK_ULTRA,
  481. SwitchbotModel.LOCK_VISION,
  482. SwitchbotModel.LOCK_VISION_PRO,
  483. SwitchbotModel.LOCK_PRO_WIFI,
  484. ],
  485. )
  486. def test_notification_handler_during_disconnect(
  487. model: str, caplog: pytest.LogCaptureFixture
  488. ) -> None:
  489. """Test _notification_handler during expected disconnect."""
  490. device = create_device_for_command_testing(model)
  491. device._notifications_enabled = True
  492. device._expected_disconnect = True
  493. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  494. with (
  495. patch.object(device, "_update_lock_status") as mock_update,
  496. caplog.at_level(logging.DEBUG),
  497. ):
  498. device._notification_handler(0, data)
  499. # Should not update lock status during disconnect
  500. mock_update.assert_not_called()
  501. # Should log debug message
  502. assert "Ignoring lock notification during expected disconnect" in caplog.text
  503. @pytest.mark.parametrize(
  504. "model",
  505. [
  506. SwitchbotModel.LOCK,
  507. SwitchbotModel.LOCK_LITE,
  508. SwitchbotModel.LOCK_PRO,
  509. SwitchbotModel.LOCK_ULTRA,
  510. SwitchbotModel.LOCK_VISION,
  511. SwitchbotModel.LOCK_VISION_PRO,
  512. SwitchbotModel.LOCK_PRO_WIFI,
  513. ],
  514. )
  515. def test_update_lock_status(model: str):
  516. """Test _update_lock_status method."""
  517. device = create_device_for_command_testing(model)
  518. data = bytearray(b"\x0f\x00\x00\x00\x80\x00\x00\x00\x00\x00")
  519. with (
  520. patch.object(device, "_decrypt", return_value=b"\x80\x00\x00\x00\x00\x00"),
  521. patch.object(device, "_update_parsed_data", return_value=True),
  522. patch.object(device, "_reset_disconnect_timer"),
  523. patch.object(device, "_fire_callbacks"),
  524. ):
  525. device._update_lock_status(data)
  526. @pytest.mark.parametrize(
  527. ("model", "data", "expected"),
  528. [
  529. (
  530. SwitchbotModel.LOCK,
  531. b"\x80\x00\x00\x00\x00\x00",
  532. {
  533. "calibration": True,
  534. "status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
  535. "door_open": False,
  536. "unclosed_alarm": False,
  537. "unlocked_alarm": False,
  538. },
  539. ),
  540. (
  541. SwitchbotModel.LOCK_LITE,
  542. b"\x80\x00\x00\x00\x00\x00",
  543. {
  544. "calibration": True,
  545. "status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
  546. "unlocked_alarm": False,
  547. },
  548. ),
  549. (
  550. SwitchbotModel.LOCK_PRO,
  551. b"\x80\x00\x00\x00\x00\x00",
  552. {
  553. "calibration": True,
  554. "status": LockStatus.LOCKED, # (0x80 & 0b01111000) >> 3 = 0 = LOCKED
  555. "door_open": False,
  556. "unclosed_alarm": False,
  557. "unlocked_alarm": False,
  558. },
  559. ),
  560. (
  561. SwitchbotModel.LOCK_ULTRA,
  562. b"\x88\x10\x00\x00\x00\xc0",
  563. {
  564. "calibration": True,
  565. "status": LockStatus.UNLOCKED, # (0x88 & 0b01111000) >> 3 = 0x08 >> 3 = 1 = UNLOCKED
  566. "door_open": True,
  567. "unclosed_alarm": True,
  568. "unlocked_alarm": True,
  569. "half_lock_calibration": False,
  570. },
  571. ),
  572. (
  573. SwitchbotModel.LOCK_VISION,
  574. b"\x80\x00\x00\x00\x00\x00",
  575. {
  576. "calibration": True,
  577. "status": LockStatus.LOCKED, # (0x80 & 0b01110000) >> 4 = 0 = LOCKED
  578. "unlocked_alarm": False,
  579. },
  580. ),
  581. (
  582. SwitchbotModel.LOCK_VISION_PRO,
  583. b"\x80\x00\x00\x00\x00\x00",
  584. {
  585. "calibration": True,
  586. "status": LockStatus.LOCKED, # (0x80 & 0b01111000) >> 3 = 0 = LOCKED
  587. "door_open": False,
  588. "unclosed_alarm": False,
  589. "unlocked_alarm": False,
  590. },
  591. ),
  592. (
  593. SwitchbotModel.LOCK_PRO_WIFI,
  594. b"\x80\x00\x00\x00\x00\x00",
  595. {
  596. "calibration": True,
  597. "status": LockStatus.LOCKED, # (0x80 & 0b01111000) >> 3 = 0 = LOCKED
  598. "door_open": False,
  599. "unclosed_alarm": False,
  600. "unlocked_alarm": False,
  601. },
  602. ),
  603. ],
  604. )
  605. def test_parse_lock_data(model: str, data: bytes, expected: dict):
  606. """Test _parse_lock_data static method."""
  607. result = lock.SwitchbotLock._parse_lock_data(data, model)
  608. assert result == expected
  609. @pytest.mark.parametrize(
  610. ("model", "data", "expected"),
  611. [
  612. # Test LOCK with different status bits and flags
  613. (
  614. SwitchbotModel.LOCK,
  615. b"\x94\x00\x00\x00\x00\x00", # Unlocked status (0x10 >> 4 = 1) with door open
  616. {
  617. "calibration": True,
  618. "status": LockStatus.UNLOCKED,
  619. "door_open": True,
  620. "unclosed_alarm": False,
  621. "unlocked_alarm": False,
  622. },
  623. ),
  624. # Test LOCK_LITE without door_open field
  625. (
  626. SwitchbotModel.LOCK_LITE,
  627. b"\x90\x10\x00\x00\x00\x00", # Unlocked with unlocked alarm
  628. {
  629. "calibration": True,
  630. "status": LockStatus.UNLOCKED,
  631. "unlocked_alarm": True,
  632. },
  633. ),
  634. # Test LOCK_PRO with new bit positions
  635. (
  636. SwitchbotModel.LOCK_PRO,
  637. b"\x90\x10\x00\x00\x00\xc0", # New format: status bits 3-6, door open bit 4 of byte 1
  638. {
  639. "calibration": True,
  640. "status": LockStatus.LOCKING, # (0x90 & 0b01111000) >> 3 = 0x10 >> 3 = 2 (LOCKING)
  641. "door_open": True, # bit 4 of byte 1 (0x10)
  642. "unclosed_alarm": True, # bit 7 of byte 5 (0xc0)
  643. "unlocked_alarm": True, # bit 6 of byte 5 (0xc0)
  644. },
  645. ),
  646. # Test LOCK_ULTRA with same format as PRO
  647. (
  648. SwitchbotModel.LOCK_ULTRA,
  649. b"\x88\x00\x00\x00\x00\x40", # Unlocked with unlocked alarm only
  650. {
  651. "calibration": True,
  652. "status": LockStatus.UNLOCKED, # (0x88 & 0b01111000) >> 3 = 0x08 >> 3 = 1
  653. "door_open": False,
  654. "unclosed_alarm": False,
  655. "unlocked_alarm": True, # bit 6 of byte 5
  656. "half_lock_calibration": False,
  657. },
  658. ),
  659. (
  660. SwitchbotModel.LOCK_PRO_WIFI,
  661. b"\x90\x10\x00\x00\x00\xc0", # New format: status bits 3-6, door open bit 4 of byte 1
  662. {
  663. "calibration": True,
  664. "status": LockStatus.LOCKING, # (0x90 & 0b01111000) >> 3 = 0x10 >> 3 = 2 (LOCKING)
  665. "door_open": True, # bit 4 of byte 1 (0x10)
  666. "unclosed_alarm": True, # bit 7 of byte 5 (0xc0)
  667. "unlocked_alarm": True, # bit 6 of byte 5 (0xc0)
  668. },
  669. ),
  670. ],
  671. )
  672. def test_parse_lock_data_new_formats(model: str, data: bytes, expected: dict):
  673. """Test _parse_lock_data with new format changes."""
  674. result = lock.SwitchbotLock._parse_lock_data(data, model)
  675. assert result == expected
  676. @pytest.mark.asyncio
  677. @pytest.mark.parametrize(
  678. "model",
  679. [
  680. SwitchbotModel.LOCK,
  681. SwitchbotModel.LOCK_LITE,
  682. SwitchbotModel.LOCK_PRO,
  683. SwitchbotModel.LOCK_ULTRA,
  684. SwitchbotModel.LOCK_VISION,
  685. SwitchbotModel.LOCK_VISION_PRO,
  686. SwitchbotModel.LOCK_PRO_WIFI,
  687. ],
  688. )
  689. async def test_lock_with_update(model: str):
  690. """Test lock method with status update."""
  691. device = create_device_for_command_testing(model)
  692. device._get_adv_value = Mock(side_effect=[None, LockStatus.UNLOCKED])
  693. with (
  694. patch.object(device, "update", new_callable=AsyncMock),
  695. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  696. patch.object(device, "_enable_notifications", return_value=True),
  697. patch.object(device, "_get_basic_info", return_value=b"\x00\x64\x01"),
  698. ):
  699. result = await device.lock()
  700. assert result is True
  701. def test_is_half_lock_calibrated():
  702. """Test is_half_lock_calibrated method."""
  703. device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
  704. device._get_adv_value = Mock(return_value=True)
  705. assert device.is_half_lock_calibrated() is True
  706. device._get_adv_value = Mock(return_value=False)
  707. assert device.is_half_lock_calibrated() is False
  708. @pytest.mark.asyncio
  709. async def test_half_lock_calibrated():
  710. """Test half_lock succeeds when calibrated."""
  711. device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
  712. device._get_adv_value = Mock(side_effect=[True, LockStatus.LOCKED])
  713. with (
  714. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  715. patch.object(device, "_enable_notifications", return_value=True),
  716. patch.object(device, "_get_basic_info", return_value=b"\x01\x64\x01"),
  717. ):
  718. result = await device.half_lock()
  719. assert result is True
  720. @pytest.mark.asyncio
  721. async def test_half_lock_not_calibrated():
  722. """Test half_lock raises SwitchbotOperationError when not calibrated."""
  723. device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
  724. device._get_adv_value = Mock(return_value=False)
  725. with pytest.raises(SwitchbotOperationError, match="not calibrated"):
  726. await device.half_lock()
  727. @pytest.mark.asyncio
  728. @pytest.mark.parametrize(
  729. "model",
  730. [
  731. SwitchbotModel.LOCK,
  732. SwitchbotModel.LOCK_LITE,
  733. SwitchbotModel.LOCK_PRO,
  734. SwitchbotModel.LOCK_VISION,
  735. SwitchbotModel.LOCK_VISION_PRO,
  736. SwitchbotModel.LOCK_PRO_WIFI,
  737. ],
  738. )
  739. async def test_half_lock_unsupported_model(model: str):
  740. """Test half_lock raises SwitchbotOperationError on unsupported models."""
  741. device = create_device_for_command_testing(model)
  742. with pytest.raises(SwitchbotOperationError, match="not supported"):
  743. await device.half_lock()
  744. @pytest.mark.asyncio
  745. async def test_half_lock():
  746. """Test half_lock method."""
  747. device = create_device_for_command_testing(SwitchbotModel.LOCK_ULTRA)
  748. device._get_adv_value = Mock(side_effect=[True, LockStatus.LOCKED])
  749. with (
  750. patch.object(device, "_send_command", return_value=b"\x01\x00") as mock_send,
  751. patch.object(device, "_enable_notifications", return_value=True),
  752. patch.object(device, "_get_basic_info", return_value=b"\x01\x64\x01"),
  753. ):
  754. result = await device.half_lock()
  755. assert result is True
  756. mock_send.assert_awaited_once_with(
  757. lock.COMMAND_HALF_LOCK[SwitchbotModel.LOCK_ULTRA]
  758. )
  759. @pytest.mark.asyncio
  760. @pytest.mark.parametrize(
  761. ("model", "status"),
  762. [
  763. (SwitchbotModel.LOCK, LockStatus.LOCKED),
  764. (SwitchbotModel.LOCK_LITE, LockStatus.LOCKING),
  765. (SwitchbotModel.LOCK_PRO, LockStatus.LOCKED),
  766. (SwitchbotModel.LOCK_ULTRA, LockStatus.LOCKING),
  767. (SwitchbotModel.LOCK_VISION, LockStatus.LOCKED),
  768. (SwitchbotModel.LOCK_VISION_PRO, LockStatus.LOCKED),
  769. (SwitchbotModel.LOCK_PRO_WIFI, LockStatus.LOCKED),
  770. ],
  771. )
  772. async def test_lock_already_locked(model: str, status: LockStatus):
  773. """Test lock method when already locked."""
  774. device = create_device_for_command_testing(model)
  775. device._get_adv_value = Mock(return_value=status)
  776. with patch.object(device, "_send_command") as mock_send:
  777. result = await device.lock()
  778. assert result is True
  779. mock_send.assert_not_called()
  780. @pytest.mark.asyncio
  781. @pytest.mark.parametrize(
  782. "model",
  783. [
  784. SwitchbotModel.LOCK,
  785. SwitchbotModel.LOCK_LITE,
  786. SwitchbotModel.LOCK_PRO,
  787. SwitchbotModel.LOCK_ULTRA,
  788. SwitchbotModel.LOCK_VISION,
  789. SwitchbotModel.LOCK_VISION_PRO,
  790. SwitchbotModel.LOCK_PRO_WIFI,
  791. ],
  792. )
  793. async def test_lock_with_invalid_basic_data(model: str):
  794. """Test lock method with invalid basic data."""
  795. device = create_device_for_command_testing(model)
  796. device._get_adv_value = Mock(return_value=LockStatus.UNLOCKED)
  797. with (
  798. patch.object(device, "_send_command", return_value=b"\x01\x00"),
  799. patch.object(device, "_enable_notifications", return_value=True),
  800. patch.object(device, "_get_basic_info", return_value=b"\x00"),
  801. ):
  802. result = await device.lock()
  803. assert result is True