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