curtain.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. import logging
  4. from typing import Any
  5. from .device import SwitchbotDevice
  6. # Curtain keys
  7. OPEN_KEY = "570f450105ff00" # 570F4501010100
  8. CLOSE_KEY = "570f450105ff64" # 570F4501010164
  9. POSITION_KEY = "570F450105ff" # +actual_position ex: 570F450105ff32 for 50%
  10. STOP_KEY = "570F450100ff"
  11. CURTAIN_EXT_SUM_KEY = "570f460401"
  12. CURTAIN_EXT_ADV_KEY = "570f460402"
  13. CURTAIN_EXT_CHAIN_INFO_KEY = "570f468101"
  14. _LOGGER = logging.getLogger(__name__)
  15. class SwitchbotCurtain(SwitchbotDevice):
  16. """Representation of a Switchbot Curtain."""
  17. def __init__(self, *args: Any, **kwargs: Any) -> None:
  18. """Switchbot Curtain/WoCurtain constructor."""
  19. # The position of the curtain is saved returned with 0 = open and 100 = closed.
  20. # This is independent of the calibration of the curtain bot (Open left to right/
  21. # Open right to left/Open from the middle).
  22. # The parameter 'reverse_mode' reverse these values,
  23. # if 'reverse_mode' = True, position = 0 equals close
  24. # and position = 100 equals open. The parameter is default set to True so that
  25. # the definition of position is the same as in Home Assistant.
  26. super().__init__(*args, **kwargs)
  27. self._reverse: bool = kwargs.pop("reverse_mode", True)
  28. self._settings: dict[str, Any] = {}
  29. self.ext_info_sum: dict[str, Any] = {}
  30. self.ext_info_adv: dict[str, Any] = {}
  31. async def open(self) -> bool:
  32. """Send open command."""
  33. result = await self._sendcommand(OPEN_KEY)
  34. return result[0] == 1
  35. async def close(self) -> bool:
  36. """Send close command."""
  37. result = await self._sendcommand(CLOSE_KEY)
  38. return result[0] == 1
  39. async def stop(self) -> bool:
  40. """Send stop command to device."""
  41. result = await self._sendcommand(STOP_KEY)
  42. return result[0] == 1
  43. async def set_position(self, position: int) -> bool:
  44. """Send position command (0-100) to device."""
  45. position = (100 - position) if self._reverse else position
  46. hex_position = "%0.2X" % position
  47. result = await self._sendcommand(POSITION_KEY + hex_position)
  48. return result[0] == 1
  49. async def update(self, interface: int | None = None) -> None:
  50. """Update position, battery percent and light level of device."""
  51. await self.get_device_data(retry=self._retry_count, interface=interface)
  52. def get_position(self) -> Any:
  53. """Return cached position (0-100) of Curtain."""
  54. # To get actual position call update() first.
  55. return self._get_adv_value("position")
  56. async def get_basic_info(self) -> dict[str, Any] | None:
  57. """Get device basic settings."""
  58. if not (_data := await self._get_basic_info()):
  59. return None
  60. _position = max(min(_data[6], 100), 0)
  61. return {
  62. "battery": _data[1],
  63. "firmware": _data[2] / 10.0,
  64. "chainLength": _data[3],
  65. "openDirection": (
  66. "right_to_left" if _data[4] & 0b10000000 == 128 else "left_to_right"
  67. ),
  68. "touchToOpen": bool(_data[4] & 0b01000000),
  69. "light": bool(_data[4] & 0b00100000),
  70. "fault": bool(_data[4] & 0b00001000),
  71. "solarPanel": bool(_data[5] & 0b00001000),
  72. "calibrated": bool(_data[5] & 0b00000100),
  73. "inMotion": bool(_data[5] & 0b01000011),
  74. "position": (100 - _position) if self._reverse else _position,
  75. "timers": _data[7],
  76. }
  77. async def get_extended_info_summary(self) -> dict[str, Any] | None:
  78. """Get basic info for all devices in chain."""
  79. _data = await self._sendcommand(key=CURTAIN_EXT_SUM_KEY)
  80. if _data in (b"\x07", b"\x00"):
  81. _LOGGER.error("%s: Unsuccessful, please try again", self.name)
  82. return None
  83. self.ext_info_sum["device0"] = {
  84. "openDirectionDefault": not bool(_data[1] & 0b10000000),
  85. "touchToOpen": bool(_data[1] & 0b01000000),
  86. "light": bool(_data[1] & 0b00100000),
  87. "openDirection": (
  88. "left_to_right" if _data[1] & 0b00010000 == 1 else "right_to_left"
  89. ),
  90. }
  91. # if grouped curtain device present.
  92. if _data[2] != 0:
  93. self.ext_info_sum["device1"] = {
  94. "openDirectionDefault": not bool(_data[1] & 0b10000000),
  95. "touchToOpen": bool(_data[1] & 0b01000000),
  96. "light": bool(_data[1] & 0b00100000),
  97. "openDirection": (
  98. "left_to_right" if _data[1] & 0b00010000 else "right_to_left"
  99. ),
  100. }
  101. return self.ext_info_sum
  102. async def get_extended_info_adv(self) -> dict[str, Any] | None:
  103. """Get advance page info for device chain."""
  104. _data = await self._sendcommand(key=CURTAIN_EXT_ADV_KEY)
  105. if _data in (b"\x07", b"\x00"):
  106. _LOGGER.error("%s: Unsuccessful, please try again", self.name)
  107. return None
  108. _state_of_charge = [
  109. "not_charging",
  110. "charging_by_adapter",
  111. "charging_by_solar",
  112. "fully_charged",
  113. "solar_not_charging",
  114. "charging_error",
  115. ]
  116. self.ext_info_adv["device0"] = {
  117. "battery": _data[1],
  118. "firmware": _data[2] / 10.0,
  119. "stateOfCharge": _state_of_charge[_data[3]],
  120. }
  121. # If grouped curtain device present.
  122. if _data[4]:
  123. self.ext_info_adv["device1"] = {
  124. "battery": _data[4],
  125. "firmware": _data[5] / 10.0,
  126. "stateOfCharge": _state_of_charge[_data[6]],
  127. }
  128. return self.ext_info_adv
  129. def get_light_level(self) -> Any:
  130. """Return cached light level."""
  131. # To get actual light level call update() first.
  132. return self._get_adv_value("lightLevel")
  133. def is_reversed(self) -> bool:
  134. """Return True if curtain position is opposite from SB data."""
  135. return self._reverse
  136. def is_calibrated(self) -> Any:
  137. """Return True curtain is calibrated."""
  138. # To get actual light level call update() first.
  139. return self._get_adv_value("calibration")