test_adv_parser.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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. assert result == SwitchBotAdvertisement(
  1501. address="aa:bb:cc:dd:ee:ff",
  1502. data={
  1503. "data": {
  1504. "battery": None,
  1505. "fahrenheit": False,
  1506. "humidity": 44,
  1507. "temp": {"c": 23.8, "f": 74.84},
  1508. "temperature": 23.8,
  1509. },
  1510. "isEncrypted": False,
  1511. "model": "4",
  1512. "modelFriendlyName": "Meter",
  1513. "modelName": SwitchbotModel.METER_PRO,
  1514. "rawAdvData": None,
  1515. },
  1516. device=ble_device,
  1517. rssi=-67,
  1518. active=False,
  1519. )
  1520. def test_meter_pro_c_active() -> None:
  1521. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1522. adv_data = generate_advertisement_data(
  1523. manufacturer_data={
  1524. 2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
  1525. },
  1526. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"5\x00d"},
  1527. rssi=-67,
  1528. )
  1529. result = parse_advertisement_data(ble_device, adv_data)
  1530. assert result == SwitchBotAdvertisement(
  1531. address="aa:bb:cc:dd:ee:ff",
  1532. data={
  1533. "data": {
  1534. "battery": 100,
  1535. "fahrenheit": True,
  1536. "humidity": 36,
  1537. "temp": {"c": 27.7, "f": 81.86},
  1538. "temperature": 27.7,
  1539. "co2": 725,
  1540. },
  1541. "isEncrypted": False,
  1542. "model": "5",
  1543. "modelFriendlyName": "Meter",
  1544. "modelName": SwitchbotModel.METER_PRO_C,
  1545. "rawAdvData": b"5\x00d",
  1546. },
  1547. device=ble_device,
  1548. rssi=-67,
  1549. active=True,
  1550. )
  1551. def test_meter_pro_c_passive() -> None:
  1552. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1553. adv_data = generate_advertisement_data(
  1554. manufacturer_data={
  1555. 2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
  1556. },
  1557. rssi=-67,
  1558. )
  1559. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER_PRO_C)
  1560. assert result == SwitchBotAdvertisement(
  1561. address="aa:bb:cc:dd:ee:ff",
  1562. data={
  1563. "data": {
  1564. "battery": None,
  1565. "fahrenheit": True,
  1566. "humidity": 36,
  1567. "temp": {"c": 27.7, "f": 81.86},
  1568. "temperature": 27.7,
  1569. "co2": 725,
  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. )