adv_parser.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. import logging
  4. from collections.abc import Callable
  5. from functools import lru_cache
  6. from typing import Any, TypedDict
  7. from bleak.backends.device import BLEDevice
  8. from bleak.backends.scanner import AdvertisementData
  9. from .adv_parsers.air_purifier import process_air_purifier
  10. from .adv_parsers.blind_tilt import process_woblindtilt
  11. from .adv_parsers.bot import process_wohand
  12. from .adv_parsers.bulb import process_color_bulb
  13. from .adv_parsers.ceiling_light import process_woceiling
  14. from .adv_parsers.climate_panel import process_climate_panel
  15. from .adv_parsers.contact import process_wocontact
  16. from .adv_parsers.curtain import process_wocurtain
  17. from .adv_parsers.fan import process_fan
  18. from .adv_parsers.hub2 import process_wohub2
  19. from .adv_parsers.hub3 import process_hub3
  20. from .adv_parsers.hubmini_matter import process_hubmini_matter
  21. from .adv_parsers.humidifier import process_evaporative_humidifier, process_wohumidifier
  22. from .adv_parsers.keypad import process_wokeypad
  23. from .adv_parsers.leak import process_leak
  24. from .adv_parsers.light_strip import process_light, process_rgbic_light, process_wostrip
  25. from .adv_parsers.lock import (
  26. process_lock2,
  27. process_locklite,
  28. process_wolock,
  29. process_wolock_pro,
  30. )
  31. from .adv_parsers.meter import process_wosensorth, process_wosensorth_c
  32. from .adv_parsers.motion import process_wopresence
  33. from .adv_parsers.plug import process_woplugmini
  34. from .adv_parsers.presence_sensor import process_presence_sensor
  35. from .adv_parsers.relay_switch import (
  36. process_garage_door_opener,
  37. process_relay_switch_1pm,
  38. process_relay_switch_2pm,
  39. process_relay_switch_common_data,
  40. )
  41. from .adv_parsers.remote import process_woremote
  42. from .adv_parsers.roller_shade import process_worollershade
  43. from .adv_parsers.smart_thermostat_radiator import process_smart_thermostat_radiator
  44. from .adv_parsers.vacuum import process_vacuum, process_vacuum_k
  45. from .const import SwitchbotModel
  46. from .models import SwitchBotAdvertisement
  47. from .utils import format_mac_upper
  48. _LOGGER = logging.getLogger(__name__)
  49. SERVICE_DATA_ORDER = (
  50. "0000fd3d-0000-1000-8000-00805f9b34fb",
  51. "00000d00-0000-1000-8000-00805f9b34fb",
  52. )
  53. MFR_DATA_ORDER = (2409, 741, 89)
  54. _MODEL_TO_MAC_CACHE: dict[str, SwitchbotModel] = {}
  55. class SwitchbotSupportedType(TypedDict):
  56. """Supported type of Switchbot."""
  57. modelName: SwitchbotModel
  58. modelFriendlyName: str
  59. func: Callable[[bytes, bytes | None], dict[str, bool | int]]
  60. manufacturer_id: int | None
  61. manufacturer_data_length: int | None
  62. SUPPORTED_TYPES: dict[str | bytes, SwitchbotSupportedType] = {
  63. "d": {
  64. "modelName": SwitchbotModel.CONTACT_SENSOR,
  65. "modelFriendlyName": "Contact Sensor",
  66. "func": process_wocontact,
  67. "manufacturer_id": 2409,
  68. },
  69. "H": {
  70. "modelName": SwitchbotModel.BOT,
  71. "modelFriendlyName": "Bot",
  72. "func": process_wohand,
  73. "manufacturer_id": 89,
  74. },
  75. "s": {
  76. "modelName": SwitchbotModel.MOTION_SENSOR,
  77. "modelFriendlyName": "Motion Sensor",
  78. "func": process_wopresence,
  79. "manufacturer_id": 2409,
  80. },
  81. "r": {
  82. "modelName": SwitchbotModel.LIGHT_STRIP,
  83. "modelFriendlyName": "Light Strip",
  84. "func": process_wostrip,
  85. "manufacturer_id": 2409,
  86. },
  87. "{": {
  88. "modelName": SwitchbotModel.CURTAIN,
  89. "modelFriendlyName": "Curtain 3",
  90. "func": process_wocurtain,
  91. "manufacturer_id": 2409,
  92. },
  93. "c": {
  94. "modelName": SwitchbotModel.CURTAIN,
  95. "modelFriendlyName": "Curtain",
  96. "func": process_wocurtain,
  97. "manufacturer_id": 2409,
  98. },
  99. "w": {
  100. "modelName": SwitchbotModel.IO_METER,
  101. "modelFriendlyName": "Indoor/Outdoor Meter",
  102. "func": process_wosensorth,
  103. "manufacturer_id": 2409,
  104. },
  105. "i": {
  106. "modelName": SwitchbotModel.METER,
  107. "modelFriendlyName": "Meter Plus",
  108. "func": process_wosensorth,
  109. "manufacturer_id": 2409,
  110. },
  111. "T": {
  112. "modelName": SwitchbotModel.METER,
  113. "modelFriendlyName": "Meter",
  114. "func": process_wosensorth,
  115. "manufacturer_id": 2409,
  116. },
  117. "4": {
  118. "modelName": SwitchbotModel.METER_PRO,
  119. "modelFriendlyName": "Meter Pro",
  120. "func": process_wosensorth,
  121. "manufacturer_id": 2409,
  122. },
  123. "5": {
  124. "modelName": SwitchbotModel.METER_PRO_C,
  125. "modelFriendlyName": "Meter Pro CO2",
  126. "func": process_wosensorth_c,
  127. "manufacturer_id": 2409,
  128. },
  129. "v": {
  130. "modelName": SwitchbotModel.HUB2,
  131. "modelFriendlyName": "Hub 2",
  132. "func": process_wohub2,
  133. "manufacturer_id": 2409,
  134. },
  135. "g": {
  136. "modelName": SwitchbotModel.PLUG_MINI,
  137. "modelFriendlyName": "Plug Mini",
  138. "func": process_woplugmini,
  139. "manufacturer_id": 2409,
  140. },
  141. "j": {
  142. "modelName": SwitchbotModel.PLUG_MINI,
  143. "modelFriendlyName": "Plug Mini (JP)",
  144. "func": process_woplugmini,
  145. "manufacturer_id": 2409,
  146. },
  147. "u": {
  148. "modelName": SwitchbotModel.COLOR_BULB,
  149. "modelFriendlyName": "Color Bulb",
  150. "func": process_color_bulb,
  151. "manufacturer_id": 2409,
  152. },
  153. "q": {
  154. "modelName": SwitchbotModel.CEILING_LIGHT,
  155. "modelFriendlyName": "Ceiling Light",
  156. "func": process_woceiling,
  157. "manufacturer_id": 2409,
  158. },
  159. "n": {
  160. "modelName": SwitchbotModel.CEILING_LIGHT,
  161. "modelFriendlyName": "Ceiling Light Pro",
  162. "func": process_woceiling,
  163. "manufacturer_id": 2409,
  164. },
  165. "e": {
  166. "modelName": SwitchbotModel.HUMIDIFIER,
  167. "modelFriendlyName": "Humidifier",
  168. "func": process_wohumidifier,
  169. "manufacturer_id": 741,
  170. "manufacturer_data_length": 6,
  171. },
  172. "#": {
  173. "modelName": SwitchbotModel.EVAPORATIVE_HUMIDIFIER,
  174. "modelFriendlyName": "Evaporative Humidifier",
  175. "func": process_evaporative_humidifier,
  176. "manufacturer_id": 2409,
  177. },
  178. "o": {
  179. "modelName": SwitchbotModel.LOCK,
  180. "modelFriendlyName": "Lock",
  181. "func": process_wolock,
  182. "manufacturer_id": 2409,
  183. },
  184. "$": {
  185. "modelName": SwitchbotModel.LOCK_PRO,
  186. "modelFriendlyName": "Lock Pro",
  187. "func": process_wolock_pro,
  188. "manufacturer_id": 2409,
  189. },
  190. "x": {
  191. "modelName": SwitchbotModel.BLIND_TILT,
  192. "modelFriendlyName": "Blind Tilt",
  193. "func": process_woblindtilt,
  194. "manufacturer_id": 2409,
  195. },
  196. "&": {
  197. "modelName": SwitchbotModel.LEAK,
  198. "modelFriendlyName": "Leak Detector",
  199. "func": process_leak,
  200. "manufacturer_id": 2409,
  201. },
  202. "y": {
  203. "modelName": SwitchbotModel.KEYPAD,
  204. "modelFriendlyName": "Keypad",
  205. "func": process_wokeypad,
  206. "manufacturer_id": 2409,
  207. },
  208. "<": {
  209. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  210. "modelFriendlyName": "Relay Switch 1PM",
  211. "func": process_relay_switch_1pm,
  212. "manufacturer_id": 2409,
  213. },
  214. ";": {
  215. "modelName": SwitchbotModel.RELAY_SWITCH_1,
  216. "modelFriendlyName": "Relay Switch 1",
  217. "func": process_relay_switch_common_data,
  218. "manufacturer_id": 2409,
  219. },
  220. "b": {
  221. "modelName": SwitchbotModel.REMOTE,
  222. "modelFriendlyName": "Remote",
  223. "func": process_woremote,
  224. "manufacturer_id": 89,
  225. },
  226. ",": {
  227. "modelName": SwitchbotModel.ROLLER_SHADE,
  228. "modelFriendlyName": "Roller Shade",
  229. "func": process_worollershade,
  230. "manufacturer_id": 2409,
  231. },
  232. "%": {
  233. "modelName": SwitchbotModel.HUBMINI_MATTER,
  234. "modelFriendlyName": "HubMini Matter",
  235. "func": process_hubmini_matter,
  236. "manufacturer_id": 2409,
  237. },
  238. "~": {
  239. "modelName": SwitchbotModel.CIRCULATOR_FAN,
  240. "modelFriendlyName": "Circulator Fan",
  241. "func": process_fan,
  242. "manufacturer_id": 2409,
  243. },
  244. ".": {
  245. "modelName": SwitchbotModel.K20_VACUUM,
  246. "modelFriendlyName": "K20 Vacuum",
  247. "func": process_vacuum,
  248. "manufacturer_id": 2409,
  249. },
  250. "z": {
  251. "modelName": SwitchbotModel.S10_VACUUM,
  252. "modelFriendlyName": "S10 Vacuum",
  253. "func": process_vacuum,
  254. "manufacturer_id": 2409,
  255. },
  256. "3": {
  257. "modelName": SwitchbotModel.K10_PRO_COMBO_VACUUM,
  258. "modelFriendlyName": "K10+ Pro Combo Vacuum",
  259. "func": process_vacuum,
  260. "manufacturer_id": 2409,
  261. },
  262. "}": {
  263. "modelName": SwitchbotModel.K10_VACUUM,
  264. "modelFriendlyName": "K10+ Vacuum",
  265. "func": process_vacuum_k,
  266. "manufacturer_id": 2409,
  267. },
  268. "(": {
  269. "modelName": SwitchbotModel.K10_PRO_VACUUM,
  270. "modelFriendlyName": "K10+ Pro Vacuum",
  271. "func": process_vacuum_k,
  272. "manufacturer_id": 2409,
  273. },
  274. "*": {
  275. "modelName": SwitchbotModel.AIR_PURIFIER,
  276. "modelFriendlyName": "Air Purifier",
  277. "func": process_air_purifier,
  278. "manufacturer_id": 2409,
  279. },
  280. "+": {
  281. "modelName": SwitchbotModel.AIR_PURIFIER,
  282. "modelFriendlyName": "Air Purifier",
  283. "func": process_air_purifier,
  284. "manufacturer_id": 2409,
  285. },
  286. "7": {
  287. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE,
  288. "modelFriendlyName": "Air Purifier Table",
  289. "func": process_air_purifier,
  290. "manufacturer_id": 2409,
  291. },
  292. "8": {
  293. "modelName": SwitchbotModel.AIR_PURIFIER_TABLE,
  294. "modelFriendlyName": "Air Purifier Table",
  295. "func": process_air_purifier,
  296. "manufacturer_id": 2409,
  297. },
  298. b"\x00\x10\xb9\x40": {
  299. "modelName": SwitchbotModel.HUB3,
  300. "modelFriendlyName": "Hub3",
  301. "func": process_hub3,
  302. "manufacturer_id": 2409,
  303. },
  304. "-": {
  305. "modelName": SwitchbotModel.LOCK_LITE,
  306. "modelFriendlyName": "Lock Lite",
  307. "func": process_locklite,
  308. "manufacturer_id": 2409,
  309. },
  310. b"\x00\x10\xa5\xb8": {
  311. "modelName": SwitchbotModel.LOCK_ULTRA,
  312. "modelFriendlyName": "Lock Ultra",
  313. "func": process_lock2,
  314. "manufacturer_id": 2409,
  315. },
  316. ">": {
  317. "modelName": SwitchbotModel.GARAGE_DOOR_OPENER,
  318. "modelFriendlyName": "Garage Door Opener",
  319. "func": process_garage_door_opener,
  320. "manufacturer_id": 2409,
  321. },
  322. "=": {
  323. "modelName": SwitchbotModel.RELAY_SWITCH_2PM,
  324. "modelFriendlyName": "Relay Switch 2PM",
  325. "func": process_relay_switch_2pm,
  326. "manufacturer_id": 2409,
  327. },
  328. b"\x00\x10\xd0\xb0": {
  329. "modelName": SwitchbotModel.FLOOR_LAMP,
  330. "modelFriendlyName": "Floor Lamp",
  331. "func": process_light,
  332. "manufacturer_id": 2409,
  333. },
  334. b"\x00\x10\xd0\xb1": {
  335. "modelName": SwitchbotModel.STRIP_LIGHT_3,
  336. "modelFriendlyName": "Strip Light 3",
  337. "func": process_light,
  338. "manufacturer_id": 2409,
  339. },
  340. "?": {
  341. "modelName": SwitchbotModel.PLUG_MINI_EU,
  342. "modelFriendlyName": "Plug Mini (EU)",
  343. "func": process_relay_switch_1pm,
  344. "manufacturer_id": 2409,
  345. },
  346. b"\x00\x10\xd0\xb3": {
  347. "modelName": SwitchbotModel.RGBICWW_STRIP_LIGHT,
  348. "modelFriendlyName": "RGBICWW Strip Light",
  349. "func": process_rgbic_light,
  350. "manufacturer_id": 2409,
  351. },
  352. b"\x00\x10\xd0\xb4": {
  353. "modelName": SwitchbotModel.RGBICWW_FLOOR_LAMP,
  354. "modelFriendlyName": "RGBICWW Floor Lamp",
  355. "func": process_rgbic_light,
  356. "manufacturer_id": 2409,
  357. },
  358. b"\x00\x10\xfb\xa8": {
  359. "modelName": SwitchbotModel.K11_VACUUM,
  360. "modelFriendlyName": "K11+ Vacuum",
  361. "func": process_vacuum,
  362. "manufacturer_id": 2409,
  363. },
  364. b"\x00\x10\xf3\xd8": {
  365. "modelName": SwitchbotModel.CLIMATE_PANEL,
  366. "modelFriendlyName": "Climate Panel",
  367. "func": process_climate_panel,
  368. "manufacturer_id": 2409,
  369. },
  370. b"\x00\x116@": {
  371. "modelName": SwitchbotModel.SMART_THERMOSTAT_RADIATOR,
  372. "modelFriendlyName": "Smart Thermostat Radiator",
  373. "func": process_smart_thermostat_radiator,
  374. "manufacturer_id": 2409,
  375. },
  376. b"\x00\x10\xe0P": {
  377. "modelName": SwitchbotModel.S20_VACUUM,
  378. "modelFriendlyName": "S20 Vacuum",
  379. "func": process_vacuum,
  380. "manufacturer_id": 2409,
  381. },
  382. b"\x00\x10\xcc\xc8": {
  383. "modelName": SwitchbotModel.PRESENCE_SENSOR,
  384. "modelFriendlyName": "Presence Sensor",
  385. "func": process_presence_sensor,
  386. "manufacturer_id": 2409,
  387. },
  388. }
  389. _SWITCHBOT_MODEL_TO_CHAR = {
  390. model_data["modelName"]: model_chr
  391. for model_chr, model_data in SUPPORTED_TYPES.items()
  392. }
  393. MODELS_BY_MANUFACTURER_DATA: dict[int, list[tuple[str, SwitchbotSupportedType]]] = {
  394. mfr_id: [] for mfr_id in MFR_DATA_ORDER
  395. }
  396. for model_chr, model in SUPPORTED_TYPES.items():
  397. if "manufacturer_id" in model:
  398. mfr_id = model["manufacturer_id"]
  399. MODELS_BY_MANUFACTURER_DATA[mfr_id].append((model_chr, model))
  400. def parse_advertisement_data(
  401. device: BLEDevice,
  402. advertisement_data: AdvertisementData,
  403. model: SwitchbotModel | None = None,
  404. ) -> SwitchBotAdvertisement | None:
  405. """Parse advertisement data."""
  406. upper_mac = format_mac_upper(device.address)
  407. if model is None and upper_mac in _MODEL_TO_MAC_CACHE:
  408. model = _MODEL_TO_MAC_CACHE[upper_mac]
  409. service_data = advertisement_data.service_data
  410. _service_data = None
  411. for uuid in SERVICE_DATA_ORDER:
  412. if uuid in service_data:
  413. _service_data = service_data[uuid]
  414. break
  415. _mfr_data = None
  416. _mfr_id = None
  417. for mfr_id in MFR_DATA_ORDER:
  418. if mfr_id in advertisement_data.manufacturer_data:
  419. _mfr_id = mfr_id
  420. _mfr_data = advertisement_data.manufacturer_data[mfr_id]
  421. break
  422. if _mfr_data is None and _service_data is None:
  423. return None
  424. try:
  425. data = _parse_data(
  426. _service_data,
  427. _mfr_data,
  428. _mfr_id,
  429. model,
  430. )
  431. except Exception: # pylint: disable=broad-except
  432. _LOGGER.exception("Failed to parse advertisement data: %s", advertisement_data)
  433. return None
  434. if not data:
  435. return None
  436. return SwitchBotAdvertisement(
  437. device.address, data, device, advertisement_data.rssi, bool(_service_data)
  438. )
  439. @lru_cache(maxsize=128)
  440. def _parse_data(
  441. _service_data: bytes | None,
  442. _mfr_data: bytes | None,
  443. _mfr_id: int | None = None,
  444. _switchbot_model: SwitchbotModel | None = None,
  445. ) -> dict[str, Any] | None:
  446. """Parse advertisement data."""
  447. _model = chr(_service_data[0] & 0b01111111) if _service_data else None
  448. if _switchbot_model and _switchbot_model in _SWITCHBOT_MODEL_TO_CHAR:
  449. _model = _SWITCHBOT_MODEL_TO_CHAR[_switchbot_model]
  450. if not _model and _mfr_id and _mfr_id in MODELS_BY_MANUFACTURER_DATA:
  451. for model_chr, model_data in MODELS_BY_MANUFACTURER_DATA[_mfr_id]:
  452. if model_data.get("manufacturer_data_length") == len(_mfr_data):
  453. _model = model_chr
  454. break
  455. if _service_data and len(_service_data) > 5:
  456. for s in (_service_data[-4:], _service_data[-5:-1]):
  457. if s in SUPPORTED_TYPES:
  458. _model = s
  459. break
  460. if not _model:
  461. return None
  462. _isEncrypted = bool(_service_data[0] & 0b10000000) if _service_data else False
  463. data = {
  464. "rawAdvData": _service_data,
  465. "data": {},
  466. "model": _model,
  467. "isEncrypted": _isEncrypted,
  468. }
  469. type_data = SUPPORTED_TYPES.get(_model)
  470. if type_data:
  471. model_data = type_data["func"](_service_data, _mfr_data)
  472. if model_data:
  473. data.update(
  474. {
  475. "modelFriendlyName": type_data["modelFriendlyName"],
  476. "modelName": type_data["modelName"],
  477. "data": model_data,
  478. }
  479. )
  480. return data
  481. def populate_model_to_mac_cache(mac: str, model: SwitchbotModel) -> None:
  482. """Populate the model to MAC address cache."""
  483. _MODEL_TO_MAC_CACHE[mac] = model