1
0

adv_parser.py 33 KB

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