1
0

adv_parser.py 9.0 KB

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