1
0

adv_parser.py 14 KB

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