bot.py 3.4 KB

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