base_cover.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. import logging
  4. from abc import abstractmethod
  5. from typing import Any
  6. from .device import REQ_HEADER, SwitchbotDevice, update_after_operation
  7. # Cover keys
  8. COVER_COMMAND = "4501"
  9. ROLLERSHADE_COMMAND = "4701"
  10. CONTROL_SOURCE = "00"
  11. # For second element of open and close arrs we should add two bytes i.e. ff00
  12. # First byte [ff] stands for speed (00 or ff - normal, 01 - slow) *
  13. # * Only for curtains 3. For other models use ff
  14. # Second byte [00] is a command (00 - open, 64 - close)
  15. POSITION_KEYS = [
  16. f"{REQ_HEADER}{COVER_COMMAND}0101",
  17. f"{REQ_HEADER}{COVER_COMMAND}05", # +speed
  18. ] # +actual_position
  19. STOP_KEYS = [f"{REQ_HEADER}{COVER_COMMAND}0001", f"{REQ_HEADER}{COVER_COMMAND}00ff"]
  20. COVER_EXT_SUM_KEY = f"{REQ_HEADER}460401"
  21. COVER_EXT_ADV_KEY = f"{REQ_HEADER}460402"
  22. _LOGGER = logging.getLogger(__name__)
  23. class SwitchbotBaseCover(SwitchbotDevice):
  24. """Representation of a Switchbot Cover devices for both curtains and tilt blinds."""
  25. def __init__(self, reverse: bool, *args: Any, **kwargs: Any) -> None:
  26. """Switchbot Cover device constructor."""
  27. super().__init__(*args, **kwargs)
  28. self._reverse = reverse
  29. self._settings: dict[str, Any] = {}
  30. self.ext_info_sum: dict[str, Any] = {}
  31. self.ext_info_adv: dict[str, Any] = {}
  32. self._is_opening: bool = False
  33. self._is_closing: bool = False
  34. @update_after_operation
  35. async def stop(self) -> bool:
  36. """Send stop command to device."""
  37. return await self._send_multiple_commands(STOP_KEYS)
  38. @update_after_operation
  39. async def set_position(self, position: int, speed: int = 255) -> bool:
  40. """Send position command (0-100) to device. Speed 255 - normal, 1 - slow"""
  41. position = (100 - position) if self._reverse else position
  42. return await self._send_multiple_commands(
  43. [
  44. f"{POSITION_KEYS[0]}{position:02X}",
  45. f"{POSITION_KEYS[1]}{speed:02X}{position:02X}",
  46. ]
  47. )
  48. @abstractmethod
  49. def get_position(self) -> Any:
  50. """Return current device position."""
  51. @abstractmethod
  52. async def get_basic_info(self) -> dict[str, Any] | None:
  53. """Get device basic settings."""
  54. @abstractmethod
  55. async def get_extended_info_summary(self) -> dict[str, Any] | None:
  56. """Get extended info for all devices in chain."""
  57. async def get_extended_info_adv(self) -> dict[str, Any] | None:
  58. """Get advance page info for device chain."""
  59. _data = await self._send_command(key=COVER_EXT_ADV_KEY)
  60. if not _data:
  61. _LOGGER.error("%s: Unsuccessful, no result from device", self.name)
  62. return None
  63. if _data in (b"\x07", b"\x00"):
  64. _LOGGER.error("%s: Unsuccessful, please try again", self.name)
  65. return None
  66. _state_of_charge = [
  67. "not_charging",
  68. "charging_by_adapter",
  69. "charging_by_solar",
  70. "fully_charged",
  71. "solar_not_charging",
  72. "charging_error",
  73. ]
  74. self.ext_info_adv["device0"] = {
  75. "battery": _data[1],
  76. "firmware": _data[2] / 10.0,
  77. "stateOfCharge": _state_of_charge[_data[3]],
  78. }
  79. # If grouped curtain device present.
  80. if _data[4]:
  81. self.ext_info_adv["device1"] = {
  82. "battery": _data[4],
  83. "firmware": _data[5] / 10.0,
  84. "stateOfCharge": _state_of_charge[_data[6]],
  85. }
  86. return self.ext_info_adv
  87. def get_light_level(self) -> Any:
  88. """Return cached light level."""
  89. # To get actual light level call update() first.
  90. return self._get_adv_value("lightLevel")
  91. def is_reversed(self) -> bool:
  92. """Return True if curtain position is opposite from SB data."""
  93. return self._reverse
  94. def is_calibrated(self) -> Any:
  95. """Return True curtain is calibrated."""
  96. # To get actual light level call update() first.
  97. return self._get_adv_value("calibration")
  98. def is_opening(self) -> bool:
  99. """Return True if the curtain is opening."""
  100. return self._is_opening
  101. def is_closing(self) -> bool:
  102. """Return True if the curtain is closing."""
  103. return self._is_closing