1
0

adv_parser.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. from collections.abc import Callable
  4. from typing import TypedDict
  5. from bleak.backends.device import BLEDevice
  6. from bleak.backends.scanner import AdvertisementData
  7. from .adv_parsers.bot import process_wohand
  8. from .adv_parsers.bulb import process_color_bulb
  9. from .adv_parsers.contact import process_wocontact
  10. from .adv_parsers.curtain import process_wocurtain
  11. from .adv_parsers.light_strip import process_wostrip
  12. from .adv_parsers.meter import process_wosensorth
  13. from .adv_parsers.motion import process_wopresence
  14. from .adv_parsers.plug import process_woplugmini
  15. from .const import SwitchbotModel
  16. from .models import SwitchBotAdvertisement
  17. class SwitchbotSupportedType(TypedDict):
  18. """Supported type of Switchbot."""
  19. modelName: SwitchbotModel
  20. modelFriendlyName: str
  21. func: Callable[[bytes, bytes | None], dict[str, bool | int]]
  22. SUPPORTED_TYPES: dict[str, SwitchbotSupportedType] = {
  23. "d": {
  24. "modelName": SwitchbotModel.CONTACT_SENSOR,
  25. "modelFriendlyName": "Contact Sensor",
  26. "func": process_wocontact,
  27. },
  28. "H": {
  29. "modelName": SwitchbotModel.BOT,
  30. "modelFriendlyName": "Bot",
  31. "func": process_wohand,
  32. },
  33. "s": {
  34. "modelName": SwitchbotModel.MOTION_SENSOR,
  35. "modelFriendlyName": "Motion Sensor",
  36. "func": process_wopresence,
  37. },
  38. "r": {
  39. "modelName": SwitchbotModel.LIGHT_STRIP,
  40. "modelFriendlyName": "Light Strip",
  41. "func": process_wostrip,
  42. },
  43. "c": {
  44. "modelName": SwitchbotModel.CURTAIN,
  45. "modelFriendlyName": "Curtain",
  46. "func": process_wocurtain,
  47. },
  48. "T": {
  49. "modelName": SwitchbotModel.METER,
  50. "modelFriendlyName": "Meter",
  51. "func": process_wosensorth,
  52. },
  53. "i": {
  54. "modelName": SwitchbotModel.METER,
  55. "modelFriendlyName": "Meter Plus",
  56. "func": process_wosensorth,
  57. },
  58. "g": {
  59. "modelName": SwitchbotModel.PLUG_MINI,
  60. "modelFriendlyName": "Plug Mini",
  61. "func": process_woplugmini,
  62. },
  63. "u": {
  64. "modelName": SwitchbotModel.COLOR_BULB,
  65. "modelFriendlyName": "Color Bulb",
  66. "func": process_color_bulb,
  67. },
  68. }
  69. def parse_advertisement_data(
  70. device: BLEDevice, advertisement_data: AdvertisementData
  71. ) -> SwitchBotAdvertisement | None:
  72. """Parse advertisement data."""
  73. _services = list(advertisement_data.service_data.values())
  74. _mgr_datas = list(advertisement_data.manufacturer_data.values())
  75. if not _services:
  76. return None
  77. _service_data = _services[0]
  78. if not _service_data:
  79. return None
  80. _mfr_data = _mgr_datas[0] if _mgr_datas else None
  81. _model = chr(_service_data[0] & 0b01111111)
  82. data = {
  83. "address": device.address, # MacOS uses UUIDs
  84. "rawAdvData": list(advertisement_data.service_data.values())[0],
  85. "data": {},
  86. }
  87. type_data = SUPPORTED_TYPES.get(_model)
  88. if type_data:
  89. data.update(
  90. {
  91. "isEncrypted": bool(_service_data[0] & 0b10000000),
  92. "model": _model,
  93. "modelFriendlyName": type_data["modelFriendlyName"],
  94. "modelName": type_data["modelName"],
  95. "data": type_data["func"](_service_data, _mfr_data),
  96. }
  97. )
  98. data["data"]["rssi"] = device.rssi
  99. return SwitchBotAdvertisement(device.address, data, device)