test_adv_parser.py 65 KB

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