adv_parser.py 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. import logging
  4. from collections import defaultdict
  5. from collections.abc import Callable
  6. from functools import lru_cache
  7. from typing import Any, TypedDict
  8. from bleak.backends.device import BLEDevice
  9. from bleak.backends.scanner import AdvertisementData
  10. from .adv_parsers.air_purifier import process_air_purifier
  11. from .adv_parsers.art_frame import process_art_frame
  12. from .adv_parsers.blind_tilt import process_woblindtilt
  13. from .adv_parsers.bot import process_wohand
  14. from .adv_parsers.bulb import process_color_bulb
  15. from .adv_parsers.ceiling_light import process_woceiling
  16. from .adv_parsers.climate_panel import process_climate_panel
  17. from .adv_parsers.contact import process_wocontact
  18. from .adv_parsers.curtain import process_wocurtain
  19. from .adv_parsers.fan import (
  20. process_circulator_fan_pro,
  21. process_fan,
  22. process_standing_fan,
  23. )
  24. from .adv_parsers.hub2 import process_wohub2
  25. from .adv_parsers.hub3 import process_hub3
  26. from .adv_parsers.hub4 import process_hub4
  27. from .adv_parsers.hubmini_matter import process_hubmini_matter
  28. from .adv_parsers.humidifier import process_evaporative_humidifier, process_wohumidifier
  29. from .adv_parsers.keypad import process_wokeypad
  30. from .adv_parsers.keypad_vision import process_keypad_vision, process_keypad_vision_pro
  31. from .adv_parsers.leak import process_leak
  32. from .adv_parsers.light_strip import (
  33. process_candle_warmer_lamp,
  34. process_light,
  35. process_rgbic_light,
  36. process_rgbicww_ceiling_light,
  37. process_wostrip,
  38. )
  39. from .adv_parsers.lock import (
  40. process_lock2,
  41. process_lock_nano,
  42. process_locklite,
  43. process_wolock,
  44. process_wolock_pro,
  45. )
  46. from .adv_parsers.meter import process_wosensorth, process_wosensorth_c
  47. from .adv_parsers.motion import process_wopresence
  48. from .adv_parsers.plug import process_outdoor_plug, process_woplugmini
  49. from .adv_parsers.presence_sensor import process_presence_sensor
  50. from .adv_parsers.relay_switch import (
  51. process_garage_door_opener,
  52. process_relay_switch_1pm,
  53. process_relay_switch_2pm,
  54. process_relay_switch_common_data,
  55. )
  56. from .adv_parsers.remote import process_woremote, process_wouniversal_remote
  57. from .adv_parsers.roller_shade import process_worollershade
  58. from .adv_parsers.smart_thermostat_radiator import process_smart_thermostat_radiator
  59. from .adv_parsers.vacuum import process_vacuum, process_vacuum_k
  60. from .adv_parsers.weather_station import process_weather_station
  61. from .const import SwitchbotModel
  62. from .models import SwitchBotAdvertisement
  63. from .utils import format_mac_upper
  64. _LOGGER = logging.getLogger(__name__)
  65. SERVICE_DATA_ORDER = (
  66. "0000fd3d-0000-1000-8000-00805f9b34fb",
  67. "00000d00-0000-1000-8000-00805f9b34fb",
  68. )
  69. MFR_DATA_ORDER = (2409, 741, 89)
  70. _MODEL_TO_MAC_CACHE: dict[str, SwitchbotModel] = {}
  71. class SwitchbotSupportedType(TypedDict):
  72. """Supported type of Switchbot."""
  73. modelName: SwitchbotModel
  74. modelFriendlyName: str
  75. func: Callable[[bytes | None, bytes | None], dict[str, bool | int | str | None]]
  76. manufacturer_id: int | None
  77. manufacturer_data_length: int | None
  78. SUPPORTED_TYPES: dict[str | bytes, SwitchbotSupportedType] = {
  79. "d": {
  80. "modelName": SwitchbotModel.CONTACT_SENSOR,
  81. "modelFriendlyName": "Contact Sensor",
  82. "func": process_wocontact,
  83. "manufacturer_id": 2409,
  84. },
  85. "D": {
  86. "modelName": SwitchbotModel.CONTACT_SENSOR,
  87. "modelFriendlyName": "Contact Sensor",
  88. "func": process_wocontact,
  89. "manufacturer_id": 2409,
  90. },
  91. "H": {
  92. "modelName": SwitchbotModel.BOT,
  93. "modelFriendlyName": "Bot",
  94. "func": process_wohand,
  95. "manufacturer_id": 89,
  96. },
  97. "s": {
  98. "modelName": SwitchbotModel.MOTION_SENSOR,
  99. "modelFriendlyName": "Motion Sensor",
  100. "func": process_wopresence,
  101. "manufacturer_id": 2409,
  102. },
  103. "S": {
  104. "modelName": SwitchbotModel.MOTION_SENSOR,
  105. "modelFriendlyName": "Motion Sensor",
  106. "func": process_wopresence,
  107. "manufacturer_id": 2409,
  108. },
  109. "r": {
  110. "modelName": SwitchbotModel.LIGHT_STRIP,
  111. "modelFriendlyName": "Light Strip",
  112. "func": process_wostrip,
  113. "manufacturer_id": 2409,
  114. },
  115. "R": {
  116. "modelName": SwitchbotModel.LIGHT_STRIP,
  117. "modelFriendlyName": "Light Strip",
  118. "func": process_wostrip,
  119. "manufacturer_id": 2409,
  120. },
  121. "{": {
  122. "modelName": SwitchbotModel.CURTAIN_3,
  123. "modelFriendlyName": "Curtain 3",
  124. "func": process_wocurtain,
  125. "manufacturer_id": 2409,
  126. },
  127. "[": {
  128. "modelName": SwitchbotModel.CURTAIN_3,
  129. "modelFriendlyName": "Curtain 3",
  130. "func": process_wocurtain,
  131. "manufacturer_id": 2409,
  132. },
  133. "c": {
  134. "modelName": SwitchbotModel.CURTAIN,
  135. "modelFriendlyName": "Curtain",
  136. "func": process_wocurtain,
  137. "manufacturer_id": 2409,
  138. },
  139. "C": {
  140. "modelName": SwitchbotModel.CURTAIN,
  141. "modelFriendlyName": "Curtain",
  142. "func": process_wocurtain,
  143. "manufacturer_id": 2409,
  144. },
  145. "w": {
  146. "modelName": SwitchbotModel.INDOOR_OUTDOOR_THERMO_HYGROMETER,
  147. "modelFriendlyName": "Indoor/Outdoor Thermo-Hygrometer",
  148. "func": process_wosensorth,
  149. "manufacturer_id": 2409,
  150. },
  151. "W": {
  152. "modelName": SwitchbotModel.INDOOR_OUTDOOR_THERMO_HYGROMETER,
  153. "modelFriendlyName": "Indoor/Outdoor Thermo-Hygrometer",
  154. "func": process_wosensorth,
  155. "manufacturer_id": 2409,
  156. },
  157. "i": {
  158. "modelName": SwitchbotModel.METER_PLUS,
  159. "modelFriendlyName": "Meter Plus",
  160. "func": process_wosensorth,
  161. "manufacturer_id": 2409,
  162. },
  163. "I": {
  164. "modelName": SwitchbotModel.METER_PLUS,
  165. "modelFriendlyName": "Meter Plus",
  166. "func": process_wosensorth,
  167. "manufacturer_id": 2409,
  168. },
  169. "T": {
  170. "modelName": SwitchbotModel.METER,
  171. "modelFriendlyName": "Meter",
  172. "func": process_wosensorth,
  173. "manufacturer_id": 2409,
  174. },
  175. "t": {
  176. "modelName": SwitchbotModel.METER,
  177. "modelFriendlyName": "Meter",
  178. "func": process_wosensorth,
  179. "manufacturer_id": 2409,
  180. },
  181. "4": {
  182. "modelName": SwitchbotModel.METER_PRO,
  183. "modelFriendlyName": "Meter Pro",
  184. "func": process_wosensorth,
  185. "manufacturer_id": 2409,
  186. },
  187. b"\x14": {
  188. "modelName": SwitchbotModel.METER_PRO,
  189. "modelFriendlyName": "Meter Pro",
  190. "func": process_wosensorth,
  191. "manufacturer_id": 2409,
  192. },
  193. "5": {
  194. "modelName": SwitchbotModel.METER_PRO_CO2,
  195. "modelFriendlyName": "Meter Pro CO2",
  196. "func": process_wosensorth_c,
  197. "manufacturer_id": 2409,
  198. },
  199. b"\x15": {
  200. "modelName": SwitchbotModel.METER_PRO_CO2,
  201. "modelFriendlyName": "Meter Pro CO2",
  202. "func": process_wosensorth_c,
  203. "manufacturer_id": 2409,
  204. },
  205. "v": {
  206. "modelName": SwitchbotModel.HUB2,
  207. "modelFriendlyName": "Hub 2",
  208. "func": process_wohub2,
  209. "manufacturer_id": 2409,
  210. },
  211. "V": {
  212. "modelName": SwitchbotModel.HUB2,
  213. "modelFriendlyName": "Hub 2",
  214. "func": process_wohub2,
  215. "manufacturer_id": 2409,
  216. },
  217. "g": {
  218. "modelName": SwitchbotModel.PLUG_MINI_US,
  219. "modelFriendlyName": "Plug Mini (US)",
  220. "func": process_woplugmini,
  221. "manufacturer_id": 2409,
  222. },
  223. "G": {
  224. "modelName": SwitchbotModel.PLUG_MINI_US,
  225. "modelFriendlyName": "Plug Mini (US)",
  226. "func": process_woplugmini,
  227. "manufacturer_id": 2409,
  228. },
  229. "j": {
  230. "modelName": SwitchbotModel.PLUG_MINI_JP,
  231. "modelFriendlyName": "Plug Mini (JP)",
  232. "func": process_woplugmini,
  233. "manufacturer_id": 2409,
  234. },
  235. "J": {
  236. "modelName": SwitchbotModel.PLUG_MINI_JP,
  237. "modelFriendlyName": "Plug Mini (JP)",
  238. "func": process_woplugmini,
  239. "manufacturer_id": 2409,
  240. },
  241. "u": {
  242. "modelName": SwitchbotModel.COLOR_BULB,
  243. "modelFriendlyName": "Color Bulb",
  244. "func": process_color_bulb,
  245. "manufacturer_id": 2409,
  246. },
  247. "U": {
  248. "modelName": SwitchbotModel.COLOR_BULB,
  249. "modelFriendlyName": "Color Bulb",
  250. "func": process_color_bulb,
  251. "manufacturer_id": 2409,
  252. },
  253. "q": {
  254. "modelName": SwitchbotModel.CEILING_LIGHT,
  255. "modelFriendlyName": "Ceiling Light",
  256. "func": process_woceiling,
  257. "manufacturer_id": 2409,
  258. },
  259. "Q": {
  260. "modelName": SwitchbotModel.CEILING_LIGHT,
  261. "modelFriendlyName": "Ceiling Light",
  262. "func": process_woceiling,
  263. "manufacturer_id": 2409,
  264. },
  265. "n": {
  266. "modelName": SwitchbotModel.CEILING_LIGHT_PRO,
  267. "modelFriendlyName": "Ceiling Light Pro",
  268. "func": process_woceiling,
  269. "manufacturer_id": 2409,
  270. },
  271. "N": {
  272. "modelName": SwitchbotModel.CEILING_LIGHT_PRO,
  273. "modelFriendlyName": "Ceiling Light Pro",
  274. "func": process_woceiling,
  275. "manufacturer_id": 2409,
  276. },
  277. "e": {
  278. "modelName": SwitchbotModel.HUMIDIFIER,
  279. "modelFriendlyName": "Humidifier",
  280. "func": process_wohumidifier,
  281. "manufacturer_id": 741,
  282. "manufacturer_data_length": 6,
  283. },
  284. "E": {
  285. "modelName": SwitchbotModel.HUMIDIFIER,
  286. "modelFriendlyName": "Humidifier",
  287. "func": process_wohumidifier,
  288. "manufacturer_id": 741,
  289. "manufacturer_data_length": 6,
  290. },
  291. "#": {
  292. "modelName": SwitchbotModel.EVAPORATIVE_HUMIDIFIER,
  293. "modelFriendlyName": "Evaporative Humidifier",
  294. "func": process_evaporative_humidifier,
  295. "manufacturer_id": 2409,
  296. },
  297. b"\x03": {
  298. "modelName": SwitchbotModel.EVAPORATIVE_HUMIDIFIER,
  299. "modelFriendlyName": "Evaporative Humidifier",
  300. "func": process_evaporative_humidifier,
  301. "manufacturer_id": 2409,
  302. },
  303. "o": {
  304. "modelName": SwitchbotModel.LOCK,
  305. "modelFriendlyName": "Lock",
  306. "func": process_wolock,
  307. "manufacturer_id": 2409,
  308. },
  309. "O": {
  310. "modelName": SwitchbotModel.LOCK,
  311. "modelFriendlyName": "Lock",
  312. "func": process_wolock,
  313. "manufacturer_id": 2409,
  314. },
  315. "$": {
  316. "modelName": SwitchbotModel.LOCK_PRO,
  317. "modelFriendlyName": "Lock Pro",
  318. "func": process_wolock_pro,
  319. "manufacturer_id": 2409,
  320. },
  321. b"\x04": {
  322. "modelName": SwitchbotModel.LOCK_PRO,
  323. "modelFriendlyName": "Lock Pro",
  324. "func": process_wolock_pro,
  325. "manufacturer_id": 2409,
  326. },
  327. "x": {
  328. "modelName": SwitchbotModel.BLIND_TILT,
  329. "modelFriendlyName": "Blind Tilt",
  330. "func": process_woblindtilt,
  331. "manufacturer_id": 2409,
  332. },
  333. "X": {
  334. "modelName": SwitchbotModel.BLIND_TILT,
  335. "modelFriendlyName": "Blind Tilt",
  336. "func": process_woblindtilt,
  337. "manufacturer_id": 2409,
  338. },
  339. "&": {
  340. "modelName": SwitchbotModel.LEAK,
  341. "modelFriendlyName": "Leak Detector",
  342. "func": process_leak,
  343. "manufacturer_id": 2409,
  344. },
  345. b"\x06": {
  346. "modelName": SwitchbotModel.LEAK,
  347. "modelFriendlyName": "Leak Detector",
  348. "func": process_leak,
  349. "manufacturer_id": 2409,
  350. },
  351. "y": {
  352. "modelName": SwitchbotModel.KEYPAD,
  353. "modelFriendlyName": "Keypad",
  354. "func": process_wokeypad,
  355. "manufacturer_id": 2409,
  356. },
  357. "Y": {
  358. "modelName": SwitchbotModel.KEYPAD,
  359. "modelFriendlyName": "Keypad",
  360. "func": process_wokeypad,
  361. "manufacturer_id": 2409,
  362. },
  363. "<": {
  364. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  365. "modelFriendlyName": "Relay Switch 1PM",
  366. "func": process_relay_switch_1pm,
  367. "manufacturer_id": 2409,
  368. },
  369. b"\x1c": {
  370. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  371. "modelFriendlyName": "Relay Switch 1PM",
  372. "func": process_relay_switch_1pm,
  373. "manufacturer_id": 2409,
  374. },
  375. ";": {
  376. "modelName": SwitchbotModel.RELAY_SWITCH_1,
  377. "modelFriendlyName": "Relay Switch 1",
  378. "func": process_relay_switch_common_data,
  379. "manufacturer_id": 2409,
  380. },
  381. b"\x1b": {
  382. "modelName": SwitchbotModel.RELAY_SWITCH_1,
  383. "modelFriendlyName": "Relay Switch 1",
  384. "func": process_relay_switch_common_data,
  385. "manufacturer_id": 2409,
  386. },
  387. "b": {
  388. "modelName": SwitchbotModel.REMOTE,
  389. "modelFriendlyName": "Remote",
  390. "func": process_woremote,
  391. "manufacturer_id": 89,
  392. },
  393. "B": {
  394. "modelName": SwitchbotModel.REMOTE,
  395. "modelFriendlyName": "Remote",
  396. "func": process_woremote,
  397. "manufacturer_id": 89,
  398. },
  399. "\x07": {
  400. "modelName": SwitchbotModel.UNIVERSAL_REMOTE,
  401. "modelFriendlyName": "Universal Remote",
  402. "func": process_wouniversal_remote,
  403. "manufacturer_id": 2409,
  404. },
  405. "'": {
  406. "modelName": SwitchbotModel.UNIVERSAL_REMOTE,
  407. "modelFriendlyName": "Universal Remote",
  408. "func": process_wouniversal_remote,
  409. "manufacturer_id": 2409,
  410. },
  411. ",": {
  412. "modelName": SwitchbotModel.ROLLER_SHADE,
  413. "modelFriendlyName": "Roller Shade",
  414. "func": process_worollershade,
  415. "manufacturer_id": 2409,
  416. },
  417. b"\x0c": {
  418. "modelName": SwitchbotModel.ROLLER_SHADE,
  419. "modelFriendlyName": "Roller Shade",
  420. "func": process_worollershade,
  421. "manufacturer_id": 2409,
  422. },
  423. "%": {
  424. "modelName": SwitchbotModel.HUBMINI_MATTER,
  425. "modelFriendlyName": "HubMini Matter",
  426. "func": process_hubmini_matter,
  427. "manufacturer_id": 2409,
  428. },
  429. b"\x05": {
  430. "modelName": SwitchbotModel.HUBMINI_MATTER,
  431. "modelFriendlyName": "HubMini Matter",
  432. "func": process_hubmini_matter,
  433. "manufacturer_id": 2409,
  434. },
  435. "~": {
  436. "modelName": SwitchbotModel.CIRCULATOR_FAN,
  437. "modelFriendlyName": "Circulator Fan",
  438. "func": process_fan,
  439. "manufacturer_id": 2409,
  440. },
  441. "^": {
  442. "modelName": SwitchbotModel.CIRCULATOR_FAN,
  443. "modelFriendlyName": "Circulator Fan",
  444. "func": process_fan,
  445. "manufacturer_id": 2409,
  446. },
  447. ".": {
  448. "modelName": SwitchbotModel.K20_VACUUM,
  449. "modelFriendlyName": "K20 Vacuum",
  450. "func": process_vacuum,
  451. "manufacturer_id": 2409,
  452. },
  453. b"\x0f": {
  454. "modelName": SwitchbotModel.K20_VACUUM,
  455. "modelFriendlyName": "K20 Vacuum",
  456. "func": process_vacuum,
  457. "manufacturer_id": 2409,
  458. },
  459. "z": {
  460. "modelName": SwitchbotModel.S10_VACUUM,
  461. "modelFriendlyName": "S10 Vacuum",
  462. "func": process_vacuum,
  463. "manufacturer_id": 2409,
  464. },
  465. "Z": {
  466. "modelName": SwitchbotModel.S10_VACUUM,
  467. "modelFriendlyName": "S10 Vacuum",
  468. "func": process_vacuum,
  469. "manufacturer_id": 2409,
  470. },
  471. "3": {
  472. "modelName": SwitchbotModel.K10_PRO_COMBO_VACUUM,
  473. "modelFriendlyName": "K10+ Pro Combo Vacuum",
  474. "func": process_vacuum,
  475. "manufacturer_id": 2409,
  476. },
  477. b"\x13": {
  478. "modelName": SwitchbotModel.K10_PRO_COMBO_VACUUM,
  479. "modelFriendlyName": "K10+ Pro Combo Vacuum",
  480. "func": process_vacuum,
  481. "manufacturer_id": 2409,
  482. },
  483. "}": {
  484. "modelName": SwitchbotModel.K10_VACUUM,
  485. "modelFriendlyName": "K10+ Vacuum",
  486. "func": process_vacuum_k,
  487. "manufacturer_id": 2409,
  488. },
  489. "]": {
  490. "modelName": SwitchbotModel.K10_VACUUM,
  491. "modelFriendlyName": "K10+ Vacuum",
  492. "func": process_vacuum_k,
  493. "manufacturer_id": 2409,
  494. },
  495. "(": {
  496. "modelName": SwitchbotModel.K10_PRO_VACUUM,
  497. "modelFriendlyName": "K10+ Pro Vacuum",
  498. "func": process_vacuum_k,
  499. "manufacturer_id": 2409,
  500. },
  501. b"\x08": {
  502. "modelName": SwitchbotModel.K10_PRO_VACUUM,
  503. "modelFriendlyName": "K10+ Pro Vacuum",
  504. "func": process_vacuum_k,
  505. "manufacturer_id": 2409,
  506. },
  507. "*": {
  508. "modelName": SwitchbotModel.AIR_PURIFIER_US,
  509. "modelFriendlyName": "Air Purifier US",
  510. "func": process_air_purifier,
  511. "manufacturer_id": 2409,
  512. },
  513. b"\x0a": {
  514. "modelName": SwitchbotModel.AIR_PURIFIER_US,
  515. "modelFriendlyName": "Air Purifier US",
  516. "func": process_air_purifier,
  517. "manufacturer_id": 2409,
  518. },
  519. "+": {
  520. "modelName": SwitchbotModel.AIR_PURIFIER_JP,
  521. "modelFriendlyName": "Air Purifier JP",
  522. "func": process_air_purifier,
  523. "manufacturer_id": 2409,
  524. },
  525. b"\x0b": {
  526. "modelName": SwitchbotModel.AIR_PURIFIER_JP,
  527. "modelFriendlyName": "Air Purifier JP",
  528. "func": process_air_purifier,
  529. "manufacturer_id": 2409,
  530. },
  531. "7": {
  532. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE_US,
  533. "modelFriendlyName": "Air Purifier Table US",
  534. "func": process_air_purifier,
  535. "manufacturer_id": 2409,
  536. },
  537. b"\x17": {
  538. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE_US,
  539. "modelFriendlyName": "Air Purifier Table US",
  540. "func": process_air_purifier,
  541. "manufacturer_id": 2409,
  542. },
  543. "8": {
  544. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE_JP,
  545. "modelFriendlyName": "Air Purifier Table JP",
  546. "func": process_air_purifier,
  547. "manufacturer_id": 2409,
  548. },
  549. b"\x18": {
  550. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE_JP,
  551. "modelFriendlyName": "Air Purifier Table JP",
  552. "func": process_air_purifier,
  553. "manufacturer_id": 2409,
  554. },
  555. b"\x00\x10\xb9\x40": {
  556. "modelName": SwitchbotModel.HUB3,
  557. "modelFriendlyName": "Hub3",
  558. "func": process_hub3,
  559. "manufacturer_id": 2409,
  560. },
  561. b"\x01\x10\xb9\x40": {
  562. "modelName": SwitchbotModel.HUB3,
  563. "modelFriendlyName": "Hub3",
  564. "func": process_hub3,
  565. "manufacturer_id": 2409,
  566. },
  567. b"\x00\x12\x82\x48": {
  568. "modelName": SwitchbotModel.HUB4,
  569. "modelFriendlyName": "Hub4",
  570. "func": process_hub4,
  571. "manufacturer_id": 2409,
  572. },
  573. b"\x01\x12\x82\x48": {
  574. "modelName": SwitchbotModel.HUB4,
  575. "modelFriendlyName": "Hub4",
  576. "func": process_hub4,
  577. "manufacturer_id": 2409,
  578. },
  579. "-": {
  580. "modelName": SwitchbotModel.LOCK_LITE,
  581. "modelFriendlyName": "Lock Lite",
  582. "func": process_locklite,
  583. "manufacturer_id": 2409,
  584. },
  585. b"\x0d": {
  586. "modelName": SwitchbotModel.LOCK_LITE,
  587. "modelFriendlyName": "Lock Lite",
  588. "func": process_locklite,
  589. "manufacturer_id": 2409,
  590. },
  591. b"\x00\x10\xa5\xb8": {
  592. "modelName": SwitchbotModel.LOCK_ULTRA,
  593. "modelFriendlyName": "Lock Ultra",
  594. "func": process_lock2,
  595. "manufacturer_id": 2409,
  596. },
  597. b"\x01\x10\xa5\xb8": {
  598. "modelName": SwitchbotModel.LOCK_ULTRA,
  599. "modelFriendlyName": "Lock Ultra",
  600. "func": process_lock2,
  601. "manufacturer_id": 2409,
  602. },
  603. ">": {
  604. "modelName": SwitchbotModel.GARAGE_DOOR_OPENER,
  605. "modelFriendlyName": "Garage Door Opener",
  606. "func": process_garage_door_opener,
  607. "manufacturer_id": 2409,
  608. },
  609. b"\x1e": {
  610. "modelName": SwitchbotModel.GARAGE_DOOR_OPENER,
  611. "modelFriendlyName": "Garage Door Opener",
  612. "func": process_garage_door_opener,
  613. "manufacturer_id": 2409,
  614. },
  615. "=": {
  616. "modelName": SwitchbotModel.RELAY_SWITCH_2PM,
  617. "modelFriendlyName": "Relay Switch 2PM",
  618. "func": process_relay_switch_2pm,
  619. "manufacturer_id": 2409,
  620. },
  621. b"\x1d": {
  622. "modelName": SwitchbotModel.RELAY_SWITCH_2PM,
  623. "modelFriendlyName": "Relay Switch 2PM",
  624. "func": process_relay_switch_2pm,
  625. "manufacturer_id": 2409,
  626. },
  627. b"\x00\x10\xd0\xb0": {
  628. "modelName": SwitchbotModel.FLOOR_LAMP,
  629. "modelFriendlyName": "Floor Lamp",
  630. "func": process_light,
  631. "manufacturer_id": 2409,
  632. },
  633. b"\x01\x10\xd0\xb0": {
  634. "modelName": SwitchbotModel.FLOOR_LAMP,
  635. "modelFriendlyName": "Floor Lamp",
  636. "func": process_light,
  637. "manufacturer_id": 2409,
  638. },
  639. b"\x00\x11\x22\xb8": {
  640. "modelName": SwitchbotModel.CANDLE_WARMER_LAMP,
  641. "modelFriendlyName": "Candle Warmer Lamp",
  642. "func": process_candle_warmer_lamp,
  643. "manufacturer_id": 2409,
  644. },
  645. b"\x01\x11\x22\xb8": {
  646. "modelName": SwitchbotModel.CANDLE_WARMER_LAMP,
  647. "modelFriendlyName": "Candle Warmer Lamp",
  648. "func": process_candle_warmer_lamp,
  649. "manufacturer_id": 2409,
  650. },
  651. b"\x00\x10\xd0\xb1": {
  652. "modelName": SwitchbotModel.STRIP_LIGHT_3,
  653. "modelFriendlyName": "Strip Light 3",
  654. "func": process_light,
  655. "manufacturer_id": 2409,
  656. },
  657. b"\x01\x10\xd0\xb1": {
  658. "modelName": SwitchbotModel.STRIP_LIGHT_3,
  659. "modelFriendlyName": "Strip Light 3",
  660. "func": process_light,
  661. "manufacturer_id": 2409,
  662. },
  663. "?": {
  664. "modelName": SwitchbotModel.PLUG_MINI_EU,
  665. "modelFriendlyName": "Plug Mini (EU)",
  666. "func": process_relay_switch_1pm,
  667. "manufacturer_id": 2409,
  668. },
  669. b"\x1f": {
  670. "modelName": SwitchbotModel.PLUG_MINI_EU,
  671. "modelFriendlyName": "Plug Mini (EU)",
  672. "func": process_relay_switch_1pm,
  673. "manufacturer_id": 2409,
  674. },
  675. b"\x00\x10\xd0\xb3": {
  676. "modelName": SwitchbotModel.RGBICWW_STRIP_LIGHT,
  677. "modelFriendlyName": "RGBICWW Strip Light",
  678. "func": process_rgbic_light,
  679. "manufacturer_id": 2409,
  680. },
  681. b"\x01\x10\xd0\xb3": {
  682. "modelName": SwitchbotModel.RGBICWW_STRIP_LIGHT,
  683. "modelFriendlyName": "RGBICWW Strip Light",
  684. "func": process_rgbic_light,
  685. "manufacturer_id": 2409,
  686. },
  687. b"\x00\x10\xd0\xb4": {
  688. "modelName": SwitchbotModel.RGBICWW_FLOOR_LAMP,
  689. "modelFriendlyName": "RGBICWW Floor Lamp",
  690. "func": process_rgbic_light,
  691. "manufacturer_id": 2409,
  692. },
  693. b"\x01\x10\xd0\xb4": {
  694. "modelName": SwitchbotModel.RGBICWW_FLOOR_LAMP,
  695. "modelFriendlyName": "RGBICWW Floor Lamp",
  696. "func": process_rgbic_light,
  697. "manufacturer_id": 2409,
  698. },
  699. b"\x00\x11\xbe\xf8": {
  700. "modelName": SwitchbotModel.RGBICWW_LIGHT_BARS,
  701. "modelFriendlyName": "RGBICWW Light Bars",
  702. "func": process_rgbic_light,
  703. "manufacturer_id": 2409,
  704. },
  705. b"\x01\x11\xbe\xf8": {
  706. "modelName": SwitchbotModel.RGBICWW_LIGHT_BARS,
  707. "modelFriendlyName": "RGBICWW Light Bars",
  708. "func": process_rgbic_light,
  709. "manufacturer_id": 2409,
  710. },
  711. b"\x00\x11\xbb\x10": {
  712. "modelName": SwitchbotModel.RGBICWW_CEILING_LIGHT,
  713. "modelFriendlyName": "RGBICWW Ceiling Light",
  714. "func": process_rgbicww_ceiling_light,
  715. "manufacturer_id": 2409,
  716. },
  717. b"\x01\x11\xbb\x10": {
  718. "modelName": SwitchbotModel.RGBICWW_CEILING_LIGHT,
  719. "modelFriendlyName": "RGBICWW Ceiling Light",
  720. "func": process_rgbicww_ceiling_light,
  721. "manufacturer_id": 2409,
  722. },
  723. b"\x00\x11\xb3@": {
  724. "modelName": SwitchbotModel.CIRCULATOR_FAN_PRO,
  725. "modelFriendlyName": "Circulator Fan Pro",
  726. "func": process_circulator_fan_pro,
  727. "manufacturer_id": 2409,
  728. },
  729. b"\x01\x11\xb3@": {
  730. "modelName": SwitchbotModel.CIRCULATOR_FAN_PRO,
  731. "modelFriendlyName": "Circulator Fan Pro",
  732. "func": process_circulator_fan_pro,
  733. "manufacturer_id": 2409,
  734. },
  735. b"\x00\x10\xd0\xb7": {
  736. "modelName": SwitchbotModel.PERMANENT_OUTDOOR_LIGHT,
  737. "modelFriendlyName": "Permanent Outdoor Light",
  738. "func": process_rgbic_light,
  739. "manufacturer_id": 2409,
  740. },
  741. b"\x01\x10\xd0\xb7": {
  742. "modelName": SwitchbotModel.PERMANENT_OUTDOOR_LIGHT,
  743. "modelFriendlyName": "Permanent Outdoor Light",
  744. "func": process_rgbic_light,
  745. "manufacturer_id": 2409,
  746. },
  747. b"\x00\x10\xd0\xb5": {
  748. "modelName": SwitchbotModel.RGBIC_NEON_WIRE_ROPE_LIGHT,
  749. "modelFriendlyName": "RGBIC Neon Wire Rope Light",
  750. "func": process_wostrip,
  751. "manufacturer_id": 2409,
  752. },
  753. b"\x00\x10\xd0\xb6": {
  754. "modelName": SwitchbotModel.RGBIC_NEON_ROPE_LIGHT,
  755. "modelFriendlyName": "RGBIC Neon Rope Light",
  756. "func": process_wostrip,
  757. "manufacturer_id": 2409,
  758. },
  759. b"\x01\x10\xd0\xb5": {
  760. "modelName": SwitchbotModel.RGBIC_NEON_WIRE_ROPE_LIGHT,
  761. "modelFriendlyName": "RGBIC Neon Wire Rope Light",
  762. "func": process_wostrip,
  763. "manufacturer_id": 2409,
  764. },
  765. b"\x01\x10\xd0\xb6": {
  766. "modelName": SwitchbotModel.RGBIC_NEON_ROPE_LIGHT,
  767. "modelFriendlyName": "RGBIC Neon Rope Light",
  768. "func": process_wostrip,
  769. "manufacturer_id": 2409,
  770. },
  771. b"\x00\x10\xfb\xa8": {
  772. "modelName": SwitchbotModel.K11_VACUUM,
  773. "modelFriendlyName": "K11+ Vacuum",
  774. "func": process_vacuum,
  775. "manufacturer_id": 2409,
  776. },
  777. b"\x01\x10\xfb\xa8": {
  778. "modelName": SwitchbotModel.K11_VACUUM,
  779. "modelFriendlyName": "K11+ Vacuum",
  780. "func": process_vacuum,
  781. "manufacturer_id": 2409,
  782. },
  783. b"\x00\x10\xf3\xd8": {
  784. "modelName": SwitchbotModel.CLIMATE_PANEL,
  785. "modelFriendlyName": "Climate Panel",
  786. "func": process_climate_panel,
  787. "manufacturer_id": 2409,
  788. },
  789. b"\x01\x10\xf3\xd8": {
  790. "modelName": SwitchbotModel.CLIMATE_PANEL,
  791. "modelFriendlyName": "Climate Panel",
  792. "func": process_climate_panel,
  793. "manufacturer_id": 2409,
  794. },
  795. b"\x00\x116@": {
  796. "modelName": SwitchbotModel.SMART_THERMOSTAT_RADIATOR,
  797. "modelFriendlyName": "Smart Thermostat Radiator",
  798. "func": process_smart_thermostat_radiator,
  799. "manufacturer_id": 2409,
  800. },
  801. b"\x01\x116@": {
  802. "modelName": SwitchbotModel.SMART_THERMOSTAT_RADIATOR,
  803. "modelFriendlyName": "Smart Thermostat Radiator",
  804. "func": process_smart_thermostat_radiator,
  805. "manufacturer_id": 2409,
  806. },
  807. b"\x00\x10\xe0P": {
  808. "modelName": SwitchbotModel.S20_VACUUM,
  809. "modelFriendlyName": "S20 Vacuum",
  810. "func": process_vacuum,
  811. "manufacturer_id": 2409,
  812. },
  813. b"\x01\x10\xe0P": {
  814. "modelName": SwitchbotModel.S20_VACUUM,
  815. "modelFriendlyName": "S20 Vacuum",
  816. "func": process_vacuum,
  817. "manufacturer_id": 2409,
  818. },
  819. b"\x00\x10\xcc\xc8": {
  820. "modelName": SwitchbotModel.PRESENCE_SENSOR,
  821. "modelFriendlyName": "Presence Sensor",
  822. "func": process_presence_sensor,
  823. "manufacturer_id": 2409,
  824. },
  825. b"\x01\x10\xcc\xc8": {
  826. "modelName": SwitchbotModel.PRESENCE_SENSOR,
  827. "modelFriendlyName": "Presence Sensor",
  828. "func": process_presence_sensor,
  829. "manufacturer_id": 2409,
  830. },
  831. b"\x00\x11>\x10": {
  832. "modelName": SwitchbotModel.ART_FRAME,
  833. "modelFriendlyName": "Art Frame",
  834. "func": process_art_frame,
  835. "manufacturer_id": 2409,
  836. },
  837. b"\x01\x11>\x10": {
  838. "modelName": SwitchbotModel.ART_FRAME,
  839. "modelFriendlyName": "Art Frame",
  840. "func": process_art_frame,
  841. "manufacturer_id": 2409,
  842. },
  843. b"\x00\x11\x03x": {
  844. "modelName": SwitchbotModel.KEYPAD_VISION,
  845. "modelFriendlyName": "Keypad Vision",
  846. "func": process_keypad_vision,
  847. "manufacturer_id": 2409,
  848. },
  849. b"\x01\x11\x03x": {
  850. "modelName": SwitchbotModel.KEYPAD_VISION,
  851. "modelFriendlyName": "Keypad Vision",
  852. "func": process_keypad_vision,
  853. "manufacturer_id": 2409,
  854. },
  855. b"\x00\x11Q\x98": {
  856. "modelName": SwitchbotModel.KEYPAD_VISION_PRO,
  857. "modelFriendlyName": "Keypad Vision Pro",
  858. "func": process_keypad_vision_pro,
  859. "manufacturer_id": 2409,
  860. },
  861. b"\x01\x11Q\x98": {
  862. "modelName": SwitchbotModel.KEYPAD_VISION_PRO,
  863. "modelFriendlyName": "Keypad Vision Pro",
  864. "func": process_keypad_vision_pro,
  865. "manufacturer_id": 2409,
  866. },
  867. b"\x00\x11\x69\x09": {
  868. "modelName": SwitchbotModel.LOCK_VISION_PRO,
  869. "modelFriendlyName": "Lock Vision Pro",
  870. "func": process_lock2,
  871. "manufacturer_id": 2409,
  872. },
  873. b"\x01\x11\x69\x09": {
  874. "modelName": SwitchbotModel.LOCK_VISION_PRO,
  875. "modelFriendlyName": "Lock Vision Pro",
  876. "func": process_lock2,
  877. "manufacturer_id": 2409,
  878. },
  879. b"\x00\x11\x69\x08": {
  880. "modelName": SwitchbotModel.LOCK_VISION,
  881. "modelFriendlyName": "Lock Vision",
  882. "func": process_locklite,
  883. "manufacturer_id": 2409,
  884. },
  885. b"\x01\x11\x69\x08": {
  886. "modelName": SwitchbotModel.LOCK_VISION,
  887. "modelFriendlyName": "Lock Vision",
  888. "func": process_locklite,
  889. "manufacturer_id": 2409,
  890. },
  891. b"\x00\x10\xff\x90": {
  892. "modelName": SwitchbotModel.LOCK_PRO_WIFI,
  893. "modelFriendlyName": "Lock Pro Wifi",
  894. "func": process_wolock_pro,
  895. "manufacturer_id": 2409,
  896. },
  897. b"\x01\x10\xff\x90": {
  898. "modelName": SwitchbotModel.LOCK_PRO_WIFI,
  899. "modelFriendlyName": "Lock Pro Wifi",
  900. "func": process_wolock_pro,
  901. "manufacturer_id": 2409,
  902. },
  903. b"\x00\x12\x11\x00": {
  904. "modelName": SwitchbotModel.UWB_LOCK,
  905. "modelFriendlyName": "UWB Lock",
  906. "func": process_lock2,
  907. "manufacturer_id": 2409,
  908. },
  909. b"\x01\x12\x11\x00": {
  910. "modelName": SwitchbotModel.UWB_LOCK,
  911. "modelFriendlyName": "UWB Lock",
  912. "func": process_lock2,
  913. "manufacturer_id": 2409,
  914. },
  915. b"\x00\x12\x24\x88": {
  916. "modelName": SwitchbotModel.LOCK_NANO,
  917. "modelFriendlyName": "Lock Nano",
  918. "func": process_lock_nano,
  919. "manufacturer_id": 2409,
  920. },
  921. b"\x01\x12\x24\x88": {
  922. "modelName": SwitchbotModel.LOCK_NANO,
  923. "modelFriendlyName": "Lock Nano",
  924. "func": process_lock_nano,
  925. "manufacturer_id": 2409,
  926. },
  927. b"\x00\x11\x9f\xb8": {
  928. "modelName": SwitchbotModel.LOCK_ULTRA_MAX,
  929. "modelFriendlyName": "Lock Ultra Max",
  930. "func": process_lock2,
  931. "manufacturer_id": 2409,
  932. },
  933. b"\x01\x11\x9f\xb8": {
  934. "modelName": SwitchbotModel.LOCK_ULTRA_MAX,
  935. "modelFriendlyName": "Lock Ultra Max",
  936. "func": process_lock2,
  937. "manufacturer_id": 2409,
  938. },
  939. b"\x00\x12\x43\xc8": {
  940. "modelName": SwitchbotModel.PLUG_MINI_IT,
  941. "modelFriendlyName": "Plug Mini (IT)",
  942. "func": process_relay_switch_1pm,
  943. "manufacturer_id": 2409,
  944. },
  945. b"\x01\x12\x43\xc8": {
  946. "modelName": SwitchbotModel.PLUG_MINI_IT,
  947. "modelFriendlyName": "Plug Mini (IT)",
  948. "func": process_relay_switch_1pm,
  949. "manufacturer_id": 2409,
  950. },
  951. b"\x00\x12\x18\xd0": {
  952. "modelName": SwitchbotModel.OUTDOOR_PLUG_EU,
  953. "modelFriendlyName": "Outdoor Plug (EU)",
  954. "func": process_outdoor_plug,
  955. "manufacturer_id": 2409,
  956. },
  957. b"\x00\x11\x07\x60": {
  958. "modelName": SwitchbotModel.STANDING_FAN,
  959. "modelFriendlyName": "Standing Fan",
  960. "func": process_standing_fan,
  961. "manufacturer_id": 2409,
  962. },
  963. b"\x01\x11\x07\x60": {
  964. "modelName": SwitchbotModel.STANDING_FAN,
  965. "modelFriendlyName": "Standing Fan",
  966. "func": process_standing_fan,
  967. "manufacturer_id": 2409,
  968. },
  969. b"\x00\x10\x53\xb0": {
  970. "modelName": SwitchbotModel.WEATHER_STATION,
  971. "modelFriendlyName": "Weather Station",
  972. "func": process_weather_station,
  973. "manufacturer_id": 2409,
  974. },
  975. b"\x01\x10\x53\xb0": {
  976. "modelName": SwitchbotModel.WEATHER_STATION,
  977. "modelFriendlyName": "Weather Station",
  978. "func": process_weather_station,
  979. "manufacturer_id": 2409,
  980. },
  981. }
  982. _SWITCHBOT_MODEL_TO_CHAR: defaultdict[SwitchbotModel, list[str | bytes]] = defaultdict(
  983. list
  984. )
  985. for model_chr, model_data in SUPPORTED_TYPES.items():
  986. _SWITCHBOT_MODEL_TO_CHAR[model_data["modelName"]].append(model_chr)
  987. MODELS_BY_MANUFACTURER_DATA: dict[int, list[tuple[str, SwitchbotSupportedType]]] = {
  988. mfr_id: [] for mfr_id in MFR_DATA_ORDER
  989. }
  990. for model_chr, model in SUPPORTED_TYPES.items():
  991. if "manufacturer_id" in model:
  992. mfr_id = model["manufacturer_id"]
  993. MODELS_BY_MANUFACTURER_DATA[mfr_id].append((model_chr, model))
  994. def parse_advertisement_data(
  995. device: BLEDevice,
  996. advertisement_data: AdvertisementData,
  997. model: SwitchbotModel | None = None,
  998. ) -> SwitchBotAdvertisement | None:
  999. """Parse advertisement data."""
  1000. upper_mac = format_mac_upper(device.address)
  1001. if model is None and upper_mac in _MODEL_TO_MAC_CACHE:
  1002. model = _MODEL_TO_MAC_CACHE[upper_mac]
  1003. service_data = advertisement_data.service_data
  1004. _service_data = None
  1005. for uuid in SERVICE_DATA_ORDER:
  1006. if uuid in service_data:
  1007. _service_data = service_data[uuid]
  1008. break
  1009. _mfr_data = None
  1010. _mfr_id = None
  1011. for mfr_id in MFR_DATA_ORDER:
  1012. if mfr_id in advertisement_data.manufacturer_data:
  1013. _mfr_id = mfr_id
  1014. _mfr_data = advertisement_data.manufacturer_data[mfr_id]
  1015. break
  1016. if _mfr_data is None and _service_data is None:
  1017. return None
  1018. try:
  1019. data = _parse_data(
  1020. _service_data,
  1021. _mfr_data,
  1022. _mfr_id,
  1023. model,
  1024. )
  1025. except Exception: # pylint: disable=broad-except
  1026. _LOGGER.exception("Failed to parse advertisement data: %s", advertisement_data)
  1027. return None
  1028. if not data:
  1029. return None
  1030. return SwitchBotAdvertisement(
  1031. device.address, data, device, advertisement_data.rssi, bool(_service_data)
  1032. )
  1033. def _find_model_from_service_data(_service_data: bytes) -> str | bytes | None:
  1034. """Find model from service data."""
  1035. char_model = chr(_service_data[0] & 0b01111111)
  1036. if char_model in SUPPORTED_TYPES:
  1037. return char_model
  1038. byte_model = bytes([_service_data[0] & 0b01111111])
  1039. if byte_model in SUPPORTED_TYPES:
  1040. return byte_model
  1041. return None
  1042. def _find_model_from_switchbot_model(
  1043. _switchbot_model: SwitchbotModel,
  1044. ) -> str | bytes | None:
  1045. """Find model from switchbot model."""
  1046. if _switchbot_model in _SWITCHBOT_MODEL_TO_CHAR:
  1047. return _SWITCHBOT_MODEL_TO_CHAR[_switchbot_model][0]
  1048. return None
  1049. def _find_model_from_manufacturer_data(
  1050. _mfr_id: int, _mfr_data: bytes | None
  1051. ) -> str | bytes | None:
  1052. """Find model from manufacturer data."""
  1053. if _mfr_id not in MODELS_BY_MANUFACTURER_DATA or _mfr_data is None:
  1054. return None
  1055. for model_chr, model_data in MODELS_BY_MANUFACTURER_DATA[_mfr_id]:
  1056. expected_length = model_data.get("manufacturer_data_length")
  1057. if expected_length is not None and expected_length == len(_mfr_data):
  1058. return model_chr
  1059. return None
  1060. def _find_model_from_service_data_suffix(_service_data: bytes) -> str | bytes | None:
  1061. """Find model from service data suffix."""
  1062. if len(_service_data) <= 5:
  1063. return None
  1064. for s in (_service_data[-4:], _service_data[-5:-1]):
  1065. if s in SUPPORTED_TYPES:
  1066. return s
  1067. return None
  1068. def build_advertisement_data(
  1069. _model: str | bytes, _service_data: bytes | None, _mfr_data: bytes | None
  1070. ) -> dict[str, Any]:
  1071. """Build advertisement data dictionary."""
  1072. _isEncrypted = bool(_service_data[0] & 0b10000000) if _service_data else False
  1073. data = {
  1074. "rawAdvData": _service_data,
  1075. "data": {},
  1076. "model": _model,
  1077. "isEncrypted": _isEncrypted,
  1078. }
  1079. type_data = SUPPORTED_TYPES.get(_model)
  1080. if type_data:
  1081. model_data = type_data["func"](_service_data, _mfr_data)
  1082. if model_data:
  1083. data.update(
  1084. {
  1085. "modelFriendlyName": type_data["modelFriendlyName"],
  1086. "modelName": type_data["modelName"],
  1087. "data": model_data,
  1088. }
  1089. )
  1090. return data
  1091. @lru_cache(maxsize=128)
  1092. def _parse_data(
  1093. _service_data: bytes | None,
  1094. _mfr_data: bytes | None,
  1095. _mfr_id: int | None = None,
  1096. _switchbot_model: SwitchbotModel | None = None,
  1097. ) -> dict[str, Any] | None:
  1098. """Parse advertisement data."""
  1099. _model = None
  1100. if _service_data:
  1101. _model = _find_model_from_service_data(_service_data)
  1102. if not _model and _switchbot_model:
  1103. _model = _find_model_from_switchbot_model(_switchbot_model)
  1104. if not _model and _mfr_id:
  1105. _model = _find_model_from_manufacturer_data(_mfr_id, _mfr_data)
  1106. if not _model and _service_data:
  1107. _model = _find_model_from_service_data_suffix(_service_data)
  1108. if not _model:
  1109. return None
  1110. return build_advertisement_data(_model, _service_data, _mfr_data)
  1111. def populate_model_to_mac_cache(mac: str, model: SwitchbotModel) -> None:
  1112. """Populate the model to MAC address cache."""
  1113. _MODEL_TO_MAC_CACHE[mac] = model