1
0

test_adv_parser.py 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. from __future__ import annotations
  2. from typing import Any
  3. from bleak.backends.device import BLEDevice
  4. from bleak.backends.scanner import AdvertisementData
  5. from switchbot import LockStatus, SwitchbotModel
  6. from switchbot.adv_parser import parse_advertisement_data
  7. from switchbot.models import SwitchBotAdvertisement
  8. ADVERTISEMENT_DATA_DEFAULTS = {
  9. "local_name": "",
  10. "manufacturer_data": {},
  11. "service_data": {},
  12. "service_uuids": [],
  13. "rssi": -127,
  14. "platform_data": ((),),
  15. "tx_power": -127,
  16. }
  17. BLE_DEVICE_DEFAULTS = {
  18. "name": None,
  19. "rssi": -127,
  20. "details": None,
  21. }
  22. def generate_ble_device(
  23. address: str | None = None,
  24. name: str | None = None,
  25. details: Any | None = None,
  26. rssi: int | None = None,
  27. **kwargs: Any,
  28. ) -> BLEDevice:
  29. """Generate a BLEDevice with defaults."""
  30. new = kwargs.copy()
  31. if address is not None:
  32. new["address"] = address
  33. if name is not None:
  34. new["name"] = name
  35. if details is not None:
  36. new["details"] = details
  37. if rssi is not None:
  38. new["rssi"] = rssi
  39. for key, value in BLE_DEVICE_DEFAULTS.items():
  40. new.setdefault(key, value)
  41. return BLEDevice(**new)
  42. def generate_advertisement_data(**kwargs: Any) -> AdvertisementData:
  43. """Generate advertisement data with defaults."""
  44. new = kwargs.copy()
  45. for key, value in ADVERTISEMENT_DATA_DEFAULTS.items():
  46. new.setdefault(key, value)
  47. return AdvertisementData(**new)
  48. def test_parse_advertisement_data_curtain():
  49. """Test parse_advertisement_data for curtain."""
  50. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  51. adv_data = generate_advertisement_data(
  52. manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
  53. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0X\x00\x11\x04"},
  54. rssi=-80,
  55. )
  56. result = parse_advertisement_data(ble_device, adv_data)
  57. assert result == SwitchBotAdvertisement(
  58. address="aa:bb:cc:dd:ee:ff",
  59. data={
  60. "rawAdvData": b"c\xc0X\x00\x11\x04",
  61. "data": {
  62. "calibration": True,
  63. "battery": 88,
  64. "inMotion": False,
  65. "position": 100,
  66. "lightLevel": 1,
  67. "deviceChain": 1,
  68. },
  69. "isEncrypted": False,
  70. "model": "c",
  71. "modelFriendlyName": "Curtain",
  72. "modelName": SwitchbotModel.CURTAIN,
  73. },
  74. device=ble_device,
  75. rssi=-80,
  76. active=True,
  77. )
  78. def test_parse_advertisement_data_curtain_passive():
  79. """Test parse_advertisement_data for curtain passive."""
  80. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  81. adv_data = generate_advertisement_data(
  82. manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
  83. service_data={},
  84. rssi=-80,
  85. )
  86. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.CURTAIN)
  87. assert result == SwitchBotAdvertisement(
  88. address="aa:bb:cc:dd:ee:ff",
  89. data={
  90. "rawAdvData": None,
  91. "data": {
  92. "calibration": None,
  93. "battery": None,
  94. "inMotion": False,
  95. "position": 100,
  96. "lightLevel": 1,
  97. "deviceChain": 1,
  98. },
  99. "isEncrypted": False,
  100. "model": "c",
  101. "modelFriendlyName": "Curtain",
  102. "modelName": SwitchbotModel.CURTAIN,
  103. },
  104. device=ble_device,
  105. rssi=-80,
  106. active=False,
  107. )
  108. def test_parse_advertisement_data_curtain_passive_12_bytes():
  109. """Test parse_advertisement_data for curtain passive."""
  110. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  111. adv_data = generate_advertisement_data(
  112. manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04\x00"},
  113. service_data={},
  114. rssi=-80,
  115. )
  116. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.CURTAIN)
  117. assert result == SwitchBotAdvertisement(
  118. address="aa:bb:cc:dd:ee:ff",
  119. data={
  120. "rawAdvData": None,
  121. "data": {
  122. "calibration": None,
  123. "battery": None,
  124. "inMotion": False,
  125. "position": 100,
  126. "lightLevel": 1,
  127. "deviceChain": 1,
  128. },
  129. "isEncrypted": False,
  130. "model": "c",
  131. "modelFriendlyName": "Curtain",
  132. "modelName": SwitchbotModel.CURTAIN,
  133. },
  134. device=ble_device,
  135. rssi=-80,
  136. active=False,
  137. )
  138. def test_parse_advertisement_data_curtain_position_zero():
  139. """Test parse_advertisement_data for curtain position zero."""
  140. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  141. adv_data = generate_advertisement_data(
  142. local_name="WoCurtain",
  143. manufacturer_data={89: b"\xc1\xc7'}U\xab"},
  144. service_data={"00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0\xced\x11\x04"},
  145. service_uuids=[
  146. "00001800-0000-1000-8000-00805f9b34fb",
  147. "00001801-0000-1000-8000-00805f9b34fb",
  148. "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
  149. ],
  150. rssi=-52,
  151. )
  152. result = parse_advertisement_data(ble_device, adv_data)
  153. assert result == SwitchBotAdvertisement(
  154. address="aa:bb:cc:dd:ee:ff",
  155. data={
  156. "rawAdvData": b"c\xd0\xced\x11\x04",
  157. "data": {
  158. "calibration": True,
  159. "battery": 78,
  160. "inMotion": False,
  161. "position": 0,
  162. "lightLevel": 1,
  163. "deviceChain": 1,
  164. },
  165. "isEncrypted": False,
  166. "model": "c",
  167. "modelFriendlyName": "Curtain",
  168. "modelName": SwitchbotModel.CURTAIN,
  169. },
  170. device=ble_device,
  171. rssi=-52,
  172. active=True,
  173. )
  174. def test_parse_advertisement_data_curtain_firmware_six_position_100():
  175. """Test parse_advertisement_data with firmware six for curtain position 100."""
  176. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  177. adv_data = generate_advertisement_data(
  178. local_name="WoCurtain",
  179. manufacturer_data={
  180. 89: b"\xf5\x98\x94\x08\xa0\xe7",
  181. 2409: b'\xf5\x98\x94\x08\xa0\xe7\x9b\x0f\x00"\x04',
  182. },
  183. service_data={
  184. "00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0H\x00\x12\x04",
  185. "0000fd3d-0000-1000-8000-00805f9b34fb": b'c\xc0G\x00"\x04',
  186. },
  187. service_uuids=[
  188. "00001800-0000-1000-8000-00805f9b34fb",
  189. "00001801-0000-1000-8000-00805f9b34fb",
  190. "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
  191. ],
  192. rssi=-62,
  193. )
  194. result = parse_advertisement_data(ble_device, adv_data)
  195. assert result == SwitchBotAdvertisement(
  196. address="aa:bb:cc:dd:ee:ff",
  197. data={
  198. "rawAdvData": b'c\xc0G\x00"\x04',
  199. "data": {
  200. "calibration": True,
  201. "battery": 71,
  202. "inMotion": False,
  203. "position": 100,
  204. "lightLevel": 2,
  205. "deviceChain": 2,
  206. },
  207. "isEncrypted": False,
  208. "model": "c",
  209. "modelFriendlyName": "Curtain",
  210. "modelName": SwitchbotModel.CURTAIN,
  211. },
  212. device=ble_device,
  213. rssi=-62,
  214. active=True,
  215. )
  216. def test_parse_advertisement_data_curtain_firmware_six_position_100_other_rssi():
  217. """Test parse_advertisement_data with firmware six for curtain position 100 other rssi."""
  218. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  219. adv_data = generate_advertisement_data(
  220. local_name="WoCurtain",
  221. manufacturer_data={
  222. 89: b"\xf5\x98\x94\x08\xa0\xe7",
  223. 2409: b'\xf5\x98\x94\x08\xa0\xe7\xa5\x0fc"\x04',
  224. },
  225. service_data={
  226. "00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0H\x00\x12\x04",
  227. "0000fd3d-0000-1000-8000-00805f9b34fb": b'c\xc0Gc"\x04',
  228. },
  229. service_uuids=[
  230. "00001800-0000-1000-8000-00805f9b34fb",
  231. "00001801-0000-1000-8000-00805f9b34fb",
  232. "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
  233. ],
  234. rssi=-67,
  235. )
  236. result = parse_advertisement_data(ble_device, adv_data)
  237. assert result == SwitchBotAdvertisement(
  238. address="aa:bb:cc:dd:ee:ff",
  239. data={
  240. "rawAdvData": b'c\xc0Gc"\x04',
  241. "data": {
  242. "calibration": True,
  243. "battery": 71,
  244. "inMotion": False,
  245. "position": 1,
  246. "lightLevel": 2,
  247. "deviceChain": 2,
  248. },
  249. "isEncrypted": False,
  250. "model": "c",
  251. "modelFriendlyName": "Curtain",
  252. "modelName": SwitchbotModel.CURTAIN,
  253. },
  254. device=ble_device,
  255. rssi=-67,
  256. active=True,
  257. )
  258. def test_parse_advertisement_data_curtain_fully_closed():
  259. """Test parse_advertisement_data with firmware six fully closed."""
  260. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  261. adv_data = generate_advertisement_data(
  262. local_name="WoCurtain",
  263. manufacturer_data={2409: b"\xc1\xc7'}U\xab\"\x0fd\x11\x04"},
  264. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0Sd\x11\x04"},
  265. service_uuids=[
  266. "00001800-0000-1000-8000-00805f9b34fb",
  267. "00001801-0000-1000-8000-00805f9b34fb",
  268. "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
  269. ],
  270. rssi=1,
  271. )
  272. result = parse_advertisement_data(ble_device, adv_data)
  273. assert result == SwitchBotAdvertisement(
  274. address="aa:bb:cc:dd:ee:ff",
  275. data={
  276. "rawAdvData": b"c\xc0Sd\x11\x04",
  277. "data": {
  278. "calibration": True,
  279. "battery": 83,
  280. "inMotion": False,
  281. "position": 0,
  282. "lightLevel": 1,
  283. "deviceChain": 1,
  284. },
  285. "isEncrypted": False,
  286. "model": "c",
  287. "modelFriendlyName": "Curtain",
  288. "modelName": SwitchbotModel.CURTAIN,
  289. },
  290. device=ble_device,
  291. rssi=1,
  292. active=True,
  293. )
  294. def test_parse_advertisement_data_curtain_fully_open():
  295. """Test parse_advertisement_data with firmware six fully open."""
  296. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  297. adv_data = generate_advertisement_data(
  298. local_name="WoCurtain",
  299. manufacturer_data={2409: b"\xc1\xc7'}U\xab%\x0f\x00\x11\x04"},
  300. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0S\x00\x11\x04"},
  301. service_uuids=[
  302. "00001800-0000-1000-8000-00805f9b34fb",
  303. "00001801-0000-1000-8000-00805f9b34fb",
  304. "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
  305. ],
  306. rssi=1,
  307. )
  308. result = parse_advertisement_data(ble_device, adv_data)
  309. assert result == SwitchBotAdvertisement(
  310. address="aa:bb:cc:dd:ee:ff",
  311. data={
  312. "rawAdvData": b"c\xc0S\x00\x11\x04",
  313. "data": {
  314. "calibration": True,
  315. "battery": 83,
  316. "inMotion": False,
  317. "position": 100,
  318. "lightLevel": 1,
  319. "deviceChain": 1,
  320. },
  321. "isEncrypted": False,
  322. "model": "c",
  323. "modelFriendlyName": "Curtain",
  324. "modelName": SwitchbotModel.CURTAIN,
  325. },
  326. device=ble_device,
  327. rssi=1,
  328. active=True,
  329. )
  330. def test_parse_advertisement_data_curtain3():
  331. """Test parse_advertisement_data for curtain 3."""
  332. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  333. adv_data = generate_advertisement_data(
  334. manufacturer_data={
  335. 2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"
  336. },
  337. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"{\xc0\x49\x00\x11\x04"},
  338. rssi=-80,
  339. )
  340. result = parse_advertisement_data(ble_device, adv_data)
  341. assert result == SwitchBotAdvertisement(
  342. address="aa:bb:cc:dd:ee:ff",
  343. data={
  344. "rawAdvData": b"{\xc0\x49\x00\x11\x04",
  345. "data": {
  346. "calibration": True,
  347. "battery": 73,
  348. "inMotion": False,
  349. "position": 100,
  350. "lightLevel": 1,
  351. "deviceChain": 1,
  352. },
  353. "isEncrypted": False,
  354. "model": "{",
  355. "modelFriendlyName": "Curtain 3",
  356. "modelName": SwitchbotModel.CURTAIN,
  357. },
  358. device=ble_device,
  359. rssi=-80,
  360. active=True,
  361. )
  362. def test_parse_advertisement_data_curtain3_passive():
  363. """Test parse_advertisement_data for curtain passive."""
  364. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  365. adv_data = generate_advertisement_data(
  366. manufacturer_data={
  367. 2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"
  368. },
  369. service_data={},
  370. rssi=-80,
  371. )
  372. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.CURTAIN)
  373. assert result == SwitchBotAdvertisement(
  374. address="aa:bb:cc:dd:ee:ff",
  375. data={
  376. "rawAdvData": None,
  377. "data": {
  378. "calibration": None,
  379. "battery": 73,
  380. "inMotion": False,
  381. "position": 100,
  382. "lightLevel": 1,
  383. "deviceChain": 1,
  384. },
  385. "isEncrypted": False,
  386. "model": "c",
  387. "modelFriendlyName": "Curtain",
  388. "modelName": SwitchbotModel.CURTAIN,
  389. },
  390. device=ble_device,
  391. rssi=-80,
  392. active=False,
  393. )
  394. def test_parse_advertisement_data_contact():
  395. """Test parse_advertisement_data for the contact sensor."""
  396. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  397. adv_data = generate_advertisement_data(
  398. manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
  399. service_data={
  400. "0000fd3d-0000-1000-8000-00805f9b34fb": b"d@d\x05\x00u\x00\xf8\x12"
  401. },
  402. rssi=-80,
  403. )
  404. result = parse_advertisement_data(ble_device, adv_data)
  405. assert result == SwitchBotAdvertisement(
  406. address="aa:bb:cc:dd:ee:ff",
  407. data={
  408. "rawAdvData": b"d@d\x05\x00u\x00\xf8\x12",
  409. "data": {
  410. "button_count": 2,
  411. "contact_open": True,
  412. "contact_timeout": True,
  413. "is_light": True,
  414. "battery": 100,
  415. "motion_detected": True,
  416. "tested": False,
  417. },
  418. "isEncrypted": False,
  419. "model": "d",
  420. "modelFriendlyName": "Contact Sensor",
  421. "modelName": SwitchbotModel.CONTACT_SENSOR,
  422. },
  423. device=ble_device,
  424. rssi=-80,
  425. active=True,
  426. )
  427. def test_parse_advertisement_data_empty():
  428. """Test parse_advertisement_data with empty data does not blow up."""
  429. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  430. adv_data = generate_advertisement_data(
  431. manufacturer_data={2403: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
  432. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b""},
  433. )
  434. result = parse_advertisement_data(ble_device, adv_data)
  435. assert result is None
  436. def test_new_bot_firmware():
  437. """Test parsing adv data from new bot firmware."""
  438. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  439. adv_data = generate_advertisement_data(
  440. manufacturer_data={89: b"\xd8.\xad\xcd\r\x85"},
  441. service_data={"00000d00-0000-1000-8000-00805f9b34fb": b"H\x10\xe1"},
  442. service_uuids=["CBA20D00-224D-11E6-9FB8-0002A5D5C51B"],
  443. rssi=-90,
  444. )
  445. result = parse_advertisement_data(ble_device, adv_data)
  446. assert result == SwitchBotAdvertisement(
  447. address="aa:bb:cc:dd:ee:ff",
  448. data={
  449. "rawAdvData": b"H\x10\xe1",
  450. "data": {"switchMode": False, "isOn": False, "battery": 97},
  451. "model": "H",
  452. "isEncrypted": False,
  453. "modelFriendlyName": "Bot",
  454. "modelName": SwitchbotModel.BOT,
  455. },
  456. device=ble_device,
  457. rssi=-90,
  458. active=True,
  459. )
  460. def test_parse_advertisement_data_curtain_firmware_six_fully_closed():
  461. """Test parse_advertisement_data with firmware six fully closed."""
  462. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  463. adv_data = generate_advertisement_data(
  464. local_name="WoCurtain",
  465. manufacturer_data={
  466. 89: b"\xcc\xf4\xc4\xf9\xacl",
  467. 2409: b"\xcc\xf4\xc4\xf9\xacl\xeb\x0fd\x12\x04",
  468. },
  469. service_data={
  470. "00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0Yd\x11\x04",
  471. "0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0dd\x12\x04",
  472. },
  473. service_uuids=[
  474. "00001800-0000-1000-8000-00805f9b34fb",
  475. "00001801-0000-1000-8000-00805f9b34fb",
  476. "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
  477. ],
  478. rssi=-2,
  479. )
  480. result = parse_advertisement_data(ble_device, adv_data)
  481. assert result == SwitchBotAdvertisement(
  482. address="aa:bb:cc:dd:ee:ff",
  483. data={
  484. "rawAdvData": b"c\xc0dd\x12\x04",
  485. "data": {
  486. "calibration": True,
  487. "battery": 100,
  488. "inMotion": False,
  489. "position": 0,
  490. "lightLevel": 1,
  491. "deviceChain": 2,
  492. },
  493. "isEncrypted": False,
  494. "model": "c",
  495. "modelFriendlyName": "Curtain",
  496. "modelName": SwitchbotModel.CURTAIN,
  497. },
  498. device=ble_device,
  499. rssi=-2,
  500. active=True,
  501. )
  502. def test_parse_advertisement_data_curtain_firmware_six_fully_open():
  503. """Test parse_advertisement_data with firmware six fully open."""
  504. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  505. adv_data = generate_advertisement_data(
  506. local_name="WoCurtain",
  507. manufacturer_data={
  508. 89: b"\xcc\xf4\xc4\xf9\xacl",
  509. 2409: b"\xcc\xf4\xc4\xf9\xacl\xe2\x0f\x00\x12\x04",
  510. },
  511. service_data={
  512. "00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0Yd\x11\x04",
  513. "0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0d\x00\x12\x04",
  514. },
  515. service_uuids=[
  516. "00001800-0000-1000-8000-00805f9b34fb",
  517. "00001801-0000-1000-8000-00805f9b34fb",
  518. "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
  519. ],
  520. rssi=-2,
  521. )
  522. result = parse_advertisement_data(ble_device, adv_data)
  523. assert result == SwitchBotAdvertisement(
  524. address="aa:bb:cc:dd:ee:ff",
  525. data={
  526. "rawAdvData": b"c\xc0d\x00\x12\x04",
  527. "data": {
  528. "calibration": True,
  529. "battery": 100,
  530. "inMotion": False,
  531. "position": 100,
  532. "lightLevel": 1,
  533. "deviceChain": 2,
  534. },
  535. "isEncrypted": False,
  536. "model": "c",
  537. "modelFriendlyName": "Curtain",
  538. "modelName": SwitchbotModel.CURTAIN,
  539. },
  540. device=ble_device,
  541. rssi=-2,
  542. active=True,
  543. )
  544. def test_contact_sensor_mfr():
  545. """Test parsing adv data from new bot firmware."""
  546. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  547. adv_data = generate_advertisement_data(
  548. manufacturer_data={2409: b"\xcb9\xcd\xc4=FA,\x00F\x01\x8f\xc4"},
  549. service_data={
  550. "0000fd3d-0000-1000-8000-00805f9b34fb": b"d\x00\xda\x04\x00F\x01\x8f\xc4"
  551. },
  552. tx_power=-127,
  553. rssi=-70,
  554. )
  555. result = parse_advertisement_data(ble_device, adv_data)
  556. assert result == SwitchBotAdvertisement(
  557. address="aa:bb:cc:dd:ee:ff",
  558. data={
  559. "data": {
  560. "battery": 90,
  561. "button_count": 4,
  562. "contact_open": True,
  563. "contact_timeout": True,
  564. "is_light": False,
  565. "motion_detected": False,
  566. "tested": False,
  567. },
  568. "isEncrypted": False,
  569. "model": "d",
  570. "modelFriendlyName": "Contact Sensor",
  571. "modelName": SwitchbotModel.CONTACT_SENSOR,
  572. "rawAdvData": b"d\x00\xda\x04\x00F\x01\x8f\xc4",
  573. },
  574. device=ble_device,
  575. rssi=-70,
  576. active=True,
  577. )
  578. def test_contact_sensor_mfr_no_service_data():
  579. """Test contact sensor with passive data only."""
  580. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  581. adv_data = generate_advertisement_data(
  582. manufacturer_data={2409: b"\xcb9\xcd\xc4=FA,\x00F\x01\x8f\xc4"},
  583. service_data={},
  584. tx_power=-127,
  585. rssi=-70,
  586. )
  587. result = parse_advertisement_data(ble_device, adv_data)
  588. # Passive detection of contact sensor is not supported
  589. # anymore since the Switchbot Curtain v3 was released
  590. # which uses the heuristics for the contact sensor.
  591. assert result is None
  592. def test_contact_sensor_srv():
  593. """Test parsing adv data from new bot firmware."""
  594. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  595. adv_data = generate_advertisement_data(
  596. service_data={
  597. "0000fd3d-0000-1000-8000-00805f9b34fb": b"d\x00\xda\x04\x00F\x01\x8f\xc4"
  598. },
  599. tx_power=-127,
  600. rssi=-70,
  601. )
  602. result = parse_advertisement_data(ble_device, adv_data)
  603. assert result == SwitchBotAdvertisement(
  604. address="aa:bb:cc:dd:ee:ff",
  605. data={
  606. "data": {
  607. "battery": 90,
  608. "button_count": 4,
  609. "contact_open": True,
  610. "contact_timeout": True,
  611. "is_light": False,
  612. "motion_detected": False,
  613. "tested": False,
  614. },
  615. "isEncrypted": False,
  616. "model": "d",
  617. "modelFriendlyName": "Contact Sensor",
  618. "modelName": SwitchbotModel.CONTACT_SENSOR,
  619. "rawAdvData": b"d\x00\xda\x04\x00F\x01\x8f\xc4",
  620. },
  621. device=ble_device,
  622. rssi=-70,
  623. active=True,
  624. )
  625. def test_contact_sensor_open():
  626. """Test parsing mfr adv data from new bot firmware."""
  627. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  628. adv_data = generate_advertisement_data(
  629. manufacturer_data={2409: b"\xcb9\xcd\xc4=F\x84\x9c\x00\x17\x00QD"},
  630. service_data={
  631. "0000fd3d-0000-1000-8000-00805f9b34fb": b"d@\xda\x02\x00\x17\x00QD"
  632. },
  633. tx_power=-127,
  634. rssi=-59,
  635. )
  636. result = parse_advertisement_data(ble_device, adv_data)
  637. assert result == SwitchBotAdvertisement(
  638. address="aa:bb:cc:dd:ee:ff",
  639. data={
  640. "data": {
  641. "battery": 90,
  642. "button_count": 4,
  643. "contact_open": True,
  644. "contact_timeout": False,
  645. "is_light": False,
  646. "motion_detected": True,
  647. "tested": False,
  648. },
  649. "isEncrypted": False,
  650. "model": "d",
  651. "modelFriendlyName": "Contact Sensor",
  652. "modelName": SwitchbotModel.CONTACT_SENSOR,
  653. "rawAdvData": b"d@\xda\x02\x00\x17\x00QD",
  654. },
  655. device=ble_device,
  656. rssi=-59,
  657. active=True,
  658. )
  659. def test_contact_sensor_closed():
  660. """Test parsing mfr adv data from new bot firmware."""
  661. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  662. adv_data = generate_advertisement_data(
  663. manufacturer_data={2409: b"\xcb9\xcd\xc4=F\x89\x8c\x00+\x00\x19\x84"},
  664. service_data={
  665. "0000fd3d-0000-1000-8000-00805f9b34fb": b"d@\xda\x00\x00+\x00\x19\x84"
  666. },
  667. tx_power=-127,
  668. rssi=-50,
  669. )
  670. result = parse_advertisement_data(ble_device, adv_data)
  671. assert result == SwitchBotAdvertisement(
  672. address="aa:bb:cc:dd:ee:ff",
  673. data={
  674. "data": {
  675. "battery": 90,
  676. "button_count": 4,
  677. "contact_open": False,
  678. "contact_timeout": False,
  679. "is_light": False,
  680. "motion_detected": True,
  681. "tested": False,
  682. },
  683. "isEncrypted": False,
  684. "model": "d",
  685. "modelFriendlyName": "Contact Sensor",
  686. "modelName": SwitchbotModel.CONTACT_SENSOR,
  687. "rawAdvData": b"d@\xda\x00\x00+\x00\x19\x84",
  688. },
  689. device=ble_device,
  690. rssi=-50,
  691. active=True,
  692. )
  693. def test_switchbot_passive():
  694. """Test parsing switchbot as passive."""
  695. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  696. adv_data = generate_advertisement_data(
  697. manufacturer_data={89: bytes.fromhex("d51cfb397856")},
  698. service_data={},
  699. tx_power=-127,
  700. rssi=-50,
  701. )
  702. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.BOT)
  703. assert result == SwitchBotAdvertisement(
  704. address="aa:bb:cc:dd:ee:ff",
  705. data={
  706. "data": {
  707. "battery": None,
  708. "switchMode": None,
  709. "isOn": None,
  710. },
  711. "isEncrypted": False,
  712. "model": "H",
  713. "modelFriendlyName": "Bot",
  714. "modelName": SwitchbotModel.BOT,
  715. "rawAdvData": None,
  716. },
  717. device=ble_device,
  718. rssi=-50,
  719. active=False,
  720. )
  721. def test_bulb_active():
  722. """Test parsing bulb as active."""
  723. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  724. adv_data = generate_advertisement_data(
  725. manufacturer_data={2409: b"\x84\xf7\x03\xb4\xcbz\x03\xe4!\x00\x00"},
  726. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"u\x00d"},
  727. tx_power=-127,
  728. rssi=-50,
  729. )
  730. result = parse_advertisement_data(ble_device, adv_data)
  731. assert result == SwitchBotAdvertisement(
  732. address="aa:bb:cc:dd:ee:ff",
  733. data={
  734. "data": {
  735. "brightness": 100,
  736. "color_mode": 1,
  737. "delay": False,
  738. "isOn": True,
  739. "loop_index": 0,
  740. "preset": False,
  741. "sequence_number": 3,
  742. "speed": 0,
  743. },
  744. "isEncrypted": False,
  745. "model": "u",
  746. "modelFriendlyName": "Color Bulb",
  747. "modelName": SwitchbotModel.COLOR_BULB,
  748. "rawAdvData": b"u\x00d",
  749. },
  750. device=ble_device,
  751. rssi=-50,
  752. active=True,
  753. )
  754. def test_lightstrip_passive():
  755. """Test parsing lightstrip as passive."""
  756. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  757. adv_data = generate_advertisement_data(
  758. manufacturer_data={
  759. 2409: b"`U\xf9(\xe5\x96\x00d\x02\xb0\x00\x00\x00\x00\x00\x00"
  760. },
  761. service_data={},
  762. tx_power=-127,
  763. rssi=-50,
  764. )
  765. result = parse_advertisement_data(ble_device, adv_data)
  766. assert result == SwitchBotAdvertisement(
  767. address="aa:bb:cc:dd:ee:ff",
  768. data={
  769. "data": {
  770. "brightness": 100,
  771. "color_mode": 2,
  772. "delay": False,
  773. "isOn": False,
  774. "loop_index": 0,
  775. "preset": False,
  776. "sequence_number": 0,
  777. "speed": 48,
  778. },
  779. "isEncrypted": False,
  780. "model": "r",
  781. "modelFriendlyName": "Light Strip",
  782. "modelName": SwitchbotModel.LIGHT_STRIP,
  783. "rawAdvData": None,
  784. },
  785. device=ble_device,
  786. rssi=-50,
  787. active=False,
  788. )
  789. def test_wosensor_passive_and_active():
  790. """Test parsing wosensor as passive with active data as well."""
  791. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  792. adv_data = generate_advertisement_data(
  793. manufacturer_data={2409: b"\xd7\xc1}]\xebC\xde\x03\x06\x985"},
  794. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\xe4\x06\x985"},
  795. tx_power=-127,
  796. rssi=-50,
  797. )
  798. result = parse_advertisement_data(ble_device, adv_data)
  799. assert result == SwitchBotAdvertisement(
  800. address="aa:bb:cc:dd:ee:ff",
  801. data={
  802. "data": {
  803. "battery": 100,
  804. "fahrenheit": False,
  805. "humidity": 53,
  806. "temp": {"c": 24.6, "f": 76.28},
  807. "temperature": 24.6,
  808. },
  809. "isEncrypted": False,
  810. "model": "T",
  811. "modelFriendlyName": "Meter",
  812. "modelName": SwitchbotModel.METER,
  813. "rawAdvData": b"T\x00\xe4\x06\x985",
  814. },
  815. device=ble_device,
  816. rssi=-50,
  817. active=True,
  818. )
  819. def test_wosensor_active():
  820. """Test parsing wosensor with active data as well."""
  821. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  822. adv_data = generate_advertisement_data(
  823. manufacturer_data={},
  824. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\xe4\x06\x985"},
  825. tx_power=-127,
  826. rssi=-50,
  827. )
  828. result = parse_advertisement_data(ble_device, adv_data)
  829. assert result == SwitchBotAdvertisement(
  830. address="aa:bb:cc:dd:ee:ff",
  831. data={
  832. "data": {
  833. "battery": 100,
  834. "fahrenheit": False,
  835. "humidity": 53,
  836. "temp": {"c": 24.6, "f": 76.28},
  837. "temperature": 24.6,
  838. },
  839. "isEncrypted": False,
  840. "model": "T",
  841. "modelFriendlyName": "Meter",
  842. "modelName": SwitchbotModel.METER,
  843. "rawAdvData": b"T\x00\xe4\x06\x985",
  844. },
  845. device=ble_device,
  846. rssi=-50,
  847. active=True,
  848. )
  849. def test_wosensor_passive_only():
  850. """Test parsing wosensor with only passive data."""
  851. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  852. adv_data = generate_advertisement_data(
  853. manufacturer_data={2409: b"\xd7\xc1}]\xebC\xde\x03\x06\x985"},
  854. service_data={},
  855. tx_power=-127,
  856. rssi=-50,
  857. )
  858. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER)
  859. assert result == SwitchBotAdvertisement(
  860. address="aa:bb:cc:dd:ee:ff",
  861. data={
  862. "data": {
  863. "battery": None,
  864. "fahrenheit": False,
  865. "humidity": 53,
  866. "temp": {"c": 24.6, "f": 76.28},
  867. "temperature": 24.6,
  868. },
  869. "isEncrypted": False,
  870. "model": "T",
  871. "modelFriendlyName": "Meter",
  872. "modelName": SwitchbotModel.METER,
  873. "rawAdvData": None,
  874. },
  875. device=ble_device,
  876. rssi=-50,
  877. active=False,
  878. )
  879. def test_wosensor_active_zero_data():
  880. """Test parsing wosensor with active data but all values are zero."""
  881. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  882. adv_data = generate_advertisement_data(
  883. manufacturer_data={},
  884. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\x00\x00\x00\x00"},
  885. tx_power=-127,
  886. rssi=-50,
  887. )
  888. result = parse_advertisement_data(ble_device, adv_data)
  889. assert result == SwitchBotAdvertisement(
  890. address="aa:bb:cc:dd:ee:ff",
  891. data={
  892. "data": {},
  893. "isEncrypted": False,
  894. "model": "T",
  895. "rawAdvData": b"T\x00\x00\x00\x00\x00",
  896. },
  897. device=ble_device,
  898. rssi=-50,
  899. active=True,
  900. )
  901. def test_wohub2_passive_and_active():
  902. """Test parsing wosensor as passive with active data as well."""
  903. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  904. adv_data = generate_advertisement_data(
  905. manufacturer_data={
  906. 2409: b"\xaa\xbb\xcc\xdd\xee\xff\x00\xfffT\x1a\xf1\x82\x07\x9a2\x00"
  907. },
  908. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"v\x00"},
  909. tx_power=-127,
  910. rssi=-50,
  911. )
  912. result = parse_advertisement_data(ble_device, adv_data)
  913. assert result == SwitchBotAdvertisement(
  914. address="aa:bb:cc:dd:ee:ff",
  915. data={
  916. "data": {
  917. "fahrenheit": False,
  918. "humidity": 50,
  919. "lightLevel": 2,
  920. "temp": {"c": 26.7, "f": 80.06},
  921. "temperature": 26.7,
  922. },
  923. "isEncrypted": False,
  924. "model": "v",
  925. "modelFriendlyName": "Hub 2",
  926. "modelName": SwitchbotModel.HUB2,
  927. "rawAdvData": b"v\x00",
  928. },
  929. device=ble_device,
  930. rssi=-50,
  931. active=True,
  932. )
  933. def test_woiosensor_passive_and_active():
  934. """Test parsing woiosensor as passive with active data as well."""
  935. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  936. adv_data = generate_advertisement_data(
  937. manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xe0\x0f\x06\x985\x00"},
  938. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"w\x00\xe4"},
  939. tx_power=-127,
  940. rssi=-50,
  941. )
  942. result = parse_advertisement_data(ble_device, adv_data)
  943. assert result == SwitchBotAdvertisement(
  944. address="aa:bb:cc:dd:ee:ff",
  945. data={
  946. "data": {
  947. "battery": 100,
  948. "fahrenheit": False,
  949. "humidity": 53,
  950. "temp": {"c": 24.6, "f": 76.28},
  951. "temperature": 24.6,
  952. },
  953. "isEncrypted": False,
  954. "model": "w",
  955. "modelFriendlyName": "Indoor/Outdoor Meter",
  956. "modelName": SwitchbotModel.IO_METER,
  957. "rawAdvData": b"w\x00\xe4",
  958. },
  959. device=ble_device,
  960. rssi=-50,
  961. active=True,
  962. )
  963. def test_woiosensor_passive_only():
  964. """Test parsing woiosensor with only passive data."""
  965. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  966. adv_data = generate_advertisement_data(
  967. manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xe0\x0f\x06\x985\x00"},
  968. service_data={},
  969. tx_power=-127,
  970. rssi=-50,
  971. )
  972. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.IO_METER)
  973. assert result == SwitchBotAdvertisement(
  974. address="aa:bb:cc:dd:ee:ff",
  975. data={
  976. "data": {
  977. "battery": None,
  978. "fahrenheit": False,
  979. "humidity": 53,
  980. "temp": {"c": 24.6, "f": 76.28},
  981. "temperature": 24.6,
  982. },
  983. "isEncrypted": False,
  984. "model": "w",
  985. "modelFriendlyName": "Indoor/Outdoor Meter",
  986. "modelName": SwitchbotModel.IO_METER,
  987. "rawAdvData": None,
  988. },
  989. device=ble_device,
  990. rssi=-50,
  991. active=False,
  992. )
  993. def test_motion_sensor_clear():
  994. """Test parsing motion sensor with clear data."""
  995. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  996. adv_data = generate_advertisement_data(
  997. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIj\x1c\x00f"},
  998. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x00f\x01"},
  999. tx_power=-127,
  1000. rssi=-87,
  1001. )
  1002. result = parse_advertisement_data(
  1003. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1004. )
  1005. assert result == SwitchBotAdvertisement(
  1006. address="aa:bb:cc:dd:ee:ff",
  1007. data={
  1008. "data": {
  1009. "battery": 98,
  1010. "iot": 0,
  1011. "is_light": False,
  1012. "led": 0,
  1013. "light_intensity": 1,
  1014. "motion_detected": False,
  1015. "sense_distance": 0,
  1016. "tested": False,
  1017. },
  1018. "isEncrypted": False,
  1019. "model": "s",
  1020. "modelFriendlyName": "Motion Sensor",
  1021. "modelName": SwitchbotModel.MOTION_SENSOR,
  1022. "rawAdvData": b"s\x00\xe2\x00f\x01",
  1023. },
  1024. device=ble_device,
  1025. rssi=-87,
  1026. active=True,
  1027. )
  1028. def test_motion_sensor_clear_passive():
  1029. """Test parsing motion sensor with clear data."""
  1030. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1031. adv_data = generate_advertisement_data(
  1032. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIj\x1c\x00f"},
  1033. service_data={},
  1034. tx_power=-127,
  1035. rssi=-87,
  1036. )
  1037. result = parse_advertisement_data(
  1038. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1039. )
  1040. assert result == SwitchBotAdvertisement(
  1041. address="aa:bb:cc:dd:ee:ff",
  1042. data={
  1043. "data": {
  1044. "battery": None,
  1045. "iot": None,
  1046. "is_light": False,
  1047. "led": None,
  1048. "light_intensity": None,
  1049. "motion_detected": False,
  1050. "sense_distance": None,
  1051. "tested": None,
  1052. },
  1053. "isEncrypted": False,
  1054. "model": "s",
  1055. "modelFriendlyName": "Motion Sensor",
  1056. "modelName": SwitchbotModel.MOTION_SENSOR,
  1057. "rawAdvData": None,
  1058. },
  1059. device=ble_device,
  1060. rssi=-87,
  1061. active=False,
  1062. )
  1063. def test_motion_sensor_motion():
  1064. """Test parsing motion sensor with motion data."""
  1065. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1066. adv_data = generate_advertisement_data(
  1067. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIi\\\x008"},
  1068. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s@\xe2\x008\x01"},
  1069. tx_power=-127,
  1070. rssi=-87,
  1071. )
  1072. result = parse_advertisement_data(
  1073. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1074. )
  1075. assert result == SwitchBotAdvertisement(
  1076. address="aa:bb:cc:dd:ee:ff",
  1077. data={
  1078. "data": {
  1079. "battery": 98,
  1080. "iot": 0,
  1081. "is_light": False,
  1082. "led": 0,
  1083. "light_intensity": 1,
  1084. "motion_detected": True,
  1085. "sense_distance": 0,
  1086. "tested": False,
  1087. },
  1088. "isEncrypted": False,
  1089. "model": "s",
  1090. "modelFriendlyName": "Motion Sensor",
  1091. "modelName": SwitchbotModel.MOTION_SENSOR,
  1092. "rawAdvData": b"s@\xe2\x008\x01",
  1093. },
  1094. device=ble_device,
  1095. rssi=-87,
  1096. active=True,
  1097. )
  1098. def test_motion_sensor_motion_passive():
  1099. """Test parsing motion sensor with motion data."""
  1100. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1101. adv_data = generate_advertisement_data(
  1102. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIi\\\x008"},
  1103. service_data={},
  1104. tx_power=-127,
  1105. rssi=-87,
  1106. )
  1107. result = parse_advertisement_data(
  1108. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1109. )
  1110. assert result == SwitchBotAdvertisement(
  1111. address="aa:bb:cc:dd:ee:ff",
  1112. data={
  1113. "data": {
  1114. "battery": None,
  1115. "iot": None,
  1116. "is_light": False,
  1117. "led": None,
  1118. "light_intensity": None,
  1119. "motion_detected": True,
  1120. "sense_distance": None,
  1121. "tested": None,
  1122. },
  1123. "isEncrypted": False,
  1124. "model": "s",
  1125. "modelFriendlyName": "Motion Sensor",
  1126. "modelName": SwitchbotModel.MOTION_SENSOR,
  1127. "rawAdvData": None,
  1128. },
  1129. device=ble_device,
  1130. rssi=-87,
  1131. active=False,
  1132. )
  1133. def test_motion_sensor_is_light_passive():
  1134. """Test parsing motion sensor with motion data."""
  1135. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1136. adv_data = generate_advertisement_data(
  1137. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIs,\x04g"},
  1138. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x04g\x02"},
  1139. tx_power=-127,
  1140. rssi=-93,
  1141. )
  1142. result = parse_advertisement_data(
  1143. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1144. )
  1145. assert result == SwitchBotAdvertisement(
  1146. address="aa:bb:cc:dd:ee:ff",
  1147. data={
  1148. "data": {
  1149. "battery": 98,
  1150. "iot": 0,
  1151. "is_light": True,
  1152. "led": 0,
  1153. "light_intensity": 2,
  1154. "motion_detected": False,
  1155. "sense_distance": 0,
  1156. "tested": False,
  1157. },
  1158. "isEncrypted": False,
  1159. "model": "s",
  1160. "modelFriendlyName": "Motion Sensor",
  1161. "modelName": SwitchbotModel.MOTION_SENSOR,
  1162. "rawAdvData": b"s\x00\xe2\x04g\x02",
  1163. },
  1164. device=ble_device,
  1165. rssi=-93,
  1166. active=True,
  1167. )
  1168. def test_motion_sensor_is_light_active():
  1169. """Test parsing motion sensor with motion data."""
  1170. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1171. adv_data = generate_advertisement_data(
  1172. manufacturer_data={},
  1173. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x04g\x02"},
  1174. tx_power=-127,
  1175. rssi=-93,
  1176. )
  1177. result = parse_advertisement_data(
  1178. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1179. )
  1180. assert result == SwitchBotAdvertisement(
  1181. address="aa:bb:cc:dd:ee:ff",
  1182. data={
  1183. "data": {
  1184. "battery": 98,
  1185. "iot": 0,
  1186. "is_light": True,
  1187. "led": 0,
  1188. "light_intensity": 2,
  1189. "motion_detected": False,
  1190. "sense_distance": 0,
  1191. "tested": False,
  1192. },
  1193. "isEncrypted": False,
  1194. "model": "s",
  1195. "modelFriendlyName": "Motion Sensor",
  1196. "modelName": SwitchbotModel.MOTION_SENSOR,
  1197. "rawAdvData": b"s\x00\xe2\x04g\x02",
  1198. },
  1199. device=ble_device,
  1200. rssi=-93,
  1201. active=True,
  1202. )
  1203. def test_motion_with_light_detected():
  1204. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1205. adv_data = generate_advertisement_data(
  1206. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIvl\x00,"},
  1207. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s@\xe2\x00,\x02"},
  1208. tx_power=-127,
  1209. rssi=-84,
  1210. )
  1211. result = parse_advertisement_data(
  1212. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1213. )
  1214. assert result == SwitchBotAdvertisement(
  1215. address="aa:bb:cc:dd:ee:ff",
  1216. data={
  1217. "data": {
  1218. "battery": 98,
  1219. "iot": 0,
  1220. "is_light": True,
  1221. "led": 0,
  1222. "light_intensity": 2,
  1223. "motion_detected": True,
  1224. "sense_distance": 0,
  1225. "tested": False,
  1226. },
  1227. "isEncrypted": False,
  1228. "model": "s",
  1229. "modelFriendlyName": "Motion Sensor",
  1230. "modelName": SwitchbotModel.MOTION_SENSOR,
  1231. "rawAdvData": b"s@\xe2\x00,\x02",
  1232. },
  1233. device=ble_device,
  1234. rssi=-84,
  1235. active=True,
  1236. )
  1237. def test_parsing_lock_active():
  1238. """Test parsing lock with active data."""
  1239. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1240. adv_data = generate_advertisement_data(
  1241. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00 "},
  1242. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"o\x80d"},
  1243. rssi=-67,
  1244. )
  1245. result = parse_advertisement_data(ble_device, adv_data)
  1246. assert result == SwitchBotAdvertisement(
  1247. address="aa:bb:cc:dd:ee:ff",
  1248. data={
  1249. "data": {
  1250. "auto_lock_paused": False,
  1251. "battery": 100,
  1252. "calibration": True,
  1253. "door_open": False,
  1254. "double_lock_mode": False,
  1255. "night_latch": False,
  1256. "status": LockStatus.LOCKED,
  1257. "unclosed_alarm": False,
  1258. "unlocked_alarm": False,
  1259. "update_from_secondary_lock": False,
  1260. },
  1261. "isEncrypted": False,
  1262. "model": "o",
  1263. "modelFriendlyName": "Lock",
  1264. "modelName": SwitchbotModel.LOCK,
  1265. "rawAdvData": b"o\x80d",
  1266. },
  1267. device=ble_device,
  1268. rssi=-67,
  1269. active=True,
  1270. )
  1271. def test_parsing_lock_passive():
  1272. """Test parsing lock with active data."""
  1273. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1274. adv_data = generate_advertisement_data(
  1275. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00 "}, rssi=-67
  1276. )
  1277. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK)
  1278. assert result == SwitchBotAdvertisement(
  1279. address="aa:bb:cc:dd:ee:ff",
  1280. data={
  1281. "data": {
  1282. "auto_lock_paused": False,
  1283. "battery": None,
  1284. "calibration": True,
  1285. "door_open": False,
  1286. "double_lock_mode": False,
  1287. "night_latch": False,
  1288. "status": LockStatus.LOCKED,
  1289. "unclosed_alarm": False,
  1290. "unlocked_alarm": False,
  1291. "update_from_secondary_lock": False,
  1292. },
  1293. "isEncrypted": False,
  1294. "model": "o",
  1295. "modelFriendlyName": "Lock",
  1296. "modelName": SwitchbotModel.LOCK,
  1297. "rawAdvData": None,
  1298. },
  1299. device=ble_device,
  1300. rssi=-67,
  1301. active=False,
  1302. )
  1303. def test_parsing_lock_pro_active():
  1304. """Test parsing lock pro with active data."""
  1305. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1306. adv_data = generate_advertisement_data(
  1307. manufacturer_data={2409: b"\xc8\xf5,\xd9-V\x07\x82\x00d\x00\x00"},
  1308. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"$\x80d"},
  1309. rssi=-80,
  1310. )
  1311. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK_PRO)
  1312. assert result == SwitchBotAdvertisement(
  1313. address="aa:bb:cc:dd:ee:ff",
  1314. data={
  1315. "data": {
  1316. "battery": 100,
  1317. "calibration": True,
  1318. "status": LockStatus.LOCKED,
  1319. "update_from_secondary_lock": False,
  1320. "door_open": False,
  1321. "double_lock_mode": False,
  1322. "unclosed_alarm": False,
  1323. "unlocked_alarm": False,
  1324. "auto_lock_paused": False,
  1325. "night_latch": False,
  1326. },
  1327. "model": "$",
  1328. "isEncrypted": False,
  1329. "modelFriendlyName": "Lock Pro",
  1330. "modelName": SwitchbotModel.LOCK_PRO,
  1331. "rawAdvData": b"$\x80d",
  1332. },
  1333. device=ble_device,
  1334. rssi=-80,
  1335. active=True,
  1336. )
  1337. def test_parsing_lock_pro_passive():
  1338. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1339. adv_data = generate_advertisement_data(
  1340. manufacturer_data={2409: bytes.fromhex("aabbccddeeff208200640000")}, rssi=-67
  1341. )
  1342. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK_PRO)
  1343. assert result == SwitchBotAdvertisement(
  1344. address="aa:bb:cc:dd:ee:ff",
  1345. data={
  1346. "data": {
  1347. "battery": None,
  1348. "calibration": True,
  1349. "status": LockStatus.LOCKED,
  1350. "update_from_secondary_lock": False,
  1351. "door_open": False,
  1352. "double_lock_mode": False,
  1353. "unclosed_alarm": False,
  1354. "unlocked_alarm": False,
  1355. "auto_lock_paused": False,
  1356. "night_latch": False,
  1357. },
  1358. "model": "$",
  1359. "isEncrypted": False,
  1360. "modelFriendlyName": "Lock Pro",
  1361. "modelName": SwitchbotModel.LOCK_PRO,
  1362. "rawAdvData": None,
  1363. },
  1364. device=ble_device,
  1365. rssi=-67,
  1366. active=False,
  1367. )
  1368. def test_parsing_lock_pro_passive_nightlatch_disabled():
  1369. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1370. adv_data = generate_advertisement_data(
  1371. manufacturer_data={2409: bytes.fromhex("aabbccddeeff0a8200630000")}, rssi=-67
  1372. )
  1373. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK_PRO)
  1374. assert result == SwitchBotAdvertisement(
  1375. address="aa:bb:cc:dd:ee:ff",
  1376. data={
  1377. "data": {
  1378. "battery": None,
  1379. "calibration": True,
  1380. "status": LockStatus.LOCKED,
  1381. "update_from_secondary_lock": False,
  1382. "door_open": False,
  1383. "double_lock_mode": False,
  1384. "unclosed_alarm": False,
  1385. "unlocked_alarm": False,
  1386. "auto_lock_paused": False,
  1387. "night_latch": False,
  1388. },
  1389. "model": "$",
  1390. "isEncrypted": False,
  1391. "modelFriendlyName": "Lock Pro",
  1392. "modelName": SwitchbotModel.LOCK_PRO,
  1393. "rawAdvData": None,
  1394. },
  1395. device=ble_device,
  1396. rssi=-67,
  1397. active=False,
  1398. )
  1399. def test_parsing_lock_active_old_firmware():
  1400. """Test parsing lock with active data. Old firmware."""
  1401. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1402. adv_data = generate_advertisement_data(
  1403. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00"},
  1404. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"o\x80d"},
  1405. rssi=-67,
  1406. )
  1407. result = parse_advertisement_data(ble_device, adv_data)
  1408. assert result == SwitchBotAdvertisement(
  1409. address="aa:bb:cc:dd:ee:ff",
  1410. data={
  1411. "data": {
  1412. "auto_lock_paused": False,
  1413. "battery": 100,
  1414. "calibration": True,
  1415. "door_open": False,
  1416. "double_lock_mode": False,
  1417. "night_latch": False,
  1418. "status": LockStatus.LOCKED,
  1419. "unclosed_alarm": False,
  1420. "unlocked_alarm": False,
  1421. "update_from_secondary_lock": False,
  1422. },
  1423. "isEncrypted": False,
  1424. "model": "o",
  1425. "modelFriendlyName": "Lock",
  1426. "modelName": SwitchbotModel.LOCK,
  1427. "rawAdvData": b"o\x80d",
  1428. },
  1429. device=ble_device,
  1430. rssi=-67,
  1431. active=True,
  1432. )
  1433. def test_parsing_lock_passive_old_firmware():
  1434. """Test parsing lock with active data. Old firmware."""
  1435. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1436. adv_data = generate_advertisement_data(
  1437. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00"}, rssi=-67
  1438. )
  1439. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK)
  1440. assert result == SwitchBotAdvertisement(
  1441. address="aa:bb:cc:dd:ee:ff",
  1442. data={
  1443. "data": {
  1444. "auto_lock_paused": False,
  1445. "battery": None,
  1446. "calibration": True,
  1447. "door_open": False,
  1448. "double_lock_mode": False,
  1449. "night_latch": False,
  1450. "status": LockStatus.LOCKED,
  1451. "unclosed_alarm": False,
  1452. "unlocked_alarm": False,
  1453. "update_from_secondary_lock": False,
  1454. },
  1455. "isEncrypted": False,
  1456. "model": "o",
  1457. "modelFriendlyName": "Lock",
  1458. "modelName": SwitchbotModel.LOCK,
  1459. "rawAdvData": None,
  1460. },
  1461. device=ble_device,
  1462. rssi=-67,
  1463. active=False,
  1464. )
  1465. def test_meter_pro_active() -> None:
  1466. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1467. adv_data = generate_advertisement_data(
  1468. manufacturer_data={2409: b"\xb0\xe9\xfeR\xdd\x84\x06d\x08\x97,\x00\x05"},
  1469. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"4\x00d"},
  1470. rssi=-67,
  1471. )
  1472. result = parse_advertisement_data(ble_device, adv_data)
  1473. assert result == SwitchBotAdvertisement(
  1474. address="aa:bb:cc:dd:ee:ff",
  1475. data={
  1476. "data": {
  1477. "battery": 100,
  1478. "fahrenheit": False,
  1479. "humidity": 44,
  1480. "temp": {"c": 23.8, "f": 74.84},
  1481. "temperature": 23.8,
  1482. },
  1483. "isEncrypted": False,
  1484. "model": "4",
  1485. "modelFriendlyName": "Meter",
  1486. "modelName": SwitchbotModel.METER_PRO,
  1487. "rawAdvData": b"4\x00d",
  1488. },
  1489. device=ble_device,
  1490. rssi=-67,
  1491. active=True,
  1492. )
  1493. def test_meter_pro_passive() -> None:
  1494. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1495. adv_data = generate_advertisement_data(
  1496. manufacturer_data={2409: b"\xb0\xe9\xfeR\xdd\x84\x06d\x08\x97,\x00\x05"},
  1497. rssi=-67,
  1498. )
  1499. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER_PRO)
  1500. import pprint
  1501. pprint.pprint(result)
  1502. assert result == SwitchBotAdvertisement(
  1503. address="aa:bb:cc:dd:ee:ff",
  1504. data={
  1505. "data": {
  1506. "battery": None,
  1507. "fahrenheit": False,
  1508. "humidity": 44,
  1509. "temp": {"c": 23.8, "f": 74.84},
  1510. "temperature": 23.8,
  1511. },
  1512. "isEncrypted": False,
  1513. "model": "4",
  1514. "modelFriendlyName": "Meter",
  1515. "modelName": SwitchbotModel.METER_PRO,
  1516. "rawAdvData": None,
  1517. },
  1518. device=ble_device,
  1519. rssi=-67,
  1520. active=False,
  1521. )
  1522. def test_meter_pro_c_active() -> None:
  1523. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1524. adv_data = generate_advertisement_data(
  1525. manufacturer_data={
  1526. 2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
  1527. },
  1528. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"5\x00d"},
  1529. rssi=-67,
  1530. )
  1531. result = parse_advertisement_data(ble_device, adv_data)
  1532. assert result == SwitchBotAdvertisement(
  1533. address="aa:bb:cc:dd:ee:ff",
  1534. data={
  1535. "data": {
  1536. "battery": 100,
  1537. "fahrenheit": True,
  1538. "humidity": 36,
  1539. "temp": {"c": 27.7, "f": 81.86},
  1540. "temperature": 27.7,
  1541. },
  1542. "isEncrypted": False,
  1543. "model": "5",
  1544. "modelFriendlyName": "Meter",
  1545. "modelName": SwitchbotModel.METER_PRO_C,
  1546. "rawAdvData": b"5\x00d",
  1547. },
  1548. device=ble_device,
  1549. rssi=-67,
  1550. active=True,
  1551. )
  1552. def test_meter_pro_c_passive() -> None:
  1553. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1554. adv_data = generate_advertisement_data(
  1555. manufacturer_data={
  1556. 2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
  1557. },
  1558. rssi=-67,
  1559. )
  1560. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER_PRO_C)
  1561. assert result == SwitchBotAdvertisement(
  1562. address="aa:bb:cc:dd:ee:ff",
  1563. data={
  1564. "data": {
  1565. "battery": None,
  1566. "fahrenheit": True,
  1567. "humidity": 36,
  1568. "temp": {"c": 27.7, "f": 81.86},
  1569. "temperature": 27.7,
  1570. },
  1571. "isEncrypted": False,
  1572. "model": "5",
  1573. "modelFriendlyName": "Meter",
  1574. "modelName": SwitchbotModel.METER_PRO_C,
  1575. "rawAdvData": None,
  1576. },
  1577. device=ble_device,
  1578. rssi=-67,
  1579. active=False,
  1580. )