adv_parser.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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
  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 process_light, process_rgbic_light, process_wostrip
  28. from .adv_parsers.lock import (
  29. process_lock2,
  30. process_locklite,
  31. process_wolock,
  32. process_wolock_pro,
  33. )
  34. from .adv_parsers.meter import process_wosensorth, process_wosensorth_c
  35. from .adv_parsers.motion import process_wopresence
  36. from .adv_parsers.plug import process_woplugmini
  37. from .adv_parsers.presence_sensor import process_presence_sensor
  38. from .adv_parsers.relay_switch import (
  39. process_garage_door_opener,
  40. process_relay_switch_1pm,
  41. process_relay_switch_2pm,
  42. process_relay_switch_common_data,
  43. )
  44. from .adv_parsers.remote import process_woremote
  45. from .adv_parsers.roller_shade import process_worollershade
  46. from .adv_parsers.smart_thermostat_radiator import process_smart_thermostat_radiator
  47. from .adv_parsers.vacuum import process_vacuum, process_vacuum_k
  48. from .adv_parsers.weather_station import process_weather_station
  49. from .const import SwitchbotModel
  50. from .models import SwitchBotAdvertisement
  51. from .utils import format_mac_upper
  52. _LOGGER = logging.getLogger(__name__)
  53. SERVICE_DATA_ORDER = (
  54. "0000fd3d-0000-1000-8000-00805f9b34fb",
  55. "00000d00-0000-1000-8000-00805f9b34fb",
  56. )
  57. MFR_DATA_ORDER = (2409, 741, 89)
  58. _MODEL_TO_MAC_CACHE: dict[str, SwitchbotModel] = {}
  59. class SwitchbotSupportedType(TypedDict):
  60. """Supported type of Switchbot."""
  61. modelName: SwitchbotModel
  62. modelFriendlyName: str
  63. func: Callable[[bytes, bytes | None], dict[str, bool | int]]
  64. manufacturer_id: int | None
  65. manufacturer_data_length: int | None
  66. SUPPORTED_TYPES: dict[str | bytes, SwitchbotSupportedType] = {
  67. "d": {
  68. "modelName": SwitchbotModel.CONTACT_SENSOR,
  69. "modelFriendlyName": "Contact Sensor",
  70. "func": process_wocontact,
  71. "manufacturer_id": 2409,
  72. },
  73. "D": {
  74. "modelName": SwitchbotModel.CONTACT_SENSOR,
  75. "modelFriendlyName": "Contact Sensor",
  76. "func": process_wocontact,
  77. "manufacturer_id": 2409,
  78. },
  79. "H": {
  80. "modelName": SwitchbotModel.BOT,
  81. "modelFriendlyName": "Bot",
  82. "func": process_wohand,
  83. "manufacturer_id": 89,
  84. },
  85. "s": {
  86. "modelName": SwitchbotModel.MOTION_SENSOR,
  87. "modelFriendlyName": "Motion Sensor",
  88. "func": process_wopresence,
  89. "manufacturer_id": 2409,
  90. },
  91. "S": {
  92. "modelName": SwitchbotModel.MOTION_SENSOR,
  93. "modelFriendlyName": "Motion Sensor",
  94. "func": process_wopresence,
  95. "manufacturer_id": 2409,
  96. },
  97. "r": {
  98. "modelName": SwitchbotModel.LIGHT_STRIP,
  99. "modelFriendlyName": "Light Strip",
  100. "func": process_wostrip,
  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. "{": {
  110. "modelName": SwitchbotModel.CURTAIN,
  111. "modelFriendlyName": "Curtain 3",
  112. "func": process_wocurtain,
  113. "manufacturer_id": 2409,
  114. },
  115. "[": {
  116. "modelName": SwitchbotModel.CURTAIN,
  117. "modelFriendlyName": "Curtain 3",
  118. "func": process_wocurtain,
  119. "manufacturer_id": 2409,
  120. },
  121. "c": {
  122. "modelName": SwitchbotModel.CURTAIN,
  123. "modelFriendlyName": "Curtain",
  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. "w": {
  134. "modelName": SwitchbotModel.IO_METER,
  135. "modelFriendlyName": "Indoor/Outdoor Meter",
  136. "func": process_wosensorth,
  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. "i": {
  146. "modelName": SwitchbotModel.METER,
  147. "modelFriendlyName": "Meter Plus",
  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. "T": {
  158. "modelName": SwitchbotModel.METER,
  159. "modelFriendlyName": "Meter",
  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. "4": {
  170. "modelName": SwitchbotModel.METER_PRO,
  171. "modelFriendlyName": "Meter Pro",
  172. "func": process_wosensorth,
  173. "manufacturer_id": 2409,
  174. },
  175. b"\x14": {
  176. "modelName": SwitchbotModel.METER_PRO,
  177. "modelFriendlyName": "Meter Pro",
  178. "func": process_wosensorth,
  179. "manufacturer_id": 2409,
  180. },
  181. "5": {
  182. "modelName": SwitchbotModel.METER_PRO_C,
  183. "modelFriendlyName": "Meter Pro CO2",
  184. "func": process_wosensorth_c,
  185. "manufacturer_id": 2409,
  186. },
  187. b"\x15": {
  188. "modelName": SwitchbotModel.METER_PRO_C,
  189. "modelFriendlyName": "Meter Pro CO2",
  190. "func": process_wosensorth_c,
  191. "manufacturer_id": 2409,
  192. },
  193. "v": {
  194. "modelName": SwitchbotModel.HUB2,
  195. "modelFriendlyName": "Hub 2",
  196. "func": process_wohub2,
  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. "g": {
  206. "modelName": SwitchbotModel.PLUG_MINI,
  207. "modelFriendlyName": "Plug Mini",
  208. "func": process_woplugmini,
  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. "j": {
  218. "modelName": SwitchbotModel.PLUG_MINI,
  219. "modelFriendlyName": "Plug Mini (JP)",
  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. "u": {
  230. "modelName": SwitchbotModel.COLOR_BULB,
  231. "modelFriendlyName": "Color Bulb",
  232. "func": process_color_bulb,
  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. "q": {
  242. "modelName": SwitchbotModel.CEILING_LIGHT,
  243. "modelFriendlyName": "Ceiling Light",
  244. "func": process_woceiling,
  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. "n": {
  254. "modelName": SwitchbotModel.CEILING_LIGHT,
  255. "modelFriendlyName": "Ceiling Light Pro",
  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. "e": {
  266. "modelName": SwitchbotModel.HUMIDIFIER,
  267. "modelFriendlyName": "Humidifier",
  268. "func": process_wohumidifier,
  269. "manufacturer_id": 741,
  270. "manufacturer_data_length": 6,
  271. },
  272. "E": {
  273. "modelName": SwitchbotModel.HUMIDIFIER,
  274. "modelFriendlyName": "Humidifier",
  275. "func": process_wohumidifier,
  276. "manufacturer_id": 741,
  277. "manufacturer_data_length": 6,
  278. },
  279. "#": {
  280. "modelName": SwitchbotModel.EVAPORATIVE_HUMIDIFIER,
  281. "modelFriendlyName": "Evaporative Humidifier",
  282. "func": process_evaporative_humidifier,
  283. "manufacturer_id": 2409,
  284. },
  285. b"\x03": {
  286. "modelName": SwitchbotModel.EVAPORATIVE_HUMIDIFIER,
  287. "modelFriendlyName": "Evaporative Humidifier",
  288. "func": process_evaporative_humidifier,
  289. "manufacturer_id": 2409,
  290. },
  291. "o": {
  292. "modelName": SwitchbotModel.LOCK,
  293. "modelFriendlyName": "Lock",
  294. "func": process_wolock,
  295. "manufacturer_id": 2409,
  296. },
  297. "O": {
  298. "modelName": SwitchbotModel.LOCK,
  299. "modelFriendlyName": "Lock",
  300. "func": process_wolock,
  301. "manufacturer_id": 2409,
  302. },
  303. "$": {
  304. "modelName": SwitchbotModel.LOCK_PRO,
  305. "modelFriendlyName": "Lock Pro",
  306. "func": process_wolock_pro,
  307. "manufacturer_id": 2409,
  308. },
  309. b"\x04": {
  310. "modelName": SwitchbotModel.LOCK_PRO,
  311. "modelFriendlyName": "Lock Pro",
  312. "func": process_wolock_pro,
  313. "manufacturer_id": 2409,
  314. },
  315. "x": {
  316. "modelName": SwitchbotModel.BLIND_TILT,
  317. "modelFriendlyName": "Blind Tilt",
  318. "func": process_woblindtilt,
  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. "&": {
  328. "modelName": SwitchbotModel.LEAK,
  329. "modelFriendlyName": "Leak Detector",
  330. "func": process_leak,
  331. "manufacturer_id": 2409,
  332. },
  333. b"\x06": {
  334. "modelName": SwitchbotModel.LEAK,
  335. "modelFriendlyName": "Leak Detector",
  336. "func": process_leak,
  337. "manufacturer_id": 2409,
  338. },
  339. "y": {
  340. "modelName": SwitchbotModel.KEYPAD,
  341. "modelFriendlyName": "Keypad",
  342. "func": process_wokeypad,
  343. "manufacturer_id": 2409,
  344. },
  345. "Y": {
  346. "modelName": SwitchbotModel.KEYPAD,
  347. "modelFriendlyName": "Keypad",
  348. "func": process_wokeypad,
  349. "manufacturer_id": 2409,
  350. },
  351. "<": {
  352. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  353. "modelFriendlyName": "Relay Switch 1PM",
  354. "func": process_relay_switch_1pm,
  355. "manufacturer_id": 2409,
  356. },
  357. b"\x1c": {
  358. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  359. "modelFriendlyName": "Relay Switch 1PM",
  360. "func": process_relay_switch_1pm,
  361. "manufacturer_id": 2409,
  362. },
  363. ";": {
  364. "modelName": SwitchbotModel.RELAY_SWITCH_1,
  365. "modelFriendlyName": "Relay Switch 1",
  366. "func": process_relay_switch_common_data,
  367. "manufacturer_id": 2409,
  368. },
  369. b"\x1b": {
  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": {
  376. "modelName": SwitchbotModel.REMOTE,
  377. "modelFriendlyName": "Remote",
  378. "func": process_woremote,
  379. "manufacturer_id": 89,
  380. },
  381. "B": {
  382. "modelName": SwitchbotModel.REMOTE,
  383. "modelFriendlyName": "Remote",
  384. "func": process_woremote,
  385. "manufacturer_id": 89,
  386. },
  387. ",": {
  388. "modelName": SwitchbotModel.ROLLER_SHADE,
  389. "modelFriendlyName": "Roller Shade",
  390. "func": process_worollershade,
  391. "manufacturer_id": 2409,
  392. },
  393. b"\x0c": {
  394. "modelName": SwitchbotModel.ROLLER_SHADE,
  395. "modelFriendlyName": "Roller Shade",
  396. "func": process_worollershade,
  397. "manufacturer_id": 2409,
  398. },
  399. "%": {
  400. "modelName": SwitchbotModel.HUBMINI_MATTER,
  401. "modelFriendlyName": "HubMini Matter",
  402. "func": process_hubmini_matter,
  403. "manufacturer_id": 2409,
  404. },
  405. b"\x05": {
  406. "modelName": SwitchbotModel.HUBMINI_MATTER,
  407. "modelFriendlyName": "HubMini Matter",
  408. "func": process_hubmini_matter,
  409. "manufacturer_id": 2409,
  410. },
  411. "~": {
  412. "modelName": SwitchbotModel.CIRCULATOR_FAN,
  413. "modelFriendlyName": "Circulator Fan",
  414. "func": process_fan,
  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.K20_VACUUM,
  425. "modelFriendlyName": "K20 Vacuum",
  426. "func": process_vacuum,
  427. "manufacturer_id": 2409,
  428. },
  429. b"\x0f": {
  430. "modelName": SwitchbotModel.K20_VACUUM,
  431. "modelFriendlyName": "K20 Vacuum",
  432. "func": process_vacuum,
  433. "manufacturer_id": 2409,
  434. },
  435. "z": {
  436. "modelName": SwitchbotModel.S10_VACUUM,
  437. "modelFriendlyName": "S10 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. "3": {
  448. "modelName": SwitchbotModel.K10_PRO_COMBO_VACUUM,
  449. "modelFriendlyName": "K10+ Pro Combo Vacuum",
  450. "func": process_vacuum,
  451. "manufacturer_id": 2409,
  452. },
  453. b"\x13": {
  454. "modelName": SwitchbotModel.K10_PRO_COMBO_VACUUM,
  455. "modelFriendlyName": "K10+ Pro Combo Vacuum",
  456. "func": process_vacuum,
  457. "manufacturer_id": 2409,
  458. },
  459. "}": {
  460. "modelName": SwitchbotModel.K10_VACUUM,
  461. "modelFriendlyName": "K10+ Vacuum",
  462. "func": process_vacuum_k,
  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_PRO_VACUUM,
  473. "modelFriendlyName": "K10+ Pro Vacuum",
  474. "func": process_vacuum_k,
  475. "manufacturer_id": 2409,
  476. },
  477. b"\x08": {
  478. "modelName": SwitchbotModel.K10_PRO_VACUUM,
  479. "modelFriendlyName": "K10+ Pro Vacuum",
  480. "func": process_vacuum_k,
  481. "manufacturer_id": 2409,
  482. },
  483. "*": {
  484. "modelName": SwitchbotModel.AIR_PURIFIER_US,
  485. "modelFriendlyName": "Air Purifier US",
  486. "func": process_air_purifier,
  487. "manufacturer_id": 2409,
  488. },
  489. b"\x0a": {
  490. "modelName": SwitchbotModel.AIR_PURIFIER_US,
  491. "modelFriendlyName": "Air Purifier US",
  492. "func": process_air_purifier,
  493. "manufacturer_id": 2409,
  494. },
  495. "+": {
  496. "modelName": SwitchbotModel.AIR_PURIFIER_JP,
  497. "modelFriendlyName": "Air Purifier JP",
  498. "func": process_air_purifier,
  499. "manufacturer_id": 2409,
  500. },
  501. b"\x0b": {
  502. "modelName": SwitchbotModel.AIR_PURIFIER_JP,
  503. "modelFriendlyName": "Air Purifier JP",
  504. "func": process_air_purifier,
  505. "manufacturer_id": 2409,
  506. },
  507. "7": {
  508. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE_US,
  509. "modelFriendlyName": "Air Purifier Table US",
  510. "func": process_air_purifier,
  511. "manufacturer_id": 2409,
  512. },
  513. b"\x17": {
  514. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE_US,
  515. "modelFriendlyName": "Air Purifier Table US",
  516. "func": process_air_purifier,
  517. "manufacturer_id": 2409,
  518. },
  519. "8": {
  520. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE_JP,
  521. "modelFriendlyName": "Air Purifier Table JP",
  522. "func": process_air_purifier,
  523. "manufacturer_id": 2409,
  524. },
  525. b"\x18": {
  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"\x00\x10\xb9\x40": {
  532. "modelName": SwitchbotModel.HUB3,
  533. "modelFriendlyName": "Hub3",
  534. "func": process_hub3,
  535. "manufacturer_id": 2409,
  536. },
  537. b"\x01\x10\xb9\x40": {
  538. "modelName": SwitchbotModel.HUB3,
  539. "modelFriendlyName": "Hub3",
  540. "func": process_hub3,
  541. "manufacturer_id": 2409,
  542. },
  543. "-": {
  544. "modelName": SwitchbotModel.LOCK_LITE,
  545. "modelFriendlyName": "Lock Lite",
  546. "func": process_locklite,
  547. "manufacturer_id": 2409,
  548. },
  549. b"\x0d": {
  550. "modelName": SwitchbotModel.LOCK_LITE,
  551. "modelFriendlyName": "Lock Lite",
  552. "func": process_locklite,
  553. "manufacturer_id": 2409,
  554. },
  555. b"\x00\x10\xa5\xb8": {
  556. "modelName": SwitchbotModel.LOCK_ULTRA,
  557. "modelFriendlyName": "Lock Ultra",
  558. "func": process_lock2,
  559. "manufacturer_id": 2409,
  560. },
  561. b"\x01\x10\xa5\xb8": {
  562. "modelName": SwitchbotModel.LOCK_ULTRA,
  563. "modelFriendlyName": "Lock Ultra",
  564. "func": process_lock2,
  565. "manufacturer_id": 2409,
  566. },
  567. ">": {
  568. "modelName": SwitchbotModel.GARAGE_DOOR_OPENER,
  569. "modelFriendlyName": "Garage Door Opener",
  570. "func": process_garage_door_opener,
  571. "manufacturer_id": 2409,
  572. },
  573. b"\x1e": {
  574. "modelName": SwitchbotModel.GARAGE_DOOR_OPENER,
  575. "modelFriendlyName": "Garage Door Opener",
  576. "func": process_garage_door_opener,
  577. "manufacturer_id": 2409,
  578. },
  579. "=": {
  580. "modelName": SwitchbotModel.RELAY_SWITCH_2PM,
  581. "modelFriendlyName": "Relay Switch 2PM",
  582. "func": process_relay_switch_2pm,
  583. "manufacturer_id": 2409,
  584. },
  585. b"\x1d": {
  586. "modelName": SwitchbotModel.RELAY_SWITCH_2PM,
  587. "modelFriendlyName": "Relay Switch 2PM",
  588. "func": process_relay_switch_2pm,
  589. "manufacturer_id": 2409,
  590. },
  591. b"\x00\x10\xd0\xb0": {
  592. "modelName": SwitchbotModel.FLOOR_LAMP,
  593. "modelFriendlyName": "Floor Lamp",
  594. "func": process_light,
  595. "manufacturer_id": 2409,
  596. },
  597. b"\x01\x10\xd0\xb0": {
  598. "modelName": SwitchbotModel.FLOOR_LAMP,
  599. "modelFriendlyName": "Floor Lamp",
  600. "func": process_light,
  601. "manufacturer_id": 2409,
  602. },
  603. b"\x00\x10\xd0\xb1": {
  604. "modelName": SwitchbotModel.STRIP_LIGHT_3,
  605. "modelFriendlyName": "Strip Light 3",
  606. "func": process_light,
  607. "manufacturer_id": 2409,
  608. },
  609. b"\x01\x10\xd0\xb1": {
  610. "modelName": SwitchbotModel.STRIP_LIGHT_3,
  611. "modelFriendlyName": "Strip Light 3",
  612. "func": process_light,
  613. "manufacturer_id": 2409,
  614. },
  615. "?": {
  616. "modelName": SwitchbotModel.PLUG_MINI_EU,
  617. "modelFriendlyName": "Plug Mini (EU)",
  618. "func": process_relay_switch_1pm,
  619. "manufacturer_id": 2409,
  620. },
  621. b"\x1f": {
  622. "modelName": SwitchbotModel.PLUG_MINI_EU,
  623. "modelFriendlyName": "Plug Mini (EU)",
  624. "func": process_relay_switch_1pm,
  625. "manufacturer_id": 2409,
  626. },
  627. b"\x00\x10\xd0\xb3": {
  628. "modelName": SwitchbotModel.RGBICWW_STRIP_LIGHT,
  629. "modelFriendlyName": "RGBICWW Strip Light",
  630. "func": process_rgbic_light,
  631. "manufacturer_id": 2409,
  632. },
  633. b"\x01\x10\xd0\xb3": {
  634. "modelName": SwitchbotModel.RGBICWW_STRIP_LIGHT,
  635. "modelFriendlyName": "RGBICWW Strip Light",
  636. "func": process_rgbic_light,
  637. "manufacturer_id": 2409,
  638. },
  639. b"\x00\x10\xd0\xb4": {
  640. "modelName": SwitchbotModel.RGBICWW_FLOOR_LAMP,
  641. "modelFriendlyName": "RGBICWW Floor Lamp",
  642. "func": process_rgbic_light,
  643. "manufacturer_id": 2409,
  644. },
  645. b"\x01\x10\xd0\xb4": {
  646. "modelName": SwitchbotModel.RGBICWW_FLOOR_LAMP,
  647. "modelFriendlyName": "RGBICWW Floor Lamp",
  648. "func": process_rgbic_light,
  649. "manufacturer_id": 2409,
  650. },
  651. b"\x00\x10\xfb\xa8": {
  652. "modelName": SwitchbotModel.K11_VACUUM,
  653. "modelFriendlyName": "K11+ Vacuum",
  654. "func": process_vacuum,
  655. "manufacturer_id": 2409,
  656. },
  657. b"\x01\x10\xfb\xa8": {
  658. "modelName": SwitchbotModel.K11_VACUUM,
  659. "modelFriendlyName": "K11+ Vacuum",
  660. "func": process_vacuum,
  661. "manufacturer_id": 2409,
  662. },
  663. b"\x00\x10\xf3\xd8": {
  664. "modelName": SwitchbotModel.CLIMATE_PANEL,
  665. "modelFriendlyName": "Climate Panel",
  666. "func": process_climate_panel,
  667. "manufacturer_id": 2409,
  668. },
  669. b"\x01\x10\xf3\xd8": {
  670. "modelName": SwitchbotModel.CLIMATE_PANEL,
  671. "modelFriendlyName": "Climate Panel",
  672. "func": process_climate_panel,
  673. "manufacturer_id": 2409,
  674. },
  675. b"\x00\x116@": {
  676. "modelName": SwitchbotModel.SMART_THERMOSTAT_RADIATOR,
  677. "modelFriendlyName": "Smart Thermostat Radiator",
  678. "func": process_smart_thermostat_radiator,
  679. "manufacturer_id": 2409,
  680. },
  681. b"\x01\x116@": {
  682. "modelName": SwitchbotModel.SMART_THERMOSTAT_RADIATOR,
  683. "modelFriendlyName": "Smart Thermostat Radiator",
  684. "func": process_smart_thermostat_radiator,
  685. "manufacturer_id": 2409,
  686. },
  687. b"\x00\x10\xe0P": {
  688. "modelName": SwitchbotModel.S20_VACUUM,
  689. "modelFriendlyName": "S20 Vacuum",
  690. "func": process_vacuum,
  691. "manufacturer_id": 2409,
  692. },
  693. b"\x01\x10\xe0P": {
  694. "modelName": SwitchbotModel.S20_VACUUM,
  695. "modelFriendlyName": "S20 Vacuum",
  696. "func": process_vacuum,
  697. "manufacturer_id": 2409,
  698. },
  699. b"\x00\x10\xcc\xc8": {
  700. "modelName": SwitchbotModel.PRESENCE_SENSOR,
  701. "modelFriendlyName": "Presence Sensor",
  702. "func": process_presence_sensor,
  703. "manufacturer_id": 2409,
  704. },
  705. b"\x01\x10\xcc\xc8": {
  706. "modelName": SwitchbotModel.PRESENCE_SENSOR,
  707. "modelFriendlyName": "Presence Sensor",
  708. "func": process_presence_sensor,
  709. "manufacturer_id": 2409,
  710. },
  711. b"\x00\x11>\x10": {
  712. "modelName": SwitchbotModel.ART_FRAME,
  713. "modelFriendlyName": "Art Frame",
  714. "func": process_art_frame,
  715. "manufacturer_id": 2409,
  716. },
  717. b"\x01\x11>\x10": {
  718. "modelName": SwitchbotModel.ART_FRAME,
  719. "modelFriendlyName": "Art Frame",
  720. "func": process_art_frame,
  721. "manufacturer_id": 2409,
  722. },
  723. b"\x00\x11\x03x": {
  724. "modelName": SwitchbotModel.KEYPAD_VISION,
  725. "modelFriendlyName": "Keypad Vision",
  726. "func": process_keypad_vision,
  727. "manufacturer_id": 2409,
  728. },
  729. b"\x01\x11\x03x": {
  730. "modelName": SwitchbotModel.KEYPAD_VISION,
  731. "modelFriendlyName": "Keypad Vision",
  732. "func": process_keypad_vision,
  733. "manufacturer_id": 2409,
  734. },
  735. b"\x00\x11Q\x98": {
  736. "modelName": SwitchbotModel.KEYPAD_VISION_PRO,
  737. "modelFriendlyName": "Keypad Vision Pro",
  738. "func": process_keypad_vision_pro,
  739. "manufacturer_id": 2409,
  740. },
  741. b"\x01\x11Q\x98": {
  742. "modelName": SwitchbotModel.KEYPAD_VISION_PRO,
  743. "modelFriendlyName": "Keypad Vision Pro",
  744. "func": process_keypad_vision_pro,
  745. "manufacturer_id": 2409,
  746. },
  747. b"\x00\x11\x69\x09": {
  748. "modelName": SwitchbotModel.LOCK_VISION_PRO,
  749. "modelFriendlyName": "Lock Vision Pro",
  750. "func": process_lock2,
  751. "manufacturer_id": 2409,
  752. },
  753. b"\x01\x11\x69\x09": {
  754. "modelName": SwitchbotModel.LOCK_VISION_PRO,
  755. "modelFriendlyName": "Lock Vision Pro",
  756. "func": process_lock2,
  757. "manufacturer_id": 2409,
  758. },
  759. b"\x00\x11\x69\x08": {
  760. "modelName": SwitchbotModel.LOCK_VISION,
  761. "modelFriendlyName": "Lock Vision",
  762. "func": process_locklite,
  763. "manufacturer_id": 2409,
  764. },
  765. b"\x01\x11\x69\x08": {
  766. "modelName": SwitchbotModel.LOCK_VISION,
  767. "modelFriendlyName": "Lock Vision",
  768. "func": process_locklite,
  769. "manufacturer_id": 2409,
  770. },
  771. b"\x00\x10\xff\x90": {
  772. "modelName": SwitchbotModel.LOCK_PRO_WIFI,
  773. "modelFriendlyName": "Lock Pro Wifi",
  774. "func": process_wolock_pro,
  775. "manufacturer_id": 2409,
  776. },
  777. b"\x01\x10\xff\x90": {
  778. "modelName": SwitchbotModel.LOCK_PRO_WIFI,
  779. "modelFriendlyName": "Lock Pro Wifi",
  780. "func": process_wolock_pro,
  781. "manufacturer_id": 2409,
  782. },
  783. b"\x00\x10\x53\xb0": {
  784. "modelName": SwitchbotModel.WEATHER_STATION,
  785. "modelFriendlyName": "Weather Station",
  786. "func": process_weather_station,
  787. "manufacturer_id": 2409,
  788. },
  789. b"\x01\x10\x53\xb0": {
  790. "modelName": SwitchbotModel.WEATHER_STATION,
  791. "modelFriendlyName": "Weather Station",
  792. "func": process_weather_station,
  793. "manufacturer_id": 2409,
  794. },
  795. }
  796. _SWITCHBOT_MODEL_TO_CHAR: defaultdict[SwitchbotModel, list[str | bytes]] = defaultdict(
  797. list
  798. )
  799. for model_chr, model_data in SUPPORTED_TYPES.items():
  800. _SWITCHBOT_MODEL_TO_CHAR[model_data["modelName"]].append(model_chr)
  801. MODELS_BY_MANUFACTURER_DATA: dict[int, list[tuple[str, SwitchbotSupportedType]]] = {
  802. mfr_id: [] for mfr_id in MFR_DATA_ORDER
  803. }
  804. for model_chr, model in SUPPORTED_TYPES.items():
  805. if "manufacturer_id" in model:
  806. mfr_id = model["manufacturer_id"]
  807. MODELS_BY_MANUFACTURER_DATA[mfr_id].append((model_chr, model))
  808. def parse_advertisement_data(
  809. device: BLEDevice,
  810. advertisement_data: AdvertisementData,
  811. model: SwitchbotModel | None = None,
  812. ) -> SwitchBotAdvertisement | None:
  813. """Parse advertisement data."""
  814. upper_mac = format_mac_upper(device.address)
  815. if model is None and upper_mac in _MODEL_TO_MAC_CACHE:
  816. model = _MODEL_TO_MAC_CACHE[upper_mac]
  817. service_data = advertisement_data.service_data
  818. _service_data = None
  819. for uuid in SERVICE_DATA_ORDER:
  820. if uuid in service_data:
  821. _service_data = service_data[uuid]
  822. break
  823. _mfr_data = None
  824. _mfr_id = None
  825. for mfr_id in MFR_DATA_ORDER:
  826. if mfr_id in advertisement_data.manufacturer_data:
  827. _mfr_id = mfr_id
  828. _mfr_data = advertisement_data.manufacturer_data[mfr_id]
  829. break
  830. if _mfr_data is None and _service_data is None:
  831. return None
  832. try:
  833. data = _parse_data(
  834. _service_data,
  835. _mfr_data,
  836. _mfr_id,
  837. model,
  838. )
  839. except Exception: # pylint: disable=broad-except
  840. _LOGGER.exception("Failed to parse advertisement data: %s", advertisement_data)
  841. return None
  842. if not data:
  843. return None
  844. return SwitchBotAdvertisement(
  845. device.address, data, device, advertisement_data.rssi, bool(_service_data)
  846. )
  847. def _find_model_from_service_data(_service_data: bytes) -> str | bytes | None:
  848. """Find model from service data."""
  849. char_model = chr(_service_data[0] & 0b01111111)
  850. if char_model in SUPPORTED_TYPES:
  851. return char_model
  852. byte_model = bytes([_service_data[0] & 0b01111111])
  853. if byte_model in SUPPORTED_TYPES:
  854. return byte_model
  855. return None
  856. def _find_model_from_switchbot_model(
  857. _switchbot_model: SwitchbotModel,
  858. ) -> str | bytes | None:
  859. """Find model from switchbot model."""
  860. if _switchbot_model in _SWITCHBOT_MODEL_TO_CHAR:
  861. return _SWITCHBOT_MODEL_TO_CHAR[_switchbot_model][0]
  862. return None
  863. def _find_model_from_manufacturer_data(
  864. _mfr_id: int, _mfr_data: bytes | None
  865. ) -> str | bytes | None:
  866. """Find model from manufacturer data."""
  867. if _mfr_id not in MODELS_BY_MANUFACTURER_DATA or _mfr_data is None:
  868. return None
  869. for model_chr, model_data in MODELS_BY_MANUFACTURER_DATA[_mfr_id]:
  870. expected_length = model_data.get("manufacturer_data_length")
  871. if expected_length is not None and expected_length == len(_mfr_data):
  872. return model_chr
  873. return None
  874. def _find_model_from_service_data_suffix(_service_data: bytes) -> str | bytes | None:
  875. """Find model from service data suffix."""
  876. if len(_service_data) <= 5:
  877. return None
  878. for s in (_service_data[-4:], _service_data[-5:-1]):
  879. if s in SUPPORTED_TYPES:
  880. return s
  881. return None
  882. def build_advertisement_data(
  883. _model: str | bytes, _service_data: bytes | None, _mfr_data: bytes | None
  884. ) -> dict[str, Any]:
  885. """Build advertisement data dictionary."""
  886. _isEncrypted = bool(_service_data[0] & 0b10000000) if _service_data else False
  887. data = {
  888. "rawAdvData": _service_data,
  889. "data": {},
  890. "model": _model,
  891. "isEncrypted": _isEncrypted,
  892. }
  893. type_data = SUPPORTED_TYPES.get(_model)
  894. if type_data:
  895. model_data = type_data["func"](_service_data, _mfr_data)
  896. if model_data:
  897. data.update(
  898. {
  899. "modelFriendlyName": type_data["modelFriendlyName"],
  900. "modelName": type_data["modelName"],
  901. "data": model_data,
  902. }
  903. )
  904. return data
  905. @lru_cache(maxsize=128)
  906. def _parse_data(
  907. _service_data: bytes | None,
  908. _mfr_data: bytes | None,
  909. _mfr_id: int | None = None,
  910. _switchbot_model: SwitchbotModel | None = None,
  911. ) -> dict[str, Any] | None:
  912. """Parse advertisement data."""
  913. _model = None
  914. if _service_data:
  915. _model = _find_model_from_service_data(_service_data)
  916. if not _model and _switchbot_model:
  917. _model = _find_model_from_switchbot_model(_switchbot_model)
  918. if not _model and _mfr_id:
  919. _model = _find_model_from_manufacturer_data(_mfr_id, _mfr_data)
  920. if not _model and _service_data:
  921. _model = _find_model_from_service_data_suffix(_service_data)
  922. if not _model:
  923. return None
  924. return build_advertisement_data(_model, _service_data, _mfr_data)
  925. def populate_model_to_mac_cache(mac: str, model: SwitchbotModel) -> None:
  926. """Populate the model to MAC address cache."""
  927. _MODEL_TO_MAC_CACHE[mac] = model