|
| 1 | +"""Library to handle connection with Switchbot.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from ..models import SwitchBotAdvertisement |
| 9 | +from .base_cover import CONTROL_SOURCE, ROLLERSHADE_COMMAND, SwitchbotBaseCover |
| 10 | +from .device import REQ_HEADER, SwitchbotSequenceDevice, update_after_operation |
| 11 | + |
| 12 | +_LOGGER = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +OPEN_KEYS = [ |
| 16 | + f"{REQ_HEADER}{ROLLERSHADE_COMMAND}01{CONTROL_SOURCE}0100", |
| 17 | + f"{REQ_HEADER}{ROLLERSHADE_COMMAND}05{CONTROL_SOURCE}0000", |
| 18 | +] |
| 19 | +CLOSE_KEYS = [ |
| 20 | + f"{REQ_HEADER}{ROLLERSHADE_COMMAND}01{CONTROL_SOURCE}0164", |
| 21 | + f"{REQ_HEADER}{ROLLERSHADE_COMMAND}05{CONTROL_SOURCE}0064", |
| 22 | +] |
| 23 | +POSITION_KEYS = [ |
| 24 | + f"{REQ_HEADER}{ROLLERSHADE_COMMAND}01{CONTROL_SOURCE}01", |
| 25 | + f"{REQ_HEADER}{ROLLERSHADE_COMMAND}05{CONTROL_SOURCE}", |
| 26 | +] # +actual_position |
| 27 | +STOP_KEYS = [f"{REQ_HEADER}{ROLLERSHADE_COMMAND}00{CONTROL_SOURCE}01"] |
| 28 | + |
| 29 | + |
| 30 | +class SwitchbotRollerShade(SwitchbotBaseCover, SwitchbotSequenceDevice): |
| 31 | + """Representation of a Switchbot Roller Shade.""" |
| 32 | + |
| 33 | + def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 34 | + """Switchbot roller shade constructor.""" |
| 35 | + # The position of the roller shade is saved returned with 0 = open and 100 = closed. |
| 36 | + # the definition of position is the same as in Home Assistant. |
| 37 | + |
| 38 | + self._reverse: bool = kwargs.pop("reverse_mode", True) |
| 39 | + super().__init__(self._reverse, *args, **kwargs) |
| 40 | + |
| 41 | + def _set_parsed_data( |
| 42 | + self, advertisement: SwitchBotAdvertisement, data: dict[str, Any] |
| 43 | + ) -> None: |
| 44 | + """Set data.""" |
| 45 | + in_motion = data["inMotion"] |
| 46 | + previous_position = self._get_adv_value("position") |
| 47 | + new_position = data["position"] |
| 48 | + self._update_motion_direction(in_motion, previous_position, new_position) |
| 49 | + super()._set_parsed_data(advertisement, data) |
| 50 | + |
| 51 | + @update_after_operation |
| 52 | + async def open(self, mode: int = 0) -> bool: |
| 53 | + """Send open command. 0 - performance mode, 1 - unfelt mode.""" |
| 54 | + self._is_opening = True |
| 55 | + self._is_closing = False |
| 56 | + return await self._send_multiple_commands(OPEN_KEYS) |
| 57 | + |
| 58 | + @update_after_operation |
| 59 | + async def close(self, speed: int = 0) -> bool: |
| 60 | + """Send close command. 0 - performance mode, 1 - unfelt mode.""" |
| 61 | + self._is_closing = True |
| 62 | + self._is_opening = False |
| 63 | + return await self._send_multiple_commands(CLOSE_KEYS) |
| 64 | + |
| 65 | + @update_after_operation |
| 66 | + async def stop(self) -> bool: |
| 67 | + """Send stop command to device.""" |
| 68 | + self._is_opening = self._is_closing = False |
| 69 | + return await self._send_multiple_commands(STOP_KEYS) |
| 70 | + |
| 71 | + @update_after_operation |
| 72 | + async def set_position(self, position: int, mode: int = 0) -> bool: |
| 73 | + """Send position command (0-100) to device. 0 - performance mode, 1 - unfelt mode.""" |
| 74 | + position = (100 - position) if self._reverse else position |
| 75 | + self._update_motion_direction(True, self._get_adv_value("position"), position) |
| 76 | + return await self._send_multiple_commands( |
| 77 | + [ |
| 78 | + f"{POSITION_KEYS[0]}{position:02X}", |
| 79 | + f"{POSITION_KEYS[1]}{mode:02X}{position:02X}", |
| 80 | + ] |
| 81 | + ) |
| 82 | + |
| 83 | + def get_position(self) -> Any: |
| 84 | + """Return cached position (0-100) of Curtain.""" |
| 85 | + # To get actual position call update() first. |
| 86 | + return self._get_adv_value("position") |
| 87 | + |
| 88 | + async def get_basic_info(self) -> dict[str, Any] | None: |
| 89 | + """Get device basic settings.""" |
| 90 | + if not (_data := await self._get_basic_info()): |
| 91 | + return None |
| 92 | + |
| 93 | + _position = max(min(_data[5], 100), 0) |
| 94 | + _direction_adjusted_position = (100 - _position) if self._reverse else _position |
| 95 | + _previous_position = self._get_adv_value("position") |
| 96 | + _in_motion = bool(_data[4] & 0b00000011) |
| 97 | + self._update_motion_direction( |
| 98 | + _in_motion, _previous_position, _direction_adjusted_position |
| 99 | + ) |
| 100 | + |
| 101 | + return { |
| 102 | + "battery": _data[1], |
| 103 | + "firmware": _data[2] / 10.0, |
| 104 | + "chainLength": _data[3], |
| 105 | + "openDirection": ( |
| 106 | + "clockwise" if _data[4] & 0b10000000 == 128 else "anticlockwise" |
| 107 | + ), |
| 108 | + "fault": bool(_data[4] & 0b00010000), |
| 109 | + "solarPanel": bool(_data[4] & 0b00001000), |
| 110 | + "calibration": bool(_data[4] & 0b00000100), |
| 111 | + "calibrated": bool(_data[4] & 0b00000100), |
| 112 | + "inMotion": _in_motion, |
| 113 | + "position": _direction_adjusted_position, |
| 114 | + "timers": _data[6], |
| 115 | + } |
| 116 | + |
| 117 | + def _update_motion_direction( |
| 118 | + self, in_motion: bool, previous_position: int | None, new_position: int |
| 119 | + ) -> None: |
| 120 | + """Update opening/closing status based on movement.""" |
| 121 | + if previous_position is None: |
| 122 | + return |
| 123 | + if in_motion is False: |
| 124 | + self._is_closing = self._is_opening = False |
| 125 | + return |
| 126 | + |
| 127 | + if new_position != previous_position: |
| 128 | + self._is_opening = new_position > previous_position |
| 129 | + self._is_closing = new_position < previous_position |
0 commit comments