test_adv_parser.py 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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_wosensor_passive_and_active():
  755. """Test parsing wosensor as passive with active data as well."""
  756. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  757. adv_data = generate_advertisement_data(
  758. manufacturer_data={2409: b"\xd7\xc1}]\xebC\xde\x03\x06\x985"},
  759. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\xe4\x06\x985"},
  760. tx_power=-127,
  761. rssi=-50,
  762. )
  763. result = parse_advertisement_data(ble_device, adv_data)
  764. assert result == SwitchBotAdvertisement(
  765. address="aa:bb:cc:dd:ee:ff",
  766. data={
  767. "data": {
  768. "battery": 100,
  769. "fahrenheit": False,
  770. "humidity": 53,
  771. "temp": {"c": 24.6, "f": 76.28},
  772. "temperature": 24.6,
  773. },
  774. "isEncrypted": False,
  775. "model": "T",
  776. "modelFriendlyName": "Meter",
  777. "modelName": SwitchbotModel.METER,
  778. "rawAdvData": b"T\x00\xe4\x06\x985",
  779. },
  780. device=ble_device,
  781. rssi=-50,
  782. active=True,
  783. )
  784. def test_wosensor_active():
  785. """Test parsing wosensor with active data as well."""
  786. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  787. adv_data = generate_advertisement_data(
  788. manufacturer_data={},
  789. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\xe4\x06\x985"},
  790. tx_power=-127,
  791. rssi=-50,
  792. )
  793. result = parse_advertisement_data(ble_device, adv_data)
  794. assert result == SwitchBotAdvertisement(
  795. address="aa:bb:cc:dd:ee:ff",
  796. data={
  797. "data": {
  798. "battery": 100,
  799. "fahrenheit": False,
  800. "humidity": 53,
  801. "temp": {"c": 24.6, "f": 76.28},
  802. "temperature": 24.6,
  803. },
  804. "isEncrypted": False,
  805. "model": "T",
  806. "modelFriendlyName": "Meter",
  807. "modelName": SwitchbotModel.METER,
  808. "rawAdvData": b"T\x00\xe4\x06\x985",
  809. },
  810. device=ble_device,
  811. rssi=-50,
  812. active=True,
  813. )
  814. def test_wosensor_passive_only():
  815. """Test parsing wosensor with only passive data."""
  816. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  817. adv_data = generate_advertisement_data(
  818. manufacturer_data={2409: b"\xd7\xc1}]\xebC\xde\x03\x06\x985"},
  819. service_data={},
  820. tx_power=-127,
  821. rssi=-50,
  822. )
  823. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER)
  824. assert result == SwitchBotAdvertisement(
  825. address="aa:bb:cc:dd:ee:ff",
  826. data={
  827. "data": {
  828. "battery": None,
  829. "fahrenheit": False,
  830. "humidity": 53,
  831. "temp": {"c": 24.6, "f": 76.28},
  832. "temperature": 24.6,
  833. },
  834. "isEncrypted": False,
  835. "model": "T",
  836. "modelFriendlyName": "Meter",
  837. "modelName": SwitchbotModel.METER,
  838. "rawAdvData": None,
  839. },
  840. device=ble_device,
  841. rssi=-50,
  842. active=False,
  843. )
  844. def test_wosensor_active_zero_data():
  845. """Test parsing wosensor with active data but all values are zero."""
  846. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  847. adv_data = generate_advertisement_data(
  848. manufacturer_data={},
  849. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\x00\x00\x00\x00"},
  850. tx_power=-127,
  851. rssi=-50,
  852. )
  853. result = parse_advertisement_data(ble_device, adv_data)
  854. assert result == SwitchBotAdvertisement(
  855. address="aa:bb:cc:dd:ee:ff",
  856. data={
  857. "data": {},
  858. "isEncrypted": False,
  859. "model": "T",
  860. "rawAdvData": b"T\x00\x00\x00\x00\x00",
  861. },
  862. device=ble_device,
  863. rssi=-50,
  864. active=True,
  865. )
  866. def test_wohub2_passive_and_active():
  867. """Test parsing wosensor as passive with active data as well."""
  868. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  869. adv_data = generate_advertisement_data(
  870. manufacturer_data={
  871. 2409: b"\xaa\xbb\xcc\xdd\xee\xff\x00\xfffT\x1a\xf1\x82\x07\x9a2\x00"
  872. },
  873. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"v\x00"},
  874. tx_power=-127,
  875. rssi=-50,
  876. )
  877. result = parse_advertisement_data(ble_device, adv_data)
  878. assert result == SwitchBotAdvertisement(
  879. address="aa:bb:cc:dd:ee:ff",
  880. data={
  881. "data": {
  882. "fahrenheit": False,
  883. "humidity": 50,
  884. "lightLevel": 2,
  885. "temp": {"c": 26.7, "f": 80.06},
  886. "temperature": 26.7,
  887. },
  888. "isEncrypted": False,
  889. "model": "v",
  890. "modelFriendlyName": "Hub 2",
  891. "modelName": SwitchbotModel.HUB2,
  892. "rawAdvData": b"v\x00",
  893. },
  894. device=ble_device,
  895. rssi=-50,
  896. active=True,
  897. )
  898. def test_woiosensor_passive_and_active():
  899. """Test parsing woiosensor as passive with active data as well."""
  900. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  901. adv_data = generate_advertisement_data(
  902. manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xe0\x0f\x06\x985\x00"},
  903. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"w\x00\xe4"},
  904. tx_power=-127,
  905. rssi=-50,
  906. )
  907. result = parse_advertisement_data(ble_device, adv_data)
  908. assert result == SwitchBotAdvertisement(
  909. address="aa:bb:cc:dd:ee:ff",
  910. data={
  911. "data": {
  912. "battery": 100,
  913. "fahrenheit": False,
  914. "humidity": 53,
  915. "temp": {"c": 24.6, "f": 76.28},
  916. "temperature": 24.6,
  917. },
  918. "isEncrypted": False,
  919. "model": "w",
  920. "modelFriendlyName": "Indoor/Outdoor Meter",
  921. "modelName": SwitchbotModel.IO_METER,
  922. "rawAdvData": b"w\x00\xe4",
  923. },
  924. device=ble_device,
  925. rssi=-50,
  926. active=True,
  927. )
  928. def test_woiosensor_passive_only():
  929. """Test parsing woiosensor with only passive data."""
  930. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  931. adv_data = generate_advertisement_data(
  932. manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xe0\x0f\x06\x985\x00"},
  933. service_data={},
  934. tx_power=-127,
  935. rssi=-50,
  936. )
  937. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.IO_METER)
  938. assert result == SwitchBotAdvertisement(
  939. address="aa:bb:cc:dd:ee:ff",
  940. data={
  941. "data": {
  942. "battery": None,
  943. "fahrenheit": False,
  944. "humidity": 53,
  945. "temp": {"c": 24.6, "f": 76.28},
  946. "temperature": 24.6,
  947. },
  948. "isEncrypted": False,
  949. "model": "w",
  950. "modelFriendlyName": "Indoor/Outdoor Meter",
  951. "modelName": SwitchbotModel.IO_METER,
  952. "rawAdvData": None,
  953. },
  954. device=ble_device,
  955. rssi=-50,
  956. active=False,
  957. )
  958. def test_motion_sensor_clear():
  959. """Test parsing motion sensor with clear data."""
  960. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  961. adv_data = generate_advertisement_data(
  962. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIj\x1c\x00f"},
  963. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x00f\x01"},
  964. tx_power=-127,
  965. rssi=-87,
  966. )
  967. result = parse_advertisement_data(
  968. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  969. )
  970. assert result == SwitchBotAdvertisement(
  971. address="aa:bb:cc:dd:ee:ff",
  972. data={
  973. "data": {
  974. "battery": 98,
  975. "iot": 0,
  976. "is_light": False,
  977. "led": 0,
  978. "light_intensity": 1,
  979. "motion_detected": False,
  980. "sense_distance": 0,
  981. "tested": False,
  982. },
  983. "isEncrypted": False,
  984. "model": "s",
  985. "modelFriendlyName": "Motion Sensor",
  986. "modelName": SwitchbotModel.MOTION_SENSOR,
  987. "rawAdvData": b"s\x00\xe2\x00f\x01",
  988. },
  989. device=ble_device,
  990. rssi=-87,
  991. active=True,
  992. )
  993. def test_motion_sensor_clear_passive():
  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={},
  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": None,
  1010. "iot": None,
  1011. "is_light": False,
  1012. "led": None,
  1013. "light_intensity": None,
  1014. "motion_detected": False,
  1015. "sense_distance": None,
  1016. "tested": None,
  1017. },
  1018. "isEncrypted": False,
  1019. "model": "s",
  1020. "modelFriendlyName": "Motion Sensor",
  1021. "modelName": SwitchbotModel.MOTION_SENSOR,
  1022. "rawAdvData": None,
  1023. },
  1024. device=ble_device,
  1025. rssi=-87,
  1026. active=False,
  1027. )
  1028. def test_motion_sensor_motion():
  1029. """Test parsing motion sensor with motion 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\xbcIi\\\x008"},
  1033. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s@\xe2\x008\x01"},
  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": 98,
  1045. "iot": 0,
  1046. "is_light": False,
  1047. "led": 0,
  1048. "light_intensity": 1,
  1049. "motion_detected": True,
  1050. "sense_distance": 0,
  1051. "tested": False,
  1052. },
  1053. "isEncrypted": False,
  1054. "model": "s",
  1055. "modelFriendlyName": "Motion Sensor",
  1056. "modelName": SwitchbotModel.MOTION_SENSOR,
  1057. "rawAdvData": b"s@\xe2\x008\x01",
  1058. },
  1059. device=ble_device,
  1060. rssi=-87,
  1061. active=True,
  1062. )
  1063. def test_motion_sensor_motion_passive():
  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={},
  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": None,
  1080. "iot": None,
  1081. "is_light": False,
  1082. "led": None,
  1083. "light_intensity": None,
  1084. "motion_detected": True,
  1085. "sense_distance": None,
  1086. "tested": None,
  1087. },
  1088. "isEncrypted": False,
  1089. "model": "s",
  1090. "modelFriendlyName": "Motion Sensor",
  1091. "modelName": SwitchbotModel.MOTION_SENSOR,
  1092. "rawAdvData": None,
  1093. },
  1094. device=ble_device,
  1095. rssi=-87,
  1096. active=False,
  1097. )
  1098. def test_motion_sensor_is_light_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\xbcIs,\x04g"},
  1103. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x04g\x02"},
  1104. tx_power=-127,
  1105. rssi=-93,
  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": 98,
  1115. "iot": 0,
  1116. "is_light": True,
  1117. "led": 0,
  1118. "light_intensity": 2,
  1119. "motion_detected": False,
  1120. "sense_distance": 0,
  1121. "tested": False,
  1122. },
  1123. "isEncrypted": False,
  1124. "model": "s",
  1125. "modelFriendlyName": "Motion Sensor",
  1126. "modelName": SwitchbotModel.MOTION_SENSOR,
  1127. "rawAdvData": b"s\x00\xe2\x04g\x02",
  1128. },
  1129. device=ble_device,
  1130. rssi=-93,
  1131. active=True,
  1132. )
  1133. def test_motion_sensor_is_light_active():
  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={},
  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_with_light_detected():
  1169. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1170. adv_data = generate_advertisement_data(
  1171. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIvl\x00,"},
  1172. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s@\xe2\x00,\x02"},
  1173. tx_power=-127,
  1174. rssi=-84,
  1175. )
  1176. result = parse_advertisement_data(
  1177. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1178. )
  1179. assert result == SwitchBotAdvertisement(
  1180. address="aa:bb:cc:dd:ee:ff",
  1181. data={
  1182. "data": {
  1183. "battery": 98,
  1184. "iot": 0,
  1185. "is_light": True,
  1186. "led": 0,
  1187. "light_intensity": 2,
  1188. "motion_detected": True,
  1189. "sense_distance": 0,
  1190. "tested": False,
  1191. },
  1192. "isEncrypted": False,
  1193. "model": "s",
  1194. "modelFriendlyName": "Motion Sensor",
  1195. "modelName": SwitchbotModel.MOTION_SENSOR,
  1196. "rawAdvData": b"s@\xe2\x00,\x02",
  1197. },
  1198. device=ble_device,
  1199. rssi=-84,
  1200. active=True,
  1201. )
  1202. def test_parsing_lock_active():
  1203. """Test parsing lock with active data."""
  1204. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1205. adv_data = generate_advertisement_data(
  1206. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00 "},
  1207. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"o\x80d"},
  1208. rssi=-67,
  1209. )
  1210. result = parse_advertisement_data(ble_device, adv_data)
  1211. assert result == SwitchBotAdvertisement(
  1212. address="aa:bb:cc:dd:ee:ff",
  1213. data={
  1214. "data": {
  1215. "auto_lock_paused": False,
  1216. "battery": 100,
  1217. "calibration": True,
  1218. "door_open": False,
  1219. "double_lock_mode": False,
  1220. "night_latch": False,
  1221. "status": LockStatus.LOCKED,
  1222. "unclosed_alarm": False,
  1223. "unlocked_alarm": False,
  1224. "update_from_secondary_lock": False,
  1225. },
  1226. "isEncrypted": False,
  1227. "model": "o",
  1228. "modelFriendlyName": "Lock",
  1229. "modelName": SwitchbotModel.LOCK,
  1230. "rawAdvData": b"o\x80d",
  1231. },
  1232. device=ble_device,
  1233. rssi=-67,
  1234. active=True,
  1235. )
  1236. def test_parsing_lock_passive():
  1237. """Test parsing lock with active data."""
  1238. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1239. adv_data = generate_advertisement_data(
  1240. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00 "}, rssi=-67
  1241. )
  1242. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK)
  1243. assert result == SwitchBotAdvertisement(
  1244. address="aa:bb:cc:dd:ee:ff",
  1245. data={
  1246. "data": {
  1247. "auto_lock_paused": False,
  1248. "battery": None,
  1249. "calibration": True,
  1250. "door_open": False,
  1251. "double_lock_mode": False,
  1252. "night_latch": False,
  1253. "status": LockStatus.LOCKED,
  1254. "unclosed_alarm": False,
  1255. "unlocked_alarm": False,
  1256. "update_from_secondary_lock": False,
  1257. },
  1258. "isEncrypted": False,
  1259. "model": "o",
  1260. "modelFriendlyName": "Lock",
  1261. "modelName": SwitchbotModel.LOCK,
  1262. "rawAdvData": None,
  1263. },
  1264. device=ble_device,
  1265. rssi=-67,
  1266. active=False,
  1267. )
  1268. def test_parsing_lock_pro_active():
  1269. """Test parsing lock pro with active data."""
  1270. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1271. adv_data = generate_advertisement_data(
  1272. manufacturer_data={2409: b"\xc8\xf5,\xd9-V\x07\x82\x00d\x00\x00"},
  1273. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"$\x80d"},
  1274. rssi=-80,
  1275. )
  1276. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK_PRO)
  1277. assert result == SwitchBotAdvertisement(
  1278. address="aa:bb:cc:dd:ee:ff",
  1279. data={
  1280. "data": {
  1281. "battery": 100,
  1282. "calibration": True,
  1283. "status": LockStatus.LOCKED,
  1284. "update_from_secondary_lock": False,
  1285. "door_open": False,
  1286. "double_lock_mode": False,
  1287. "unclosed_alarm": False,
  1288. "unlocked_alarm": False,
  1289. "auto_lock_paused": False,
  1290. "night_latch": False,
  1291. },
  1292. "model": "$",
  1293. "isEncrypted": False,
  1294. "modelFriendlyName": "Lock Pro",
  1295. "modelName": SwitchbotModel.LOCK_PRO,
  1296. "rawAdvData": b"$\x80d",
  1297. },
  1298. device=ble_device,
  1299. rssi=-80,
  1300. active=True,
  1301. )
  1302. def test_parsing_lock_pro_passive():
  1303. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1304. adv_data = generate_advertisement_data(
  1305. manufacturer_data={2409: bytes.fromhex("aabbccddeeff208200640000")}, rssi=-67
  1306. )
  1307. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK_PRO)
  1308. assert result == SwitchBotAdvertisement(
  1309. address="aa:bb:cc:dd:ee:ff",
  1310. data={
  1311. "data": {
  1312. "battery": None,
  1313. "calibration": True,
  1314. "status": LockStatus.LOCKED,
  1315. "update_from_secondary_lock": False,
  1316. "door_open": False,
  1317. "double_lock_mode": False,
  1318. "unclosed_alarm": False,
  1319. "unlocked_alarm": False,
  1320. "auto_lock_paused": False,
  1321. "night_latch": False,
  1322. },
  1323. "model": "$",
  1324. "isEncrypted": False,
  1325. "modelFriendlyName": "Lock Pro",
  1326. "modelName": SwitchbotModel.LOCK_PRO,
  1327. "rawAdvData": None,
  1328. },
  1329. device=ble_device,
  1330. rssi=-67,
  1331. active=False,
  1332. )
  1333. def test_parsing_lock_pro_passive_nightlatch_disabled():
  1334. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1335. adv_data = generate_advertisement_data(
  1336. manufacturer_data={2409: bytes.fromhex("aabbccddeeff0a8200630000")}, rssi=-67
  1337. )
  1338. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK_PRO)
  1339. assert result == SwitchBotAdvertisement(
  1340. address="aa:bb:cc:dd:ee:ff",
  1341. data={
  1342. "data": {
  1343. "battery": None,
  1344. "calibration": True,
  1345. "status": LockStatus.LOCKED,
  1346. "update_from_secondary_lock": False,
  1347. "door_open": False,
  1348. "double_lock_mode": False,
  1349. "unclosed_alarm": False,
  1350. "unlocked_alarm": False,
  1351. "auto_lock_paused": False,
  1352. "night_latch": False,
  1353. },
  1354. "model": "$",
  1355. "isEncrypted": False,
  1356. "modelFriendlyName": "Lock Pro",
  1357. "modelName": SwitchbotModel.LOCK_PRO,
  1358. "rawAdvData": None,
  1359. },
  1360. device=ble_device,
  1361. rssi=-67,
  1362. active=False,
  1363. )
  1364. def test_parsing_lock_active_old_firmware():
  1365. """Test parsing lock with active data. Old firmware."""
  1366. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1367. adv_data = generate_advertisement_data(
  1368. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00"},
  1369. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"o\x80d"},
  1370. rssi=-67,
  1371. )
  1372. result = parse_advertisement_data(ble_device, adv_data)
  1373. assert result == SwitchBotAdvertisement(
  1374. address="aa:bb:cc:dd:ee:ff",
  1375. data={
  1376. "data": {
  1377. "auto_lock_paused": False,
  1378. "battery": 100,
  1379. "calibration": True,
  1380. "door_open": False,
  1381. "double_lock_mode": False,
  1382. "night_latch": False,
  1383. "status": LockStatus.LOCKED,
  1384. "unclosed_alarm": False,
  1385. "unlocked_alarm": False,
  1386. "update_from_secondary_lock": False,
  1387. },
  1388. "isEncrypted": False,
  1389. "model": "o",
  1390. "modelFriendlyName": "Lock",
  1391. "modelName": SwitchbotModel.LOCK,
  1392. "rawAdvData": b"o\x80d",
  1393. },
  1394. device=ble_device,
  1395. rssi=-67,
  1396. active=True,
  1397. )
  1398. def test_parsing_lock_passive_old_firmware():
  1399. """Test parsing lock with active data. Old firmware."""
  1400. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1401. adv_data = generate_advertisement_data(
  1402. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00"}, rssi=-67
  1403. )
  1404. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK)
  1405. assert result == SwitchBotAdvertisement(
  1406. address="aa:bb:cc:dd:ee:ff",
  1407. data={
  1408. "data": {
  1409. "auto_lock_paused": False,
  1410. "battery": None,
  1411. "calibration": True,
  1412. "door_open": False,
  1413. "double_lock_mode": False,
  1414. "night_latch": False,
  1415. "status": LockStatus.LOCKED,
  1416. "unclosed_alarm": False,
  1417. "unlocked_alarm": False,
  1418. "update_from_secondary_lock": False,
  1419. },
  1420. "isEncrypted": False,
  1421. "model": "o",
  1422. "modelFriendlyName": "Lock",
  1423. "modelName": SwitchbotModel.LOCK,
  1424. "rawAdvData": None,
  1425. },
  1426. device=ble_device,
  1427. rssi=-67,
  1428. active=False,
  1429. )
  1430. def test_meter_pro_active() -> None:
  1431. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1432. adv_data = generate_advertisement_data(
  1433. manufacturer_data={2409: b"\xb0\xe9\xfeR\xdd\x84\x06d\x08\x97,\x00\x05"},
  1434. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"4\x00d"},
  1435. rssi=-67,
  1436. )
  1437. result = parse_advertisement_data(ble_device, adv_data)
  1438. assert result == SwitchBotAdvertisement(
  1439. address="aa:bb:cc:dd:ee:ff",
  1440. data={
  1441. "data": {
  1442. "battery": 100,
  1443. "fahrenheit": False,
  1444. "humidity": 44,
  1445. "temp": {"c": 23.8, "f": 74.84},
  1446. "temperature": 23.8,
  1447. },
  1448. "isEncrypted": False,
  1449. "model": "4",
  1450. "modelFriendlyName": "Meter",
  1451. "modelName": SwitchbotModel.METER_PRO,
  1452. "rawAdvData": b"4\x00d",
  1453. },
  1454. device=ble_device,
  1455. rssi=-67,
  1456. active=True,
  1457. )
  1458. def test_meter_pro_passive() -> None:
  1459. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1460. adv_data = generate_advertisement_data(
  1461. manufacturer_data={2409: b"\xb0\xe9\xfeR\xdd\x84\x06d\x08\x97,\x00\x05"},
  1462. rssi=-67,
  1463. )
  1464. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER_PRO)
  1465. assert result == SwitchBotAdvertisement(
  1466. address="aa:bb:cc:dd:ee:ff",
  1467. data={
  1468. "data": {
  1469. "battery": None,
  1470. "fahrenheit": False,
  1471. "humidity": 44,
  1472. "temp": {"c": 23.8, "f": 74.84},
  1473. "temperature": 23.8,
  1474. },
  1475. "isEncrypted": False,
  1476. "model": "4",
  1477. "modelFriendlyName": "Meter",
  1478. "modelName": SwitchbotModel.METER_PRO,
  1479. "rawAdvData": None,
  1480. },
  1481. device=ble_device,
  1482. rssi=-67,
  1483. active=False,
  1484. )
  1485. def test_meter_pro_c_active() -> None:
  1486. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1487. adv_data = generate_advertisement_data(
  1488. manufacturer_data={
  1489. 2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
  1490. },
  1491. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"5\x00d"},
  1492. rssi=-67,
  1493. )
  1494. result = parse_advertisement_data(ble_device, adv_data)
  1495. assert result == SwitchBotAdvertisement(
  1496. address="aa:bb:cc:dd:ee:ff",
  1497. data={
  1498. "data": {
  1499. "battery": 100,
  1500. "fahrenheit": True,
  1501. "humidity": 36,
  1502. "temp": {"c": 27.7, "f": 81.86},
  1503. "temperature": 27.7,
  1504. "co2": 725,
  1505. },
  1506. "isEncrypted": False,
  1507. "model": "5",
  1508. "modelFriendlyName": "Meter",
  1509. "modelName": SwitchbotModel.METER_PRO_C,
  1510. "rawAdvData": b"5\x00d",
  1511. },
  1512. device=ble_device,
  1513. rssi=-67,
  1514. active=True,
  1515. )
  1516. def test_meter_pro_c_passive() -> None:
  1517. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1518. adv_data = generate_advertisement_data(
  1519. manufacturer_data={
  1520. 2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
  1521. },
  1522. rssi=-67,
  1523. )
  1524. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER_PRO_C)
  1525. assert result == SwitchBotAdvertisement(
  1526. address="aa:bb:cc:dd:ee:ff",
  1527. data={
  1528. "data": {
  1529. "battery": None,
  1530. "fahrenheit": True,
  1531. "humidity": 36,
  1532. "temp": {"c": 27.7, "f": 81.86},
  1533. "temperature": 27.7,
  1534. "co2": 725,
  1535. },
  1536. "isEncrypted": False,
  1537. "model": "5",
  1538. "modelFriendlyName": "Meter",
  1539. "modelName": SwitchbotModel.METER_PRO_C,
  1540. "rawAdvData": None,
  1541. },
  1542. device=ble_device,
  1543. rssi=-67,
  1544. active=False,
  1545. )
  1546. def test_parse_advertisement_data_keypad():
  1547. """Test parse_advertisement_data for the keypad."""
  1548. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1549. adv_data = generate_advertisement_data(
  1550. manufacturer_data={2409: b"\xeb\x13\x02\xe6#\x0f\x8fd\x00\x00\x00\x00"},
  1551. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"y\x00d"},
  1552. rssi=-67,
  1553. )
  1554. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.KEYPAD)
  1555. assert result == SwitchBotAdvertisement(
  1556. address="aa:bb:cc:dd:ee:ff",
  1557. data={
  1558. "data": {"attempt_state": 143, "battery": 100},
  1559. "isEncrypted": False,
  1560. "model": "y",
  1561. "modelFriendlyName": "Keypad",
  1562. "modelName": SwitchbotModel.KEYPAD,
  1563. "rawAdvData": b"y\x00d",
  1564. },
  1565. device=ble_device,
  1566. rssi=-67,
  1567. active=True,
  1568. )
  1569. def test_parse_advertisement_data_relay_switch_1pm():
  1570. """Test parse_advertisement_data for the keypad."""
  1571. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1572. adv_data = generate_advertisement_data(
  1573. manufacturer_data={2409: b"$X|\x0866G\x81\x00\x00\x001\x00\x00\x00\x00"},
  1574. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"<\x00\x00\x00"},
  1575. rssi=-67,
  1576. )
  1577. result = parse_advertisement_data(
  1578. ble_device, adv_data, SwitchbotModel.RELAY_SWITCH_1PM
  1579. )
  1580. assert result == SwitchBotAdvertisement(
  1581. address="aa:bb:cc:dd:ee:ff",
  1582. data={
  1583. "data": {
  1584. "switchMode": True,
  1585. "sequence_number": 71,
  1586. "isOn": True,
  1587. "power": 4.9,
  1588. "voltage": 0,
  1589. "current": 0,
  1590. },
  1591. "isEncrypted": False,
  1592. "model": "<",
  1593. "modelFriendlyName": "Relay Switch 1PM",
  1594. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  1595. "rawAdvData": b"<\x00\x00\x00",
  1596. },
  1597. device=ble_device,
  1598. rssi=-67,
  1599. active=True,
  1600. )
  1601. def test_parse_advertisement_data_relay_switch_1():
  1602. """Test parse_advertisement_data for the keypad."""
  1603. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1604. adv_data = generate_advertisement_data(
  1605. manufacturer_data={2409: b"$X|\x0866G\x81\x00\x00\x001\x00\x00\x00\x00"},
  1606. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b";\x00\x00\x00"},
  1607. rssi=-67,
  1608. )
  1609. result = parse_advertisement_data(
  1610. ble_device, adv_data, SwitchbotModel.RELAY_SWITCH_1
  1611. )
  1612. assert result == SwitchBotAdvertisement(
  1613. address="aa:bb:cc:dd:ee:ff",
  1614. data={
  1615. "data": {
  1616. "switchMode": True,
  1617. "sequence_number": 71,
  1618. "isOn": True,
  1619. },
  1620. "isEncrypted": False,
  1621. "model": ";",
  1622. "modelFriendlyName": "Relay Switch 1",
  1623. "modelName": SwitchbotModel.RELAY_SWITCH_1,
  1624. "rawAdvData": b";\x00\x00\x00",
  1625. },
  1626. device=ble_device,
  1627. rssi=-67,
  1628. active=True,
  1629. )
  1630. def test_leak_active():
  1631. """Test parse_advertisement_data for the leak detector."""
  1632. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1633. adv_data = generate_advertisement_data(
  1634. manufacturer_data={2409: b"\xc4407Lz\x18N\x98g^\x94Q<\x05\x00\x00\x00\x00"},
  1635. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"&\x00N"},
  1636. rssi=-72,
  1637. )
  1638. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LEAK)
  1639. assert result == SwitchBotAdvertisement(
  1640. address="aa:bb:cc:dd:ee:ff",
  1641. data={
  1642. "data": {
  1643. "leak": False,
  1644. "tampered": False,
  1645. "battery": 78,
  1646. "low_battery": False,
  1647. },
  1648. "isEncrypted": False,
  1649. "model": "3",
  1650. "modelFriendlyName": "Leak Detector",
  1651. "modelName": SwitchbotModel.LEAK,
  1652. "rawAdvData": b"&\x00N",
  1653. },
  1654. device=ble_device,
  1655. rssi=-72,
  1656. active=True,
  1657. )
  1658. def test_leak_passive():
  1659. """Test parse_advertisement_data for the leak detector."""
  1660. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1661. adv_data = generate_advertisement_data(
  1662. manufacturer_data={2409: b"\xc4407Lz\x18N\x98g^\x94Q<\x05\x00\x00\x00\x00"},
  1663. rssi=-72,
  1664. )
  1665. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LEAK)
  1666. assert result == SwitchBotAdvertisement(
  1667. address="aa:bb:cc:dd:ee:ff",
  1668. data={
  1669. "data": {},
  1670. "isEncrypted": False,
  1671. "model": "3",
  1672. "rawAdvData": None,
  1673. },
  1674. device=ble_device,
  1675. rssi=-72,
  1676. active=False,
  1677. )
  1678. def test_leak_no_leak_detected():
  1679. """Test parse_advertisement_data for the leak detector."""
  1680. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "Any")
  1681. adv_data = generate_advertisement_data(
  1682. manufacturer_data={
  1683. 2409: b"\xc4407LzJd\x98ga\xc4\n<\x05\x00\x00\x00\x00"
  1684. }, # no leak, batt
  1685. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"&\x00d"},
  1686. rssi=-73,
  1687. )
  1688. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LEAK)
  1689. assert result == SwitchBotAdvertisement(
  1690. address="aa:bb:cc:dd:ee:ff",
  1691. data={
  1692. "data": {
  1693. "leak": False,
  1694. "tampered": False,
  1695. "battery": 100,
  1696. "low_battery": False,
  1697. },
  1698. "isEncrypted": False,
  1699. "model": "3",
  1700. "modelFriendlyName": "Leak Detector",
  1701. "modelName": SwitchbotModel.LEAK,
  1702. "rawAdvData": b"&\x00d",
  1703. },
  1704. device=ble_device,
  1705. rssi=-73,
  1706. active=True,
  1707. )
  1708. def test_leak_leak_detected():
  1709. """Test parse_advertisement_data for the leak detector."""
  1710. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "Any")
  1711. adv_data = generate_advertisement_data(
  1712. manufacturer_data={
  1713. 2409: b"\xc4407LzGd\xf9ga\xc4\x08<\x05\x00\x00\x00\x00"
  1714. }, # leak, batt
  1715. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"&\x00d"},
  1716. rssi=-73,
  1717. )
  1718. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LEAK)
  1719. assert result == SwitchBotAdvertisement(
  1720. address="aa:bb:cc:dd:ee:ff",
  1721. data={
  1722. "data": {
  1723. "leak": True,
  1724. "tampered": False,
  1725. "battery": 100,
  1726. "low_battery": False,
  1727. },
  1728. "isEncrypted": False,
  1729. "model": "3",
  1730. "modelFriendlyName": "Leak Detector",
  1731. "modelName": SwitchbotModel.LEAK,
  1732. "rawAdvData": b"&\x00d",
  1733. },
  1734. device=ble_device,
  1735. rssi=-73,
  1736. active=True,
  1737. )
  1738. def test_leak_low_battery():
  1739. """Test parse_advertisement_data for the leak detector."""
  1740. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "Any")
  1741. adv_data = generate_advertisement_data(
  1742. manufacturer_data={
  1743. 2409: b"\xc4407Lz\x02\t\x98\x00\x00\x00\x00<\x05\x00\x00\x00\x00"
  1744. }, # no leak, low battery
  1745. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"&\x00d"},
  1746. rssi=-73,
  1747. )
  1748. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LEAK)
  1749. assert result == SwitchBotAdvertisement(
  1750. address="aa:bb:cc:dd:ee:ff",
  1751. data={
  1752. "data": {
  1753. "leak": False,
  1754. "tampered": False,
  1755. "battery": 9,
  1756. "low_battery": False,
  1757. },
  1758. "isEncrypted": False,
  1759. "model": "3",
  1760. "modelFriendlyName": "Leak Detector",
  1761. "modelName": SwitchbotModel.LEAK,
  1762. "rawAdvData": b"&\x00d",
  1763. },
  1764. device=ble_device,
  1765. rssi=-73,
  1766. active=True,
  1767. )