|
| 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 ..const.fan import FanMode |
| 9 | +from .device import ( |
| 10 | + DEVICE_GET_BASIC_SETTINGS_KEY, |
| 11 | + SwitchbotSequenceDevice, |
| 12 | + update_after_operation, |
| 13 | +) |
| 14 | + |
| 15 | +_LOGGER = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +COMMAND_HEAD = "570f41" |
| 19 | +COMMAND_TURN_ON = f"{COMMAND_HEAD}0101" |
| 20 | +COMMAND_TURN_OFF = f"{COMMAND_HEAD}0102" |
| 21 | +COMMAND_START_OSCILLATION = f"{COMMAND_HEAD}020101ff" |
| 22 | +COMMAND_STOP_OSCILLATION = f"{COMMAND_HEAD}020102ff" |
| 23 | +COMMAND_SET_MODE = { |
| 24 | + FanMode.NORMAL.name: f"{COMMAND_HEAD}030101ff", |
| 25 | + FanMode.NATURAL.name: f"{COMMAND_HEAD}030102ff", |
| 26 | + FanMode.SLEEP.name: f"{COMMAND_HEAD}030103", |
| 27 | + FanMode.BABY.name: f"{COMMAND_HEAD}030104", |
| 28 | +} |
| 29 | +COMMAND_SET_PERCENTAGE = f"{COMMAND_HEAD}0302" # +speed |
| 30 | +COMMAND_GET_BASIC_INFO = "570f428102" |
| 31 | + |
| 32 | + |
| 33 | +class SwitchbotFan(SwitchbotSequenceDevice): |
| 34 | + """Representation of a Switchbot Circulator Fan.""" |
| 35 | + |
| 36 | + def __init__(self, device, password=None, interface=0, **kwargs): |
| 37 | + super().__init__(device, password, interface, **kwargs) |
| 38 | + |
| 39 | + async def get_basic_info(self) -> dict[str, Any] | None: |
| 40 | + """Get device basic settings.""" |
| 41 | + if not (_data := await self._get_basic_info(COMMAND_GET_BASIC_INFO)): |
| 42 | + return None |
| 43 | + if not (_data1 := await self._get_basic_info(DEVICE_GET_BASIC_SETTINGS_KEY)): |
| 44 | + return None |
| 45 | + |
| 46 | + _LOGGER.debug("data: %s", _data) |
| 47 | + battery = _data[2] & 0b01111111 |
| 48 | + isOn = bool(_data[3] & 0b10000000) |
| 49 | + oscillating = bool(_data[3] & 0b01100000) |
| 50 | + _mode = _data[8] & 0b00000111 |
| 51 | + mode = FanMode(_mode).name if 1 <= _mode <= 4 else None |
| 52 | + speed = _data[9] |
| 53 | + firmware = _data1[2] / 10.0 |
| 54 | + |
| 55 | + return { |
| 56 | + "battery": battery, |
| 57 | + "isOn": isOn, |
| 58 | + "oscillating": oscillating, |
| 59 | + "mode": mode, |
| 60 | + "speed": speed, |
| 61 | + "firmware": firmware, |
| 62 | + } |
| 63 | + |
| 64 | + async def _get_basic_info(self, cmd: str) -> bytes | None: |
| 65 | + """Return basic info of device.""" |
| 66 | + _data = await self._send_command(key=cmd, retry=self._retry_count) |
| 67 | + |
| 68 | + if _data in (b"\x07", b"\x00"): |
| 69 | + _LOGGER.error("Unsuccessful, please try again") |
| 70 | + return None |
| 71 | + |
| 72 | + return _data |
| 73 | + |
| 74 | + @update_after_operation |
| 75 | + async def set_preset_mode(self, preset_mode: str) -> bool: |
| 76 | + """Send command to set fan preset_mode.""" |
| 77 | + return await self._send_command(COMMAND_SET_MODE[preset_mode]) |
| 78 | + |
| 79 | + @update_after_operation |
| 80 | + async def set_percentage(self, percentage: int) -> bool: |
| 81 | + """Send command to set fan percentage.""" |
| 82 | + return await self._send_command(f"{COMMAND_SET_PERCENTAGE}{percentage:02X}") |
| 83 | + |
| 84 | + @update_after_operation |
| 85 | + async def set_oscillation(self, oscillating: bool) -> bool: |
| 86 | + """Send command to set fan oscillation""" |
| 87 | + if oscillating: |
| 88 | + return await self._send_command(COMMAND_START_OSCILLATION) |
| 89 | + else: |
| 90 | + return await self._send_command(COMMAND_STOP_OSCILLATION) |
| 91 | + |
| 92 | + @update_after_operation |
| 93 | + async def turn_on(self) -> bool: |
| 94 | + """Turn on the fan.""" |
| 95 | + return await self._send_command(COMMAND_TURN_ON) |
| 96 | + |
| 97 | + @update_after_operation |
| 98 | + async def turn_off(self) -> bool: |
| 99 | + """Turn off the fan.""" |
| 100 | + return await self._send_command(COMMAND_TURN_OFF) |
| 101 | + |
| 102 | + def get_current_percentage(self) -> Any: |
| 103 | + """Return cached percentage.""" |
| 104 | + return self._get_adv_value("speed") |
| 105 | + |
| 106 | + def is_on(self) -> bool | None: |
| 107 | + """Return fan state from cache.""" |
| 108 | + return self._get_adv_value("isOn") |
| 109 | + |
| 110 | + def get_oscillating_state(self) -> Any: |
| 111 | + """Return cached oscillating.""" |
| 112 | + return self._get_adv_value("oscillating") |
| 113 | + |
| 114 | + def get_current_mode(self) -> Any: |
| 115 | + """Return cached mode.""" |
| 116 | + return self._get_adv_value("mode") |
0 commit comments