1
0

adv_parser.py 37 KB

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