1
0

test_adv_parser.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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_woiosensor_passive_and_active():
  902. """Test parsing woiosensor 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={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xe0\x0f\x06\x985\x00"},
  906. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"w\x00\xe4"},
  907. tx_power=-127,
  908. rssi=-50,
  909. )
  910. result = parse_advertisement_data(ble_device, adv_data)
  911. assert result == SwitchBotAdvertisement(
  912. address="aa:bb:cc:dd:ee:ff",
  913. data={
  914. "data": {
  915. "battery": 100,
  916. "fahrenheit": False,
  917. "humidity": 53,
  918. "temp": {"c": 24.6, "f": 76.28},
  919. "temperature": 24.6,
  920. },
  921. "isEncrypted": False,
  922. "model": "w",
  923. "modelFriendlyName": "Indoor/Outdoor Meter",
  924. "modelName": SwitchbotModel.IO_METER,
  925. "rawAdvData": b"w\x00\xe4",
  926. },
  927. device=ble_device,
  928. rssi=-50,
  929. active=True,
  930. )
  931. def test_woiosensor_passive_only():
  932. """Test parsing woiosensor with only passive data."""
  933. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  934. adv_data = generate_advertisement_data(
  935. manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xe0\x0f\x06\x985\x00"},
  936. service_data={},
  937. tx_power=-127,
  938. rssi=-50,
  939. )
  940. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.IO_METER)
  941. assert result == SwitchBotAdvertisement(
  942. address="aa:bb:cc:dd:ee:ff",
  943. data={
  944. "data": {
  945. "battery": None,
  946. "fahrenheit": False,
  947. "humidity": 53,
  948. "temp": {"c": 24.6, "f": 76.28},
  949. "temperature": 24.6,
  950. },
  951. "isEncrypted": False,
  952. "model": "w",
  953. "modelFriendlyName": "Indoor/Outdoor Meter",
  954. "modelName": SwitchbotModel.IO_METER,
  955. "rawAdvData": None,
  956. },
  957. device=ble_device,
  958. rssi=-50,
  959. active=False,
  960. )
  961. def test_motion_sensor_clear():
  962. """Test parsing motion sensor with clear data."""
  963. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  964. adv_data = generate_advertisement_data(
  965. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIj\x1c\x00f"},
  966. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x00f\x01"},
  967. tx_power=-127,
  968. rssi=-87,
  969. )
  970. result = parse_advertisement_data(
  971. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  972. )
  973. assert result == SwitchBotAdvertisement(
  974. address="aa:bb:cc:dd:ee:ff",
  975. data={
  976. "data": {
  977. "battery": 98,
  978. "iot": 0,
  979. "is_light": False,
  980. "led": 0,
  981. "light_intensity": 1,
  982. "motion_detected": False,
  983. "sense_distance": 0,
  984. "tested": False,
  985. },
  986. "isEncrypted": False,
  987. "model": "s",
  988. "modelFriendlyName": "Motion Sensor",
  989. "modelName": SwitchbotModel.MOTION_SENSOR,
  990. "rawAdvData": b"s\x00\xe2\x00f\x01",
  991. },
  992. device=ble_device,
  993. rssi=-87,
  994. active=True,
  995. )
  996. def test_motion_sensor_clear_passive():
  997. """Test parsing motion sensor with clear data."""
  998. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  999. adv_data = generate_advertisement_data(
  1000. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIj\x1c\x00f"},
  1001. service_data={},
  1002. tx_power=-127,
  1003. rssi=-87,
  1004. )
  1005. result = parse_advertisement_data(
  1006. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1007. )
  1008. assert result == SwitchBotAdvertisement(
  1009. address="aa:bb:cc:dd:ee:ff",
  1010. data={
  1011. "data": {
  1012. "battery": None,
  1013. "iot": None,
  1014. "is_light": False,
  1015. "led": None,
  1016. "light_intensity": None,
  1017. "motion_detected": False,
  1018. "sense_distance": None,
  1019. "tested": None,
  1020. },
  1021. "isEncrypted": False,
  1022. "model": "s",
  1023. "modelFriendlyName": "Motion Sensor",
  1024. "modelName": SwitchbotModel.MOTION_SENSOR,
  1025. "rawAdvData": None,
  1026. },
  1027. device=ble_device,
  1028. rssi=-87,
  1029. active=False,
  1030. )
  1031. def test_motion_sensor_motion():
  1032. """Test parsing motion sensor with motion data."""
  1033. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1034. adv_data = generate_advertisement_data(
  1035. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIi\\\x008"},
  1036. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s@\xe2\x008\x01"},
  1037. tx_power=-127,
  1038. rssi=-87,
  1039. )
  1040. result = parse_advertisement_data(
  1041. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1042. )
  1043. assert result == SwitchBotAdvertisement(
  1044. address="aa:bb:cc:dd:ee:ff",
  1045. data={
  1046. "data": {
  1047. "battery": 98,
  1048. "iot": 0,
  1049. "is_light": False,
  1050. "led": 0,
  1051. "light_intensity": 1,
  1052. "motion_detected": True,
  1053. "sense_distance": 0,
  1054. "tested": False,
  1055. },
  1056. "isEncrypted": False,
  1057. "model": "s",
  1058. "modelFriendlyName": "Motion Sensor",
  1059. "modelName": SwitchbotModel.MOTION_SENSOR,
  1060. "rawAdvData": b"s@\xe2\x008\x01",
  1061. },
  1062. device=ble_device,
  1063. rssi=-87,
  1064. active=True,
  1065. )
  1066. def test_motion_sensor_motion_passive():
  1067. """Test parsing motion sensor with motion data."""
  1068. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1069. adv_data = generate_advertisement_data(
  1070. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIi\\\x008"},
  1071. service_data={},
  1072. tx_power=-127,
  1073. rssi=-87,
  1074. )
  1075. result = parse_advertisement_data(
  1076. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1077. )
  1078. assert result == SwitchBotAdvertisement(
  1079. address="aa:bb:cc:dd:ee:ff",
  1080. data={
  1081. "data": {
  1082. "battery": None,
  1083. "iot": None,
  1084. "is_light": False,
  1085. "led": None,
  1086. "light_intensity": None,
  1087. "motion_detected": True,
  1088. "sense_distance": None,
  1089. "tested": None,
  1090. },
  1091. "isEncrypted": False,
  1092. "model": "s",
  1093. "modelFriendlyName": "Motion Sensor",
  1094. "modelName": SwitchbotModel.MOTION_SENSOR,
  1095. "rawAdvData": None,
  1096. },
  1097. device=ble_device,
  1098. rssi=-87,
  1099. active=False,
  1100. )
  1101. def test_motion_sensor_is_light_passive():
  1102. """Test parsing motion sensor with motion data."""
  1103. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1104. adv_data = generate_advertisement_data(
  1105. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIs,\x04g"},
  1106. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x04g\x02"},
  1107. tx_power=-127,
  1108. rssi=-93,
  1109. )
  1110. result = parse_advertisement_data(
  1111. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1112. )
  1113. assert result == SwitchBotAdvertisement(
  1114. address="aa:bb:cc:dd:ee:ff",
  1115. data={
  1116. "data": {
  1117. "battery": 98,
  1118. "iot": 0,
  1119. "is_light": True,
  1120. "led": 0,
  1121. "light_intensity": 2,
  1122. "motion_detected": False,
  1123. "sense_distance": 0,
  1124. "tested": False,
  1125. },
  1126. "isEncrypted": False,
  1127. "model": "s",
  1128. "modelFriendlyName": "Motion Sensor",
  1129. "modelName": SwitchbotModel.MOTION_SENSOR,
  1130. "rawAdvData": b"s\x00\xe2\x04g\x02",
  1131. },
  1132. device=ble_device,
  1133. rssi=-93,
  1134. active=True,
  1135. )
  1136. def test_motion_sensor_is_light_active():
  1137. """Test parsing motion sensor with motion data."""
  1138. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1139. adv_data = generate_advertisement_data(
  1140. manufacturer_data={},
  1141. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x04g\x02"},
  1142. tx_power=-127,
  1143. rssi=-93,
  1144. )
  1145. result = parse_advertisement_data(
  1146. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1147. )
  1148. assert result == SwitchBotAdvertisement(
  1149. address="aa:bb:cc:dd:ee:ff",
  1150. data={
  1151. "data": {
  1152. "battery": 98,
  1153. "iot": 0,
  1154. "is_light": True,
  1155. "led": 0,
  1156. "light_intensity": 2,
  1157. "motion_detected": False,
  1158. "sense_distance": 0,
  1159. "tested": False,
  1160. },
  1161. "isEncrypted": False,
  1162. "model": "s",
  1163. "modelFriendlyName": "Motion Sensor",
  1164. "modelName": SwitchbotModel.MOTION_SENSOR,
  1165. "rawAdvData": b"s\x00\xe2\x04g\x02",
  1166. },
  1167. device=ble_device,
  1168. rssi=-93,
  1169. active=True,
  1170. )
  1171. def test_motion_with_light_detected():
  1172. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1173. adv_data = generate_advertisement_data(
  1174. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIvl\x00,"},
  1175. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s@\xe2\x00,\x02"},
  1176. tx_power=-127,
  1177. rssi=-84,
  1178. )
  1179. result = parse_advertisement_data(
  1180. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1181. )
  1182. assert result == SwitchBotAdvertisement(
  1183. address="aa:bb:cc:dd:ee:ff",
  1184. data={
  1185. "data": {
  1186. "battery": 98,
  1187. "iot": 0,
  1188. "is_light": True,
  1189. "led": 0,
  1190. "light_intensity": 2,
  1191. "motion_detected": True,
  1192. "sense_distance": 0,
  1193. "tested": False,
  1194. },
  1195. "isEncrypted": False,
  1196. "model": "s",
  1197. "modelFriendlyName": "Motion Sensor",
  1198. "modelName": SwitchbotModel.MOTION_SENSOR,
  1199. "rawAdvData": b"s@\xe2\x00,\x02",
  1200. },
  1201. device=ble_device,
  1202. rssi=-84,
  1203. active=True,
  1204. )
  1205. def test_motion_sensor_motion_passive():
  1206. """Test parsing motion sensor with motion data."""
  1207. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1208. adv_data = generate_advertisement_data(
  1209. manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIi\\\x008"},
  1210. service_data={},
  1211. tx_power=-127,
  1212. rssi=-87,
  1213. )
  1214. result = parse_advertisement_data(
  1215. ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
  1216. )
  1217. assert result == SwitchBotAdvertisement(
  1218. address="aa:bb:cc:dd:ee:ff",
  1219. data={
  1220. "data": {
  1221. "battery": None,
  1222. "iot": None,
  1223. "is_light": False,
  1224. "led": None,
  1225. "light_intensity": None,
  1226. "motion_detected": True,
  1227. "sense_distance": None,
  1228. "tested": None,
  1229. },
  1230. "isEncrypted": False,
  1231. "model": "s",
  1232. "modelFriendlyName": "Motion Sensor",
  1233. "modelName": SwitchbotModel.MOTION_SENSOR,
  1234. "rawAdvData": None,
  1235. },
  1236. device=ble_device,
  1237. rssi=-87,
  1238. active=False,
  1239. )
  1240. def test_parsing_lock_active():
  1241. """Test parsing lock with active data."""
  1242. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1243. adv_data = generate_advertisement_data(
  1244. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00 "},
  1245. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"o\x80d"},
  1246. rssi=-67,
  1247. )
  1248. result = parse_advertisement_data(ble_device, adv_data)
  1249. assert result == SwitchBotAdvertisement(
  1250. address="aa:bb:cc:dd:ee:ff",
  1251. data={
  1252. "data": {
  1253. "auto_lock_paused": False,
  1254. "battery": 100,
  1255. "calibration": True,
  1256. "door_open": False,
  1257. "double_lock_mode": False,
  1258. "night_latch": False,
  1259. "status": LockStatus.LOCKED,
  1260. "unclosed_alarm": False,
  1261. "unlocked_alarm": False,
  1262. "update_from_secondary_lock": False,
  1263. },
  1264. "isEncrypted": False,
  1265. "model": "o",
  1266. "modelFriendlyName": "Lock",
  1267. "modelName": SwitchbotModel.LOCK,
  1268. "rawAdvData": b"o\x80d",
  1269. },
  1270. device=ble_device,
  1271. rssi=-67,
  1272. active=True,
  1273. )
  1274. def test_parsing_lock_passive():
  1275. """Test parsing lock with active data."""
  1276. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1277. adv_data = generate_advertisement_data(
  1278. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00 "}, rssi=-67
  1279. )
  1280. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK)
  1281. assert result == SwitchBotAdvertisement(
  1282. address="aa:bb:cc:dd:ee:ff",
  1283. data={
  1284. "data": {
  1285. "auto_lock_paused": False,
  1286. "battery": None,
  1287. "calibration": True,
  1288. "door_open": False,
  1289. "double_lock_mode": False,
  1290. "night_latch": False,
  1291. "status": LockStatus.LOCKED,
  1292. "unclosed_alarm": False,
  1293. "unlocked_alarm": False,
  1294. "update_from_secondary_lock": False,
  1295. },
  1296. "isEncrypted": False,
  1297. "model": "o",
  1298. "modelFriendlyName": "Lock",
  1299. "modelName": SwitchbotModel.LOCK,
  1300. "rawAdvData": None,
  1301. },
  1302. device=ble_device,
  1303. rssi=-67,
  1304. active=False,
  1305. )
  1306. def test_parsing_lock_active_old_firmware():
  1307. """Test parsing lock with active data. Old firmware."""
  1308. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1309. adv_data = generate_advertisement_data(
  1310. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00"},
  1311. service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"o\x80d"},
  1312. rssi=-67,
  1313. )
  1314. result = parse_advertisement_data(ble_device, adv_data)
  1315. assert result == SwitchBotAdvertisement(
  1316. address="aa:bb:cc:dd:ee:ff",
  1317. data={
  1318. "data": {
  1319. "auto_lock_paused": False,
  1320. "battery": 100,
  1321. "calibration": True,
  1322. "door_open": False,
  1323. "double_lock_mode": False,
  1324. "night_latch": False,
  1325. "status": LockStatus.LOCKED,
  1326. "unclosed_alarm": False,
  1327. "unlocked_alarm": False,
  1328. "update_from_secondary_lock": False,
  1329. },
  1330. "isEncrypted": False,
  1331. "model": "o",
  1332. "modelFriendlyName": "Lock",
  1333. "modelName": SwitchbotModel.LOCK,
  1334. "rawAdvData": b"o\x80d",
  1335. },
  1336. device=ble_device,
  1337. rssi=-67,
  1338. active=True,
  1339. )
  1340. def test_parsing_lock_passive_old_firmware():
  1341. """Test parsing lock with active data. Old firmware."""
  1342. ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
  1343. adv_data = generate_advertisement_data(
  1344. manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00"}, rssi=-67
  1345. )
  1346. result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK)
  1347. assert result == SwitchBotAdvertisement(
  1348. address="aa:bb:cc:dd:ee:ff",
  1349. data={
  1350. "data": {
  1351. "auto_lock_paused": False,
  1352. "battery": None,
  1353. "calibration": True,
  1354. "door_open": False,
  1355. "double_lock_mode": False,
  1356. "night_latch": False,
  1357. "status": LockStatus.LOCKED,
  1358. "unclosed_alarm": False,
  1359. "unlocked_alarm": False,
  1360. "update_from_secondary_lock": False,
  1361. },
  1362. "isEncrypted": False,
  1363. "model": "o",
  1364. "modelFriendlyName": "Lock",
  1365. "modelName": SwitchbotModel.LOCK,
  1366. "rawAdvData": None,
  1367. },
  1368. device=ble_device,
  1369. rssi=-67,
  1370. active=False,
  1371. )