1
0

test_adv_parser.py 56 KB

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