bot.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. async def update(self, interface: int | None = None) -> None:
  20. """Update mode, battery percent and state of device."""
  21. await self.get_device_data(retry=self._retry_count, interface=interface)
  22. async def turn_on(self) -> bool:
  23. """Turn device on."""
  24. result = await self._sendcommand(ON_KEY)
  25. return self._check_command_result(result, 0, {1, 5})
  26. async def turn_off(self) -> bool:
  27. """Turn device off."""
  28. result = await self._sendcommand(OFF_KEY)
  29. return self._check_command_result(result, 0, {1, 5})
  30. async def hand_up(self) -> bool:
  31. """Raise device arm."""
  32. result = await self._sendcommand(UP_KEY)
  33. return self._check_command_result(result, 0, {1, 5})
  34. async def hand_down(self) -> bool:
  35. """Lower device arm."""
  36. result = await self._sendcommand(DOWN_KEY)
  37. return self._check_command_result(result, 0, {1, 5})
  38. async def press(self) -> bool:
  39. """Press command to device."""
  40. result = await self._sendcommand(PRESS_KEY)
  41. return self._check_command_result(result, 0, {1, 5})
  42. async def set_switch_mode(
  43. self, switch_mode: bool = False, strength: int = 100, inverse: bool = False
  44. ) -> bool:
  45. """Change bot mode."""
  46. mode_key = format(switch_mode, "b") + format(inverse, "b")
  47. strength_key = f"{strength:0{2}x}" # to hex with padding to double digit
  48. result = await self._sendcommand(DEVICE_SET_MODE_KEY + strength_key + mode_key)
  49. return self._check_command_result(result, 0, {1})
  50. async def set_long_press(self, duration: int = 0) -> bool:
  51. """Set bot long press duration."""
  52. duration_key = f"{duration:0{2}x}" # to hex with padding to double digit
  53. result = await self._sendcommand(DEVICE_SET_EXTENDED_KEY + "08" + duration_key)
  54. return self._check_command_result(result, 0, {1})
  55. async def get_basic_info(self) -> dict[str, Any] | None:
  56. """Get device basic settings."""
  57. if not (_data := await self._get_basic_info()):
  58. return None
  59. return {
  60. "battery": _data[1],
  61. "firmware": _data[2] / 10.0,
  62. "strength": _data[3],
  63. "timers": _data[8],
  64. "switchMode": bool(_data[9] & 16),
  65. "inverseDirection": bool(_data[9] & 1),
  66. "holdSeconds": _data[10],
  67. }
  68. def switch_mode(self) -> Any:
  69. """Return true or false from cache."""
  70. # To get actual position call update() first.
  71. return self._get_adv_value("switchMode")
  72. def is_on(self) -> Any:
  73. """Return switch state from cache."""
  74. # To get actual position call update() first.
  75. value = self._get_adv_value("isOn")
  76. if value is None:
  77. return None
  78. if self._inverse:
  79. return not value
  80. return value