1
0

bot.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. import logging
  4. from typing import Any
  5. from .device import DEVICE_SET_EXTENDED_KEY, DEVICE_SET_MODE_KEY, SwitchbotDevice
  6. # Bot keys
  7. PRESS_KEY = "570100"
  8. ON_KEY = "570101"
  9. OFF_KEY = "570102"
  10. DOWN_KEY = "570103"
  11. UP_KEY = "570104"
  12. _LOGGER = logging.getLogger(__name__)
  13. class Switchbot(SwitchbotDevice):
  14. """Representation of a Switchbot."""
  15. def __init__(self, *args: Any, **kwargs: Any) -> None:
  16. """Switchbot Bot/WoHand constructor."""
  17. super().__init__(*args, **kwargs)
  18. self._inverse: bool = kwargs.pop("inverse_mode", False)
  19. self._settings: dict[str, Any] = {}
  20. async def update(self, interface: int | None = None) -> None:
  21. """Update mode, battery percent and state of device."""
  22. await self.get_device_data(retry=self._retry_count, interface=interface)
  23. async def turn_on(self) -> bool:
  24. """Turn device on."""
  25. result = await self._sendcommand(ON_KEY)
  26. if result[0] == 1:
  27. return True
  28. if result[0] == 5:
  29. _LOGGER.debug(
  30. "%s: Bot is in press mode and doesn't have on state", self.name
  31. )
  32. return True
  33. return False
  34. async def turn_off(self) -> bool:
  35. """Turn device off."""
  36. result = await self._sendcommand(OFF_KEY)
  37. if result[0] == 1:
  38. return True
  39. if result[0] == 5:
  40. _LOGGER.debug(
  41. "%s: Bot is in press mode and doesn't have off state", self.name
  42. )
  43. return True
  44. return False
  45. async def hand_up(self) -> bool:
  46. """Raise device arm."""
  47. result = await self._sendcommand(UP_KEY)
  48. if result[0] == 1:
  49. return True
  50. if result[0] == 5:
  51. _LOGGER.debug("%s: Bot is in press mode", self.name)
  52. return True
  53. return False
  54. async def hand_down(self) -> bool:
  55. """Lower device arm."""
  56. result = await self._sendcommand(DOWN_KEY)
  57. if result[0] == 1:
  58. return True
  59. if result[0] == 5:
  60. _LOGGER.debug("%s: Bot is in press mode", self.name)
  61. return True
  62. return False
  63. async def press(self) -> bool:
  64. """Press command to device."""
  65. result = await self._sendcommand(PRESS_KEY)
  66. if result[0] == 1:
  67. return True
  68. if result[0] == 5:
  69. _LOGGER.debug("%s: Bot is in switch mode", self.name)
  70. return True
  71. return False
  72. async def set_switch_mode(
  73. self, switch_mode: bool = False, strength: int = 100, inverse: bool = False
  74. ) -> bool:
  75. """Change bot mode."""
  76. mode_key = format(switch_mode, "b") + format(inverse, "b")
  77. strength_key = f"{strength:0{2}x}" # to hex with padding to double digit
  78. result = await self._sendcommand(DEVICE_SET_MODE_KEY + strength_key + mode_key)
  79. return result[0] == 1
  80. async def set_long_press(self, duration: int = 0) -> bool:
  81. """Set bot long press duration."""
  82. duration_key = f"{duration:0{2}x}" # to hex with padding to double digit
  83. result = await self._sendcommand(DEVICE_SET_EXTENDED_KEY + "08" + duration_key)
  84. return result[0] == 1
  85. async def get_basic_info(self) -> dict[str, Any] | None:
  86. """Get device basic settings."""
  87. if not (_data := await self._get_basic_info()):
  88. return None
  89. return {
  90. "battery": _data[1],
  91. "firmware": _data[2] / 10.0,
  92. "strength": _data[3],
  93. "timers": _data[8],
  94. "switchMode": bool(_data[9] & 16),
  95. "inverseDirection": bool(_data[9] & 1),
  96. "holdSeconds": _data[10],
  97. }
  98. def switch_mode(self) -> Any:
  99. """Return true or false from cache."""
  100. # To get actual position call update() first.
  101. return self._get_adv_value("switchMode")
  102. def is_on(self) -> Any:
  103. """Return switch state from cache."""
  104. # To get actual position call update() first.
  105. value = self._get_adv_value("isOn")
  106. if value is None:
  107. return None
  108. if self._inverse:
  109. return not value
  110. return value