adv_parser.py 37 KB

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