1
0

adv_parser.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.meter import process_wosensorth
  12. from .adv_parsers.motion import process_wopresence
  13. from .adv_parsers.plug import process_woplugmini
  14. from .models import SwitchBotAdvertisement
  15. class SwitchbotSupportedType(TypedDict):
  16. """Supported type of Switchbot."""
  17. modelName: str
  18. modelFriendlyName: str
  19. func: Callable[[bytes, bytes | None], dict[str, bool | int]]
  20. SUPPORTED_TYPES: dict[str, SwitchbotSupportedType] = {
  21. "d": {
  22. "modelName": "WoContact",
  23. "modelFriendlyName": "Contact Sensor",
  24. "func": process_wocontact,
  25. },
  26. "H": {"modelName": "WoHand", "modelFriendlyName": "Bot", "func": process_wohand},
  27. "s": {
  28. "modelName": "WoPresence",
  29. "modelFriendlyName": "Motion Sensor",
  30. "func": process_wopresence,
  31. },
  32. "c": {
  33. "modelName": "WoCurtain",
  34. "modelFriendlyName": "Curtain",
  35. "func": process_wocurtain,
  36. },
  37. "T": {
  38. "modelName": "WoSensorTH",
  39. "modelFriendlyName": "Meter",
  40. "func": process_wosensorth,
  41. },
  42. "i": {
  43. "modelName": "WoSensorTH",
  44. "modelFriendlyName": "Meter Plus",
  45. "func": process_wosensorth,
  46. },
  47. "g": {
  48. "modelName": "WoPlug",
  49. "modelFriendlyName": "Plug Mini",
  50. "func": process_woplugmini,
  51. },
  52. "u": {
  53. "modelName": "WoBulb",
  54. "modelFriendlyName": "Color Bulb",
  55. "func": process_color_bulb,
  56. },
  57. }
  58. def parse_advertisement_data(
  59. device: BLEDevice, advertisement_data: AdvertisementData
  60. ) -> SwitchBotAdvertisement | None:
  61. """Parse advertisement data."""
  62. _services = list(advertisement_data.service_data.values())
  63. _mgr_datas = list(advertisement_data.manufacturer_data.values())
  64. if not _services:
  65. return None
  66. _service_data = _services[0]
  67. _mfr_data = _mgr_datas[0] if _mgr_datas else None
  68. _model = chr(_service_data[0] & 0b01111111)
  69. data = {
  70. "address": device.address, # MacOS uses UUIDs
  71. "rawAdvData": list(advertisement_data.service_data.values())[0],
  72. "data": {},
  73. }
  74. type_data = SUPPORTED_TYPES.get(_model)
  75. if type_data:
  76. data.update(
  77. {
  78. "isEncrypted": bool(_service_data[0] & 0b10000000),
  79. "model": _model,
  80. "model_friendly_name": type_data["modelFriendlyName"],
  81. "modelName": type_data["modelName"],
  82. "data": type_data["func"](_service_data, _mfr_data),
  83. }
  84. )
  85. data["data"]["rssi"] = device.rssi
  86. return SwitchBotAdvertisement(device.address, data, device)