1
0

adv_parser.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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.blind_tilt import process_woblindtilt
  12. from .adv_parsers.bot import process_wohand
  13. from .adv_parsers.bulb import process_color_bulb
  14. from .adv_parsers.ceiling_light import process_woceiling
  15. from .adv_parsers.climate_panel import process_climate_panel
  16. from .adv_parsers.contact import process_wocontact
  17. from .adv_parsers.curtain import process_wocurtain
  18. from .adv_parsers.fan import process_fan
  19. from .adv_parsers.hub2 import process_wohub2
  20. from .adv_parsers.hub3 import process_hub3
  21. from .adv_parsers.hubmini_matter import process_hubmini_matter
  22. from .adv_parsers.humidifier import process_evaporative_humidifier, process_wohumidifier
  23. from .adv_parsers.keypad import process_wokeypad
  24. from .adv_parsers.leak import process_leak
  25. from .adv_parsers.light_strip import process_light, process_rgbic_light, process_wostrip
  26. from .adv_parsers.lock import (
  27. process_lock2,
  28. process_locklite,
  29. process_wolock,
  30. process_wolock_pro,
  31. )
  32. from .adv_parsers.meter import process_wosensorth, process_wosensorth_c
  33. from .adv_parsers.motion import process_wopresence
  34. from .adv_parsers.plug import process_woplugmini
  35. from .adv_parsers.presence_sensor import process_presence_sensor
  36. from .adv_parsers.relay_switch import (
  37. process_garage_door_opener,
  38. process_relay_switch_1pm,
  39. process_relay_switch_2pm,
  40. process_relay_switch_common_data,
  41. )
  42. from .adv_parsers.remote import process_woremote
  43. from .adv_parsers.roller_shade import process_worollershade
  44. from .adv_parsers.smart_thermostat_radiator import process_smart_thermostat_radiator
  45. from .adv_parsers.vacuum import process_vacuum, process_vacuum_k
  46. from .const import SwitchbotModel
  47. from .models import SwitchBotAdvertisement
  48. from .utils import format_mac_upper
  49. _LOGGER = logging.getLogger(__name__)
  50. SERVICE_DATA_ORDER = (
  51. "0000fd3d-0000-1000-8000-00805f9b34fb",
  52. "00000d00-0000-1000-8000-00805f9b34fb",
  53. )
  54. MFR_DATA_ORDER = (2409, 741, 89)
  55. _MODEL_TO_MAC_CACHE: dict[str, SwitchbotModel] = {}
  56. class SwitchbotSupportedType(TypedDict):
  57. """Supported type of Switchbot."""
  58. modelName: SwitchbotModel
  59. modelFriendlyName: str
  60. func: Callable[[bytes, bytes | None], dict[str, bool | int]]
  61. manufacturer_id: int | None
  62. manufacturer_data_length: int | None
  63. SUPPORTED_TYPES: dict[str | bytes, SwitchbotSupportedType] = {
  64. "d": {
  65. "modelName": SwitchbotModel.CONTACT_SENSOR,
  66. "modelFriendlyName": "Contact Sensor",
  67. "func": process_wocontact,
  68. "manufacturer_id": 2409,
  69. },
  70. "D": {
  71. "modelName": SwitchbotModel.CONTACT_SENSOR,
  72. "modelFriendlyName": "Contact Sensor",
  73. "func": process_wocontact,
  74. "manufacturer_id": 2409,
  75. },
  76. "H": {
  77. "modelName": SwitchbotModel.BOT,
  78. "modelFriendlyName": "Bot",
  79. "func": process_wohand,
  80. "manufacturer_id": 89,
  81. },
  82. "s": {
  83. "modelName": SwitchbotModel.MOTION_SENSOR,
  84. "modelFriendlyName": "Motion Sensor",
  85. "func": process_wopresence,
  86. "manufacturer_id": 2409,
  87. },
  88. "S": {
  89. "modelName": SwitchbotModel.MOTION_SENSOR,
  90. "modelFriendlyName": "Motion Sensor",
  91. "func": process_wopresence,
  92. "manufacturer_id": 2409,
  93. },
  94. "r": {
  95. "modelName": SwitchbotModel.LIGHT_STRIP,
  96. "modelFriendlyName": "Light Strip",
  97. "func": process_wostrip,
  98. "manufacturer_id": 2409,
  99. },
  100. "R": {
  101. "modelName": SwitchbotModel.LIGHT_STRIP,
  102. "modelFriendlyName": "Light Strip",
  103. "func": process_wostrip,
  104. "manufacturer_id": 2409,
  105. },
  106. "{": {
  107. "modelName": SwitchbotModel.CURTAIN,
  108. "modelFriendlyName": "Curtain 3",
  109. "func": process_wocurtain,
  110. "manufacturer_id": 2409,
  111. },
  112. "[": {
  113. "modelName": SwitchbotModel.CURTAIN,
  114. "modelFriendlyName": "Curtain 3",
  115. "func": process_wocurtain,
  116. "manufacturer_id": 2409,
  117. },
  118. "c": {
  119. "modelName": SwitchbotModel.CURTAIN,
  120. "modelFriendlyName": "Curtain",
  121. "func": process_wocurtain,
  122. "manufacturer_id": 2409,
  123. },
  124. "C": {
  125. "modelName": SwitchbotModel.CURTAIN,
  126. "modelFriendlyName": "Curtain",
  127. "func": process_wocurtain,
  128. "manufacturer_id": 2409,
  129. },
  130. "w": {
  131. "modelName": SwitchbotModel.IO_METER,
  132. "modelFriendlyName": "Indoor/Outdoor Meter",
  133. "func": process_wosensorth,
  134. "manufacturer_id": 2409,
  135. },
  136. "W": {
  137. "modelName": SwitchbotModel.IO_METER,
  138. "modelFriendlyName": "Indoor/Outdoor Meter",
  139. "func": process_wosensorth,
  140. "manufacturer_id": 2409,
  141. },
  142. "i": {
  143. "modelName": SwitchbotModel.METER,
  144. "modelFriendlyName": "Meter Plus",
  145. "func": process_wosensorth,
  146. "manufacturer_id": 2409,
  147. },
  148. "I": {
  149. "modelName": SwitchbotModel.METER,
  150. "modelFriendlyName": "Meter Plus",
  151. "func": process_wosensorth,
  152. "manufacturer_id": 2409,
  153. },
  154. "T": {
  155. "modelName": SwitchbotModel.METER,
  156. "modelFriendlyName": "Meter",
  157. "func": process_wosensorth,
  158. "manufacturer_id": 2409,
  159. },
  160. "t": {
  161. "modelName": SwitchbotModel.METER,
  162. "modelFriendlyName": "Meter",
  163. "func": process_wosensorth,
  164. "manufacturer_id": 2409,
  165. },
  166. "4": {
  167. "modelName": SwitchbotModel.METER_PRO,
  168. "modelFriendlyName": "Meter Pro",
  169. "func": process_wosensorth,
  170. "manufacturer_id": 2409,
  171. },
  172. b"\x14": {
  173. "modelName": SwitchbotModel.METER_PRO,
  174. "modelFriendlyName": "Meter Pro",
  175. "func": process_wosensorth,
  176. "manufacturer_id": 2409,
  177. },
  178. "5": {
  179. "modelName": SwitchbotModel.METER_PRO_C,
  180. "modelFriendlyName": "Meter Pro CO2",
  181. "func": process_wosensorth_c,
  182. "manufacturer_id": 2409,
  183. },
  184. b"\x15": {
  185. "modelName": SwitchbotModel.METER_PRO_C,
  186. "modelFriendlyName": "Meter Pro CO2",
  187. "func": process_wosensorth_c,
  188. "manufacturer_id": 2409,
  189. },
  190. "v": {
  191. "modelName": SwitchbotModel.HUB2,
  192. "modelFriendlyName": "Hub 2",
  193. "func": process_wohub2,
  194. "manufacturer_id": 2409,
  195. },
  196. "V": {
  197. "modelName": SwitchbotModel.HUB2,
  198. "modelFriendlyName": "Hub 2",
  199. "func": process_wohub2,
  200. "manufacturer_id": 2409,
  201. },
  202. "g": {
  203. "modelName": SwitchbotModel.PLUG_MINI,
  204. "modelFriendlyName": "Plug Mini",
  205. "func": process_woplugmini,
  206. "manufacturer_id": 2409,
  207. },
  208. "G": {
  209. "modelName": SwitchbotModel.PLUG_MINI,
  210. "modelFriendlyName": "Plug Mini",
  211. "func": process_woplugmini,
  212. "manufacturer_id": 2409,
  213. },
  214. "j": {
  215. "modelName": SwitchbotModel.PLUG_MINI,
  216. "modelFriendlyName": "Plug Mini (JP)",
  217. "func": process_woplugmini,
  218. "manufacturer_id": 2409,
  219. },
  220. "J": {
  221. "modelName": SwitchbotModel.PLUG_MINI,
  222. "modelFriendlyName": "Plug Mini (JP)",
  223. "func": process_woplugmini,
  224. "manufacturer_id": 2409,
  225. },
  226. "u": {
  227. "modelName": SwitchbotModel.COLOR_BULB,
  228. "modelFriendlyName": "Color Bulb",
  229. "func": process_color_bulb,
  230. "manufacturer_id": 2409,
  231. },
  232. "U": {
  233. "modelName": SwitchbotModel.COLOR_BULB,
  234. "modelFriendlyName": "Color Bulb",
  235. "func": process_color_bulb,
  236. "manufacturer_id": 2409,
  237. },
  238. "q": {
  239. "modelName": SwitchbotModel.CEILING_LIGHT,
  240. "modelFriendlyName": "Ceiling Light",
  241. "func": process_woceiling,
  242. "manufacturer_id": 2409,
  243. },
  244. "Q": {
  245. "modelName": SwitchbotModel.CEILING_LIGHT,
  246. "modelFriendlyName": "Ceiling Light",
  247. "func": process_woceiling,
  248. "manufacturer_id": 2409,
  249. },
  250. "n": {
  251. "modelName": SwitchbotModel.CEILING_LIGHT,
  252. "modelFriendlyName": "Ceiling Light Pro",
  253. "func": process_woceiling,
  254. "manufacturer_id": 2409,
  255. },
  256. "N": {
  257. "modelName": SwitchbotModel.CEILING_LIGHT,
  258. "modelFriendlyName": "Ceiling Light Pro",
  259. "func": process_woceiling,
  260. "manufacturer_id": 2409,
  261. },
  262. "e": {
  263. "modelName": SwitchbotModel.HUMIDIFIER,
  264. "modelFriendlyName": "Humidifier",
  265. "func": process_wohumidifier,
  266. "manufacturer_id": 741,
  267. "manufacturer_data_length": 6,
  268. },
  269. "E": {
  270. "modelName": SwitchbotModel.HUMIDIFIER,
  271. "modelFriendlyName": "Humidifier",
  272. "func": process_wohumidifier,
  273. "manufacturer_id": 741,
  274. "manufacturer_data_length": 6,
  275. },
  276. "#": {
  277. "modelName": SwitchbotModel.EVAPORATIVE_HUMIDIFIER,
  278. "modelFriendlyName": "Evaporative Humidifier",
  279. "func": process_evaporative_humidifier,
  280. "manufacturer_id": 2409,
  281. },
  282. b"\x03": {
  283. "modelName": SwitchbotModel.EVAPORATIVE_HUMIDIFIER,
  284. "modelFriendlyName": "Evaporative Humidifier",
  285. "func": process_evaporative_humidifier,
  286. "manufacturer_id": 2409,
  287. },
  288. "o": {
  289. "modelName": SwitchbotModel.LOCK,
  290. "modelFriendlyName": "Lock",
  291. "func": process_wolock,
  292. "manufacturer_id": 2409,
  293. },
  294. "O": {
  295. "modelName": SwitchbotModel.LOCK,
  296. "modelFriendlyName": "Lock",
  297. "func": process_wolock,
  298. "manufacturer_id": 2409,
  299. },
  300. "$": {
  301. "modelName": SwitchbotModel.LOCK_PRO,
  302. "modelFriendlyName": "Lock Pro",
  303. "func": process_wolock_pro,
  304. "manufacturer_id": 2409,
  305. },
  306. b"\x04": {
  307. "modelName": SwitchbotModel.LOCK_PRO,
  308. "modelFriendlyName": "Lock Pro",
  309. "func": process_wolock_pro,
  310. "manufacturer_id": 2409,
  311. },
  312. "x": {
  313. "modelName": SwitchbotModel.BLIND_TILT,
  314. "modelFriendlyName": "Blind Tilt",
  315. "func": process_woblindtilt,
  316. "manufacturer_id": 2409,
  317. },
  318. "X": {
  319. "modelName": SwitchbotModel.BLIND_TILT,
  320. "modelFriendlyName": "Blind Tilt",
  321. "func": process_woblindtilt,
  322. "manufacturer_id": 2409,
  323. },
  324. "&": {
  325. "modelName": SwitchbotModel.LEAK,
  326. "modelFriendlyName": "Leak Detector",
  327. "func": process_leak,
  328. "manufacturer_id": 2409,
  329. },
  330. b"\x06": {
  331. "modelName": SwitchbotModel.LEAK,
  332. "modelFriendlyName": "Leak Detector",
  333. "func": process_leak,
  334. "manufacturer_id": 2409,
  335. },
  336. "y": {
  337. "modelName": SwitchbotModel.KEYPAD,
  338. "modelFriendlyName": "Keypad",
  339. "func": process_wokeypad,
  340. "manufacturer_id": 2409,
  341. },
  342. "Y": {
  343. "modelName": SwitchbotModel.KEYPAD,
  344. "modelFriendlyName": "Keypad",
  345. "func": process_wokeypad,
  346. "manufacturer_id": 2409,
  347. },
  348. "<": {
  349. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  350. "modelFriendlyName": "Relay Switch 1PM",
  351. "func": process_relay_switch_1pm,
  352. "manufacturer_id": 2409,
  353. },
  354. b"\x1c": {
  355. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  356. "modelFriendlyName": "Relay Switch 1PM",
  357. "func": process_relay_switch_1pm,
  358. "manufacturer_id": 2409,
  359. },
  360. ";": {
  361. "modelName": SwitchbotModel.RELAY_SWITCH_1,
  362. "modelFriendlyName": "Relay Switch 1",
  363. "func": process_relay_switch_common_data,
  364. "manufacturer_id": 2409,
  365. },
  366. b"\x1b": {
  367. "modelName": SwitchbotModel.RELAY_SWITCH_1,
  368. "modelFriendlyName": "Relay Switch 1",
  369. "func": process_relay_switch_common_data,
  370. "manufacturer_id": 2409,
  371. },
  372. "b": {
  373. "modelName": SwitchbotModel.REMOTE,
  374. "modelFriendlyName": "Remote",
  375. "func": process_woremote,
  376. "manufacturer_id": 89,
  377. },
  378. "B": {
  379. "modelName": SwitchbotModel.REMOTE,
  380. "modelFriendlyName": "Remote",
  381. "func": process_woremote,
  382. "manufacturer_id": 89,
  383. },
  384. ",": {
  385. "modelName": SwitchbotModel.ROLLER_SHADE,
  386. "modelFriendlyName": "Roller Shade",
  387. "func": process_worollershade,
  388. "manufacturer_id": 2409,
  389. },
  390. b"\x0c": {
  391. "modelName": SwitchbotModel.ROLLER_SHADE,
  392. "modelFriendlyName": "Roller Shade",
  393. "func": process_worollershade,
  394. "manufacturer_id": 2409,
  395. },
  396. "%": {
  397. "modelName": SwitchbotModel.HUBMINI_MATTER,
  398. "modelFriendlyName": "HubMini Matter",
  399. "func": process_hubmini_matter,
  400. "manufacturer_id": 2409,
  401. },
  402. b"\x05": {
  403. "modelName": SwitchbotModel.HUBMINI_MATTER,
  404. "modelFriendlyName": "HubMini Matter",
  405. "func": process_hubmini_matter,
  406. "manufacturer_id": 2409,
  407. },
  408. "~": {
  409. "modelName": SwitchbotModel.CIRCULATOR_FAN,
  410. "modelFriendlyName": "Circulator Fan",
  411. "func": process_fan,
  412. "manufacturer_id": 2409,
  413. },
  414. "^": {
  415. "modelName": SwitchbotModel.CIRCULATOR_FAN,
  416. "modelFriendlyName": "Circulator Fan",
  417. "func": process_fan,
  418. "manufacturer_id": 2409,
  419. },
  420. ".": {
  421. "modelName": SwitchbotModel.K20_VACUUM,
  422. "modelFriendlyName": "K20 Vacuum",
  423. "func": process_vacuum,
  424. "manufacturer_id": 2409,
  425. },
  426. b"\x0f": {
  427. "modelName": SwitchbotModel.K20_VACUUM,
  428. "modelFriendlyName": "K20 Vacuum",
  429. "func": process_vacuum,
  430. "manufacturer_id": 2409,
  431. },
  432. "z": {
  433. "modelName": SwitchbotModel.S10_VACUUM,
  434. "modelFriendlyName": "S10 Vacuum",
  435. "func": process_vacuum,
  436. "manufacturer_id": 2409,
  437. },
  438. "Z": {
  439. "modelName": SwitchbotModel.S10_VACUUM,
  440. "modelFriendlyName": "S10 Vacuum",
  441. "func": process_vacuum,
  442. "manufacturer_id": 2409,
  443. },
  444. "3": {
  445. "modelName": SwitchbotModel.K10_PRO_COMBO_VACUUM,
  446. "modelFriendlyName": "K10+ Pro Combo Vacuum",
  447. "func": process_vacuum,
  448. "manufacturer_id": 2409,
  449. },
  450. b"\x13": {
  451. "modelName": SwitchbotModel.K10_PRO_COMBO_VACUUM,
  452. "modelFriendlyName": "K10+ Pro Combo Vacuum",
  453. "func": process_vacuum,
  454. "manufacturer_id": 2409,
  455. },
  456. "}": {
  457. "modelName": SwitchbotModel.K10_VACUUM,
  458. "modelFriendlyName": "K10+ Vacuum",
  459. "func": process_vacuum_k,
  460. "manufacturer_id": 2409,
  461. },
  462. "]": {
  463. "modelName": SwitchbotModel.K10_VACUUM,
  464. "modelFriendlyName": "K10+ Vacuum",
  465. "func": process_vacuum_k,
  466. "manufacturer_id": 2409,
  467. },
  468. "(": {
  469. "modelName": SwitchbotModel.K10_PRO_VACUUM,
  470. "modelFriendlyName": "K10+ Pro Vacuum",
  471. "func": process_vacuum_k,
  472. "manufacturer_id": 2409,
  473. },
  474. b"\x08": {
  475. "modelName": SwitchbotModel.K10_PRO_VACUUM,
  476. "modelFriendlyName": "K10+ Pro Vacuum",
  477. "func": process_vacuum_k,
  478. "manufacturer_id": 2409,
  479. },
  480. "*": {
  481. "modelName": SwitchbotModel.AIR_PURIFIER,
  482. "modelFriendlyName": "Air Purifier",
  483. "func": process_air_purifier,
  484. "manufacturer_id": 2409,
  485. },
  486. b"\x0a": {
  487. "modelName": SwitchbotModel.AIR_PURIFIER,
  488. "modelFriendlyName": "Air Purifier",
  489. "func": process_air_purifier,
  490. "manufacturer_id": 2409,
  491. },
  492. "+": {
  493. "modelName": SwitchbotModel.AIR_PURIFIER,
  494. "modelFriendlyName": "Air Purifier",
  495. "func": process_air_purifier,
  496. "manufacturer_id": 2409,
  497. },
  498. b"\x0b": {
  499. "modelName": SwitchbotModel.AIR_PURIFIER,
  500. "modelFriendlyName": "Air Purifier",
  501. "func": process_air_purifier,
  502. "manufacturer_id": 2409,
  503. },
  504. "7": {
  505. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE,
  506. "modelFriendlyName": "Air Purifier Table",
  507. "func": process_air_purifier,
  508. "manufacturer_id": 2409,
  509. },
  510. b"\x17": {
  511. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE,
  512. "modelFriendlyName": "Air Purifier Table",
  513. "func": process_air_purifier,
  514. "manufacturer_id": 2409,
  515. },
  516. "8": {
  517. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE,
  518. "modelFriendlyName": "Air Purifier Table",
  519. "func": process_air_purifier,
  520. "manufacturer_id": 2409,
  521. },
  522. b"\x18": {
  523. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE,
  524. "modelFriendlyName": "Air Purifier Table",
  525. "func": process_air_purifier,
  526. "manufacturer_id": 2409,
  527. },
  528. b"\x00\x10\xb9\x40": {
  529. "modelName": SwitchbotModel.HUB3,
  530. "modelFriendlyName": "Hub3",
  531. "func": process_hub3,
  532. "manufacturer_id": 2409,
  533. },
  534. b"\x01\x10\xb9\x40": {
  535. "modelName": SwitchbotModel.HUB3,
  536. "modelFriendlyName": "Hub3",
  537. "func": process_hub3,
  538. "manufacturer_id": 2409,
  539. },
  540. "-": {
  541. "modelName": SwitchbotModel.LOCK_LITE,
  542. "modelFriendlyName": "Lock Lite",
  543. "func": process_locklite,
  544. "manufacturer_id": 2409,
  545. },
  546. b"\x0d": {
  547. "modelName": SwitchbotModel.LOCK_LITE,
  548. "modelFriendlyName": "Lock Lite",
  549. "func": process_locklite,
  550. "manufacturer_id": 2409,
  551. },
  552. b"\x00\x10\xa5\xb8": {
  553. "modelName": SwitchbotModel.LOCK_ULTRA,
  554. "modelFriendlyName": "Lock Ultra",
  555. "func": process_lock2,
  556. "manufacturer_id": 2409,
  557. },
  558. b"\x01\x10\xa5\xb8": {
  559. "modelName": SwitchbotModel.LOCK_ULTRA,
  560. "modelFriendlyName": "Lock Ultra",
  561. "func": process_lock2,
  562. "manufacturer_id": 2409,
  563. },
  564. ">": {
  565. "modelName": SwitchbotModel.GARAGE_DOOR_OPENER,
  566. "modelFriendlyName": "Garage Door Opener",
  567. "func": process_garage_door_opener,
  568. "manufacturer_id": 2409,
  569. },
  570. b"\x1e": {
  571. "modelName": SwitchbotModel.GARAGE_DOOR_OPENER,
  572. "modelFriendlyName": "Garage Door Opener",
  573. "func": process_garage_door_opener,
  574. "manufacturer_id": 2409,
  575. },
  576. "=": {
  577. "modelName": SwitchbotModel.RELAY_SWITCH_2PM,
  578. "modelFriendlyName": "Relay Switch 2PM",
  579. "func": process_relay_switch_2pm,
  580. "manufacturer_id": 2409,
  581. },
  582. b"\x1d": {
  583. "modelName": SwitchbotModel.RELAY_SWITCH_2PM,
  584. "modelFriendlyName": "Relay Switch 2PM",
  585. "func": process_relay_switch_2pm,
  586. "manufacturer_id": 2409,
  587. },
  588. b"\x00\x10\xd0\xb0": {
  589. "modelName": SwitchbotModel.FLOOR_LAMP,
  590. "modelFriendlyName": "Floor Lamp",
  591. "func": process_light,
  592. "manufacturer_id": 2409,
  593. },
  594. b"\x01\x10\xd0\xb0": {
  595. "modelName": SwitchbotModel.FLOOR_LAMP,
  596. "modelFriendlyName": "Floor Lamp",
  597. "func": process_light,
  598. "manufacturer_id": 2409,
  599. },
  600. b"\x00\x10\xd0\xb1": {
  601. "modelName": SwitchbotModel.STRIP_LIGHT_3,
  602. "modelFriendlyName": "Strip Light 3",
  603. "func": process_light,
  604. "manufacturer_id": 2409,
  605. },
  606. b"\x01\x10\xd0\xb1": {
  607. "modelName": SwitchbotModel.STRIP_LIGHT_3,
  608. "modelFriendlyName": "Strip Light 3",
  609. "func": process_light,
  610. "manufacturer_id": 2409,
  611. },
  612. "?": {
  613. "modelName": SwitchbotModel.PLUG_MINI_EU,
  614. "modelFriendlyName": "Plug Mini (EU)",
  615. "func": process_relay_switch_1pm,
  616. "manufacturer_id": 2409,
  617. },
  618. b"\x1f": {
  619. "modelName": SwitchbotModel.PLUG_MINI_EU,
  620. "modelFriendlyName": "Plug Mini (EU)",
  621. "func": process_relay_switch_1pm,
  622. "manufacturer_id": 2409,
  623. },
  624. b"\x00\x10\xd0\xb3": {
  625. "modelName": SwitchbotModel.RGBICWW_STRIP_LIGHT,
  626. "modelFriendlyName": "RGBICWW Strip Light",
  627. "func": process_rgbic_light,
  628. "manufacturer_id": 2409,
  629. },
  630. b"\x01\x10\xd0\xb3": {
  631. "modelName": SwitchbotModel.RGBICWW_STRIP_LIGHT,
  632. "modelFriendlyName": "RGBICWW Strip Light",
  633. "func": process_rgbic_light,
  634. "manufacturer_id": 2409,
  635. },
  636. b"\x00\x10\xd0\xb4": {
  637. "modelName": SwitchbotModel.RGBICWW_FLOOR_LAMP,
  638. "modelFriendlyName": "RGBICWW Floor Lamp",
  639. "func": process_rgbic_light,
  640. "manufacturer_id": 2409,
  641. },
  642. b"\x01\x10\xd0\xb4": {
  643. "modelName": SwitchbotModel.RGBICWW_FLOOR_LAMP,
  644. "modelFriendlyName": "RGBICWW Floor Lamp",
  645. "func": process_rgbic_light,
  646. "manufacturer_id": 2409,
  647. },
  648. b"\x00\x10\xfb\xa8": {
  649. "modelName": SwitchbotModel.K11_VACUUM,
  650. "modelFriendlyName": "K11+ Vacuum",
  651. "func": process_vacuum,
  652. "manufacturer_id": 2409,
  653. },
  654. b"\x01\x10\xfb\xa8": {
  655. "modelName": SwitchbotModel.K11_VACUUM,
  656. "modelFriendlyName": "K11+ Vacuum",
  657. "func": process_vacuum,
  658. "manufacturer_id": 2409,
  659. },
  660. b"\x00\x10\xf3\xd8": {
  661. "modelName": SwitchbotModel.CLIMATE_PANEL,
  662. "modelFriendlyName": "Climate Panel",
  663. "func": process_climate_panel,
  664. "manufacturer_id": 2409,
  665. },
  666. b"\x01\x10\xf3\xd8": {
  667. "modelName": SwitchbotModel.CLIMATE_PANEL,
  668. "modelFriendlyName": "Climate Panel",
  669. "func": process_climate_panel,
  670. "manufacturer_id": 2409,
  671. },
  672. b"\x00\x116@": {
  673. "modelName": SwitchbotModel.SMART_THERMOSTAT_RADIATOR,
  674. "modelFriendlyName": "Smart Thermostat Radiator",
  675. "func": process_smart_thermostat_radiator,
  676. "manufacturer_id": 2409,
  677. },
  678. b"\x01\x116@": {
  679. "modelName": SwitchbotModel.SMART_THERMOSTAT_RADIATOR,
  680. "modelFriendlyName": "Smart Thermostat Radiator",
  681. "func": process_smart_thermostat_radiator,
  682. "manufacturer_id": 2409,
  683. },
  684. b"\x00\x10\xe0P": {
  685. "modelName": SwitchbotModel.S20_VACUUM,
  686. "modelFriendlyName": "S20 Vacuum",
  687. "func": process_vacuum,
  688. "manufacturer_id": 2409,
  689. },
  690. b"\x01\x10\xe0P": {
  691. "modelName": SwitchbotModel.S20_VACUUM,
  692. "modelFriendlyName": "S20 Vacuum",
  693. "func": process_vacuum,
  694. "manufacturer_id": 2409,
  695. },
  696. b"\x00\x10\xcc\xc8": {
  697. "modelName": SwitchbotModel.PRESENCE_SENSOR,
  698. "modelFriendlyName": "Presence Sensor",
  699. "func": process_presence_sensor,
  700. "manufacturer_id": 2409,
  701. },
  702. b"\x01\x10\xcc\xc8": {
  703. "modelName": SwitchbotModel.PRESENCE_SENSOR,
  704. "modelFriendlyName": "Presence Sensor",
  705. "func": process_presence_sensor,
  706. "manufacturer_id": 2409,
  707. },
  708. }
  709. _SWITCHBOT_MODEL_TO_CHAR: defaultdict[SwitchbotModel, list[str | bytes]] = defaultdict(
  710. list
  711. )
  712. for model_chr, model_data in SUPPORTED_TYPES.items():
  713. _SWITCHBOT_MODEL_TO_CHAR[model_data["modelName"]].append(model_chr)
  714. MODELS_BY_MANUFACTURER_DATA: dict[int, list[tuple[str, SwitchbotSupportedType]]] = {
  715. mfr_id: [] for mfr_id in MFR_DATA_ORDER
  716. }
  717. for model_chr, model in SUPPORTED_TYPES.items():
  718. if "manufacturer_id" in model:
  719. mfr_id = model["manufacturer_id"]
  720. MODELS_BY_MANUFACTURER_DATA[mfr_id].append((model_chr, model))
  721. def parse_advertisement_data(
  722. device: BLEDevice,
  723. advertisement_data: AdvertisementData,
  724. model: SwitchbotModel | None = None,
  725. ) -> SwitchBotAdvertisement | None:
  726. """Parse advertisement data."""
  727. upper_mac = format_mac_upper(device.address)
  728. if model is None and upper_mac in _MODEL_TO_MAC_CACHE:
  729. model = _MODEL_TO_MAC_CACHE[upper_mac]
  730. service_data = advertisement_data.service_data
  731. _service_data = None
  732. for uuid in SERVICE_DATA_ORDER:
  733. if uuid in service_data:
  734. _service_data = service_data[uuid]
  735. break
  736. _mfr_data = None
  737. _mfr_id = None
  738. for mfr_id in MFR_DATA_ORDER:
  739. if mfr_id in advertisement_data.manufacturer_data:
  740. _mfr_id = mfr_id
  741. _mfr_data = advertisement_data.manufacturer_data[mfr_id]
  742. break
  743. if _mfr_data is None and _service_data is None:
  744. return None
  745. try:
  746. data = _parse_data(
  747. _service_data,
  748. _mfr_data,
  749. _mfr_id,
  750. model,
  751. )
  752. except Exception: # pylint: disable=broad-except
  753. _LOGGER.exception("Failed to parse advertisement data: %s", advertisement_data)
  754. return None
  755. if not data:
  756. return None
  757. return SwitchBotAdvertisement(
  758. device.address, data, device, advertisement_data.rssi, bool(_service_data)
  759. )
  760. def _find_model_from_service_data(_service_data: bytes) -> str | bytes | None:
  761. """Find model from service data."""
  762. char_model = chr(_service_data[0] & 0b01111111)
  763. if char_model in SUPPORTED_TYPES:
  764. return char_model
  765. byte_model = bytes([_service_data[0] & 0b01111111])
  766. if byte_model in SUPPORTED_TYPES:
  767. return byte_model
  768. return None
  769. def _find_model_from_switchbot_model(
  770. _switchbot_model: SwitchbotModel,
  771. ) -> str | bytes | None:
  772. """Find model from switchbot model."""
  773. if _switchbot_model in _SWITCHBOT_MODEL_TO_CHAR:
  774. return _SWITCHBOT_MODEL_TO_CHAR[_switchbot_model][0]
  775. return None
  776. def _find_model_from_manufacturer_data(
  777. _mfr_id: int, _mfr_data: bytes | None
  778. ) -> str | bytes | None:
  779. """Find model from manufacturer data."""
  780. if _mfr_id not in MODELS_BY_MANUFACTURER_DATA or _mfr_data is None:
  781. return None
  782. for model_chr, model_data in MODELS_BY_MANUFACTURER_DATA[_mfr_id]:
  783. expected_length = model_data.get("manufacturer_data_length")
  784. if expected_length is not None and expected_length == len(_mfr_data):
  785. return model_chr
  786. return None
  787. def _find_model_from_service_data_suffix(_service_data: bytes) -> str | bytes | None:
  788. """Find model from service data suffix."""
  789. if len(_service_data) <= 5:
  790. return None
  791. for s in (_service_data[-4:], _service_data[-5:-1]):
  792. if s in SUPPORTED_TYPES:
  793. return s
  794. return None
  795. def build_advertisement_data(
  796. _model: str | bytes, _service_data: bytes | None, _mfr_data: bytes | None
  797. ) -> dict[str, Any]:
  798. """Build advertisement data dictionary."""
  799. _isEncrypted = bool(_service_data[0] & 0b10000000) if _service_data else False
  800. data = {
  801. "rawAdvData": _service_data,
  802. "data": {},
  803. "model": _model,
  804. "isEncrypted": _isEncrypted,
  805. }
  806. type_data = SUPPORTED_TYPES.get(_model)
  807. if type_data:
  808. model_data = type_data["func"](_service_data, _mfr_data)
  809. if model_data:
  810. data.update(
  811. {
  812. "modelFriendlyName": type_data["modelFriendlyName"],
  813. "modelName": type_data["modelName"],
  814. "data": model_data,
  815. }
  816. )
  817. return data
  818. @lru_cache(maxsize=128)
  819. def _parse_data(
  820. _service_data: bytes | None,
  821. _mfr_data: bytes | None,
  822. _mfr_id: int | None = None,
  823. _switchbot_model: SwitchbotModel | None = None,
  824. ) -> dict[str, Any] | None:
  825. """Parse advertisement data."""
  826. _model = None
  827. if _service_data:
  828. _model = _find_model_from_service_data(_service_data)
  829. if not _model and _switchbot_model:
  830. _model = _find_model_from_switchbot_model(_switchbot_model)
  831. if not _model and _mfr_id:
  832. _model = _find_model_from_manufacturer_data(_mfr_id, _mfr_data)
  833. if not _model and _service_data:
  834. _model = _find_model_from_service_data_suffix(_service_data)
  835. if not _model:
  836. return None
  837. return build_advertisement_data(_model, _service_data, _mfr_data)
  838. def populate_model_to_mac_cache(mac: str, model: SwitchbotModel) -> None:
  839. """Populate the model to MAC address cache."""
  840. _MODEL_TO_MAC_CACHE[mac] = model