adv_parser.py 9.2 KB

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