adv_parser.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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.blind_tilt import process_woblindtilt
  10. from .adv_parsers.bot import process_wohand
  11. from .adv_parsers.bulb import process_color_bulb
  12. from .adv_parsers.ceiling_light import process_woceiling
  13. from .adv_parsers.contact import process_wocontact
  14. from .adv_parsers.curtain import process_wocurtain
  15. from .adv_parsers.hub2 import process_wohub2
  16. from .adv_parsers.humidifier import process_evaporative_humidifier, process_wohumidifier
  17. from .adv_parsers.keypad import process_wokeypad
  18. from .adv_parsers.leak import process_leak
  19. from .adv_parsers.light_strip import process_wostrip
  20. from .adv_parsers.lock import process_wolock, process_wolock_pro
  21. from .adv_parsers.meter import process_wosensorth, process_wosensorth_c
  22. from .adv_parsers.motion import process_wopresence
  23. from .adv_parsers.plug import process_woplugmini
  24. from .adv_parsers.relay_switch import (
  25. process_worelay_switch_1,
  26. process_worelay_switch_1pm,
  27. )
  28. from .adv_parsers.remote import process_woremote
  29. from .const import SwitchbotModel
  30. from .models import SwitchBotAdvertisement
  31. _LOGGER = logging.getLogger(__name__)
  32. SERVICE_DATA_ORDER = (
  33. "0000fd3d-0000-1000-8000-00805f9b34fb",
  34. "00000d00-0000-1000-8000-00805f9b34fb",
  35. )
  36. MFR_DATA_ORDER = (2409, 741, 89)
  37. class SwitchbotSupportedType(TypedDict):
  38. """Supported type of Switchbot."""
  39. modelName: SwitchbotModel
  40. modelFriendlyName: str
  41. func: Callable[[bytes, bytes | None], dict[str, bool | int]]
  42. manufacturer_id: int | None
  43. manufacturer_data_length: int | None
  44. SUPPORTED_TYPES: dict[str, SwitchbotSupportedType] = {
  45. "d": {
  46. "modelName": SwitchbotModel.CONTACT_SENSOR,
  47. "modelFriendlyName": "Contact Sensor",
  48. "func": process_wocontact,
  49. "manufacturer_id": 2409,
  50. },
  51. "H": {
  52. "modelName": SwitchbotModel.BOT,
  53. "modelFriendlyName": "Bot",
  54. "func": process_wohand,
  55. "manufacturer_id": 89,
  56. },
  57. "s": {
  58. "modelName": SwitchbotModel.MOTION_SENSOR,
  59. "modelFriendlyName": "Motion Sensor",
  60. "func": process_wopresence,
  61. "manufacturer_id": 2409,
  62. },
  63. "r": {
  64. "modelName": SwitchbotModel.LIGHT_STRIP,
  65. "modelFriendlyName": "Light Strip",
  66. "func": process_wostrip,
  67. "manufacturer_id": 2409,
  68. },
  69. "{": {
  70. "modelName": SwitchbotModel.CURTAIN,
  71. "modelFriendlyName": "Curtain 3",
  72. "func": process_wocurtain,
  73. "manufacturer_id": 2409,
  74. },
  75. "c": {
  76. "modelName": SwitchbotModel.CURTAIN,
  77. "modelFriendlyName": "Curtain",
  78. "func": process_wocurtain,
  79. "manufacturer_id": 2409,
  80. },
  81. "w": {
  82. "modelName": SwitchbotModel.IO_METER,
  83. "modelFriendlyName": "Indoor/Outdoor Meter",
  84. "func": process_wosensorth,
  85. "manufacturer_id": 2409,
  86. },
  87. "i": {
  88. "modelName": SwitchbotModel.METER,
  89. "modelFriendlyName": "Meter Plus",
  90. "func": process_wosensorth,
  91. "manufacturer_id": 2409,
  92. },
  93. "T": {
  94. "modelName": SwitchbotModel.METER,
  95. "modelFriendlyName": "Meter",
  96. "func": process_wosensorth,
  97. "manufacturer_id": 2409,
  98. },
  99. "4": {
  100. "modelName": SwitchbotModel.METER_PRO,
  101. "modelFriendlyName": "Meter",
  102. "func": process_wosensorth,
  103. "manufacturer_id": 2409,
  104. },
  105. "5": {
  106. "modelName": SwitchbotModel.METER_PRO_C,
  107. "modelFriendlyName": "Meter",
  108. "func": process_wosensorth_c,
  109. "manufacturer_id": 2409,
  110. },
  111. "v": {
  112. "modelName": SwitchbotModel.HUB2,
  113. "modelFriendlyName": "Hub 2",
  114. "func": process_wohub2,
  115. "manufacturer_id": 2409,
  116. },
  117. "g": {
  118. "modelName": SwitchbotModel.PLUG_MINI,
  119. "modelFriendlyName": "Plug Mini",
  120. "func": process_woplugmini,
  121. "manufacturer_id": 2409,
  122. },
  123. "j": {
  124. "modelName": SwitchbotModel.PLUG_MINI,
  125. "modelFriendlyName": "Plug Mini (JP)",
  126. "func": process_woplugmini,
  127. "manufacturer_id": 2409,
  128. },
  129. "u": {
  130. "modelName": SwitchbotModel.COLOR_BULB,
  131. "modelFriendlyName": "Color Bulb",
  132. "func": process_color_bulb,
  133. "manufacturer_id": 2409,
  134. },
  135. "q": {
  136. "modelName": SwitchbotModel.CEILING_LIGHT,
  137. "modelFriendlyName": "Ceiling Light",
  138. "func": process_woceiling,
  139. "manufacturer_id": 2409,
  140. },
  141. "n": {
  142. "modelName": SwitchbotModel.CEILING_LIGHT,
  143. "modelFriendlyName": "Ceiling Light Pro",
  144. "func": process_woceiling,
  145. "manufacturer_id": 2409,
  146. },
  147. "e": {
  148. "modelName": SwitchbotModel.HUMIDIFIER,
  149. "modelFriendlyName": "Humidifier",
  150. "func": process_wohumidifier,
  151. "manufacturer_id": 741,
  152. "manufacturer_data_length": 6,
  153. },
  154. "#": {
  155. "modelName": SwitchbotModel.EVAPORATIVE_HUMIDIFIER,
  156. "modelFriendlyName": "Evaporative Humidifier",
  157. "func": process_evaporative_humidifier,
  158. "manufacturer_id": 2409,
  159. },
  160. "o": {
  161. "modelName": SwitchbotModel.LOCK,
  162. "modelFriendlyName": "Lock",
  163. "func": process_wolock,
  164. "manufacturer_id": 2409,
  165. },
  166. "$": {
  167. "modelName": SwitchbotModel.LOCK_PRO,
  168. "modelFriendlyName": "Lock Pro",
  169. "func": process_wolock_pro,
  170. "manufacturer_id": 2409,
  171. },
  172. "x": {
  173. "modelName": SwitchbotModel.BLIND_TILT,
  174. "modelFriendlyName": "Blind Tilt",
  175. "func": process_woblindtilt,
  176. "manufacturer_id": 2409,
  177. },
  178. "&": {
  179. "modelName": SwitchbotModel.LEAK,
  180. "modelFriendlyName": "Leak Detector",
  181. "func": process_leak,
  182. "manufacturer_id": 2409,
  183. },
  184. "y": {
  185. "modelName": SwitchbotModel.KEYPAD,
  186. "modelFriendlyName": "Keypad",
  187. "func": process_wokeypad,
  188. "manufacturer_id": 2409,
  189. },
  190. "<": {
  191. "modelName": SwitchbotModel.RELAY_SWITCH_1PM,
  192. "modelFriendlyName": "Relay Switch 1PM",
  193. "func": process_worelay_switch_1pm,
  194. "manufacturer_id": 2409,
  195. },
  196. ";": {
  197. "modelName": SwitchbotModel.RELAY_SWITCH_1,
  198. "modelFriendlyName": "Relay Switch 1",
  199. "func": process_worelay_switch_1,
  200. "manufacturer_id": 2409,
  201. },
  202. "b": {
  203. "modelName": SwitchbotModel.REMOTE,
  204. "modelFriendlyName": "Remote",
  205. "func": process_woremote,
  206. "manufacturer_id": 89,
  207. },
  208. }
  209. _SWITCHBOT_MODEL_TO_CHAR = {
  210. model_data["modelName"]: model_chr
  211. for model_chr, model_data in SUPPORTED_TYPES.items()
  212. }
  213. MODELS_BY_MANUFACTURER_DATA: dict[int, list[tuple[str, SwitchbotSupportedType]]] = {
  214. mfr_id: [] for mfr_id in MFR_DATA_ORDER
  215. }
  216. for model_chr, model in SUPPORTED_TYPES.items():
  217. if "manufacturer_id" in model:
  218. mfr_id = model["manufacturer_id"]
  219. MODELS_BY_MANUFACTURER_DATA[mfr_id].append((model_chr, model))
  220. def parse_advertisement_data(
  221. device: BLEDevice,
  222. advertisement_data: AdvertisementData,
  223. model: SwitchbotModel | None = None,
  224. ) -> SwitchBotAdvertisement | None:
  225. """Parse advertisement data."""
  226. service_data = advertisement_data.service_data
  227. _service_data = None
  228. for uuid in SERVICE_DATA_ORDER:
  229. if uuid in service_data:
  230. _service_data = service_data[uuid]
  231. break
  232. _mfr_data = None
  233. _mfr_id = None
  234. for mfr_id in MFR_DATA_ORDER:
  235. if mfr_id in advertisement_data.manufacturer_data:
  236. _mfr_id = mfr_id
  237. _mfr_data = advertisement_data.manufacturer_data[mfr_id]
  238. break
  239. if _mfr_data is None and _service_data is None:
  240. return None
  241. try:
  242. data = _parse_data(
  243. _service_data,
  244. _mfr_data,
  245. _mfr_id,
  246. model,
  247. )
  248. except Exception as err: # pylint: disable=broad-except
  249. _LOGGER.exception(
  250. "Failed to parse advertisement data: %s: %s", advertisement_data, err
  251. )
  252. return None
  253. if not data:
  254. return None
  255. return SwitchBotAdvertisement(
  256. device.address, data, device, advertisement_data.rssi, bool(_service_data)
  257. )
  258. @lru_cache(maxsize=128)
  259. def _parse_data(
  260. _service_data: bytes | None,
  261. _mfr_data: bytes | None,
  262. _mfr_id: int | None = None,
  263. _switchbot_model: SwitchbotModel | None = None,
  264. ) -> dict[str, Any] | None:
  265. """Parse advertisement data."""
  266. _model = chr(_service_data[0] & 0b01111111) if _service_data else None
  267. if _switchbot_model and _switchbot_model in _SWITCHBOT_MODEL_TO_CHAR:
  268. _model = _SWITCHBOT_MODEL_TO_CHAR[_switchbot_model]
  269. if not _model and _mfr_id and _mfr_id in MODELS_BY_MANUFACTURER_DATA:
  270. for model_chr, model_data in MODELS_BY_MANUFACTURER_DATA[_mfr_id]:
  271. if model_data.get("manufacturer_data_length") == len(_mfr_data):
  272. _model = model_chr
  273. break
  274. if not _model:
  275. return None
  276. _isEncrypted = bool(_service_data[0] & 0b10000000) if _service_data else False
  277. data = {
  278. "rawAdvData": _service_data,
  279. "data": {},
  280. "model": _model,
  281. "isEncrypted": _isEncrypted,
  282. }
  283. type_data = SUPPORTED_TYPES.get(_model)
  284. if type_data:
  285. model_data = type_data["func"](_service_data, _mfr_data)
  286. if model_data:
  287. data.update(
  288. {
  289. "modelFriendlyName": type_data["modelFriendlyName"],
  290. "modelName": type_data["modelName"],
  291. "data": model_data,
  292. }
  293. )
  294. return data