adv_parser.py 3.6 KB

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