blind_tilt.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. import logging
  4. from typing import Any
  5. from switchbot.devices.device import (
  6. REQ_HEADER,
  7. SwitchbotSequenceDevice,
  8. update_after_operation,
  9. )
  10. from .curtain import CURTAIN_EXT_SUM_KEY, SwitchbotCurtain
  11. _LOGGER = logging.getLogger(__name__)
  12. BLIND_COMMAND = "4501"
  13. OPEN_KEYS = [
  14. f"{REQ_HEADER}{BLIND_COMMAND}010132",
  15. f"{REQ_HEADER}{BLIND_COMMAND}05ff32",
  16. ]
  17. CLOSE_DOWN_KEYS = [
  18. f"{REQ_HEADER}{BLIND_COMMAND}010100",
  19. f"{REQ_HEADER}{BLIND_COMMAND}05ff00",
  20. ]
  21. CLOSE_UP_KEYS = [
  22. f"{REQ_HEADER}{BLIND_COMMAND}010164",
  23. f"{REQ_HEADER}{BLIND_COMMAND}05ff64",
  24. ]
  25. class SwitchbotBlindTilt(SwitchbotCurtain, SwitchbotSequenceDevice):
  26. """Representation of a Switchbot Blind Tilt."""
  27. # The position of the blind is saved returned with 0 = closed down, 50 = open and 100 = closed up.
  28. # This is independent of the calibration of the blind.
  29. # The parameter 'reverse_mode' reverse these values,
  30. # if 'reverse_mode' = True, position = 0 equals closed up
  31. # and position = 100 equals closed down. The parameter is default set to False so that
  32. # the definition of position is the same as in Home Assistant.
  33. # This is opposite to the base class so needs to be overwritten.
  34. def __init__(self, *args: Any, **kwargs: Any) -> None:
  35. """Switchbot Blind Tilt/woBlindTilt constructor."""
  36. super().__init__(*args, **kwargs)
  37. self._reverse: bool = kwargs.pop("reverse_mode", False)
  38. @update_after_operation
  39. async def open(self) -> bool:
  40. """Send open command."""
  41. return await self._send_multiple_commands(OPEN_KEYS)
  42. @update_after_operation
  43. async def close_up(self) -> bool:
  44. """Send close up command."""
  45. return await self._send_multiple_commands(CLOSE_UP_KEYS)
  46. @update_after_operation
  47. async def close_down(self) -> bool:
  48. """Send close down command."""
  49. return await self._send_multiple_commands(CLOSE_DOWN_KEYS)
  50. # The aim of this is to close to the nearest endpoint.
  51. # If we're open upwards we close up, if we're open downwards we close down.
  52. # If we're in the middle we default to close down as that seems to be the app's preference.
  53. @update_after_operation
  54. async def close(self) -> bool:
  55. """Send close command."""
  56. if self.get_position() > 50:
  57. return await self.close_up()
  58. else:
  59. return await self.close_down()
  60. def get_position(self) -> Any:
  61. """Return cached tilt (0-100) of Blind Tilt."""
  62. # To get actual tilt call update() first.
  63. return self._get_adv_value("tilt")
  64. async def get_basic_info(self) -> dict[str, Any] | None:
  65. """Get device basic settings."""
  66. if not (_data := await self._get_basic_info()):
  67. return None
  68. _tilt = max(min(_data[6], 100), 0)
  69. _moving = bool(_data[5] & 0b00000011)
  70. if _moving:
  71. _opening = bool(_data[5] & 0b00000010)
  72. _closing = not _opening and bool(_data[5] & 0b00000001)
  73. if _opening:
  74. _flag = bool(_data[5] & 0b00000001)
  75. _up = _flag if self._reverse else not _flag
  76. else:
  77. _up = _tilt < 50 if self._reverse else _tilt > 50
  78. return {
  79. "battery": _data[1],
  80. "firmware": _data[2] / 10.0,
  81. "light": bool(_data[4] & 0b00100000),
  82. "fault": bool(_data[4] & 0b00001000),
  83. "solarPanel": bool(_data[5] & 0b00001000),
  84. "calibration": bool(_data[5] & 0b00000100),
  85. "calibrated": bool(_data[5] & 0b00000100),
  86. "inMotion": _moving,
  87. "motionDirection": {
  88. "opening": _moving and _opening,
  89. "closing": _moving and _closing,
  90. "up": _moving and _up,
  91. "down": _moving and not _up,
  92. },
  93. "tilt": (100 - _tilt) if self._reverse else _tilt,
  94. "timers": _data[7],
  95. }
  96. async def get_extended_info_summary(self) -> dict[str, Any] | None:
  97. """Get basic info for all devices in chain."""
  98. _data = await self._send_command(key=CURTAIN_EXT_SUM_KEY)
  99. if not _data:
  100. _LOGGER.error("%s: Unsuccessful, no result from device", self.name)
  101. return None
  102. if _data in (b"\x07", b"\x00"):
  103. _LOGGER.error("%s: Unsuccessful, please try again", self.name)
  104. return None
  105. self.ext_info_sum["device0"] = {
  106. "light": bool(_data[1] & 0b00100000),
  107. }
  108. return self.ext_info_sum