|
| 1 | +from switchbot_client import SwitchBotAPIClient, SwitchBotAPIResponse, ControlCommand |
| 2 | + |
| 3 | + |
| 4 | +class SwitchBotDevice: |
| 5 | + def __init__(self, client: SwitchBotAPIClient, device_id: str): |
| 6 | + if client is None: |
| 7 | + raise TypeError |
| 8 | + self.client = client |
| 9 | + if device_id is None: |
| 10 | + raise TypeError |
| 11 | + self.device_id = device_id |
| 12 | + |
| 13 | + def check_device_type(self, expected_device_type: str): |
| 14 | + status = self.client.devices_status(self.device_id) |
| 15 | + actual_device_type = status.body["deviceType"] |
| 16 | + if actual_device_type != expected_device_type: |
| 17 | + raise RuntimeError( |
| 18 | + f"Illegal device type. " |
| 19 | + f"expected: {expected_device_type}, actual: {actual_device_type}" |
| 20 | + ) |
| 21 | + |
| 22 | + def check_device_type_for_virtual_infrared(self, expected_device_type: str): |
| 23 | + infrared_remote_devices = self.client.devices().body["infraredRemoteList"] |
| 24 | + for device in infrared_remote_devices: |
| 25 | + if device["deviceId"] == self.device_id: |
| 26 | + actual_device_type = device["remoteType"] |
| 27 | + if actual_device_type == expected_device_type: |
| 28 | + return |
| 29 | + raise RuntimeError( |
| 30 | + f"Illegal device type. " |
| 31 | + f"expected: {expected_device_type}, actual: {actual_device_type}" |
| 32 | + ) |
| 33 | + raise RuntimeError(f"device not found: {self.device_id}") |
| 34 | + |
| 35 | + def status(self) -> SwitchBotAPIResponse: |
| 36 | + return self.client.devices_status(self.device_id) |
| 37 | + |
| 38 | + def control( |
| 39 | + self, command: str, parameter: str = None, command_type: str = None |
| 40 | + ) -> SwitchBotAPIResponse: |
| 41 | + return self.client.devices_control(self.device_id, command, parameter, command_type) |
| 42 | + |
| 43 | + |
| 44 | +class Bot(SwitchBotDevice): |
| 45 | + def __init__(self, client: SwitchBotAPIClient, device_id: str): |
| 46 | + super().__init__(client, device_id) |
| 47 | + self.check_device_type("Bot") |
| 48 | + |
| 49 | + def turn_on(self) -> SwitchBotAPIResponse: |
| 50 | + return self.control(ControlCommand.Bot.TURN_ON) |
| 51 | + |
| 52 | + def turn_off(self) -> SwitchBotAPIResponse: |
| 53 | + return self.control(ControlCommand.Bot.TURN_OFF) |
| 54 | + |
| 55 | + def press(self) -> SwitchBotAPIResponse: |
| 56 | + return self.control(ControlCommand.Bot.PRESS) |
| 57 | + |
| 58 | + |
| 59 | +class Plug(SwitchBotDevice): |
| 60 | + def __init__(self, client: SwitchBotAPIClient, device_id: str): |
| 61 | + super().__init__(client, device_id) |
| 62 | + self.check_device_type("Plug") |
| 63 | + |
| 64 | + def turn_on(self) -> SwitchBotAPIResponse: |
| 65 | + return self.control(ControlCommand.Plug.TURN_ON) |
| 66 | + |
| 67 | + def turn_off(self) -> SwitchBotAPIResponse: |
| 68 | + return self.control(ControlCommand.Plug.TURN_OFF) |
| 69 | + |
| 70 | + |
| 71 | +class Curtain(SwitchBotDevice): |
| 72 | + class Parameters: |
| 73 | + MODE_PERFORMANCE = "0" |
| 74 | + MODE_SILENT = "1" |
| 75 | + MODE_DEFAULT = "ff" |
| 76 | + |
| 77 | + def __init__(self, client: SwitchBotAPIClient, device_id: str): |
| 78 | + super().__init__(client, device_id) |
| 79 | + self.check_device_type("Curtain") |
| 80 | + |
| 81 | + def turn_on(self) -> SwitchBotAPIResponse: |
| 82 | + return self.control(ControlCommand.Curtain.TURN_ON) |
| 83 | + |
| 84 | + def turn_off(self) -> SwitchBotAPIResponse: |
| 85 | + return self.control(ControlCommand.Curtain.TURN_OFF) |
| 86 | + |
| 87 | + def set_position(self, index: int, mode: str, position: int) -> SwitchBotAPIResponse: |
| 88 | + """ |
| 89 | + mode(Parameters.MODE_XXX): 0 (Performance Mode), 1 (Silent Mode), ff (default mode) |
| 90 | + position: 0 ~ 100 (0 means opened, 100 means closed) |
| 91 | + """ |
| 92 | + return self.control( |
| 93 | + ControlCommand.Curtain.SET_POSITION, parameter=f"{index},{mode},{position}" |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +class Humidifier(SwitchBotDevice): |
| 98 | + def __init__(self, client: SwitchBotAPIClient, device_id: str): |
| 99 | + super().__init__(client, device_id) |
| 100 | + self.check_device_type("Humidifier") |
| 101 | + |
| 102 | + def turn_on(self) -> SwitchBotAPIResponse: |
| 103 | + return self.control(ControlCommand.Humidifier.TURN_ON) |
| 104 | + |
| 105 | + def turn_off(self) -> SwitchBotAPIResponse: |
| 106 | + return self.control(ControlCommand.Humidifier.TURN_OFF) |
| 107 | + |
| 108 | + def set_mode(self, mode: str) -> SwitchBotAPIResponse: |
| 109 | + """ |
| 110 | + auto or 101 or 102 or 103 or {0~100} |
| 111 | + auto: set to Auto Mode |
| 112 | + 101: set atomization efficiency to 34% |
| 113 | + 102: set atomization efficiency to 67% |
| 114 | + 103: set atomization efficiency to 100% |
| 115 | + """ |
| 116 | + return self.control(ControlCommand.Humidifier.SET_MODE, parameter=mode) |
| 117 | + |
| 118 | + |
| 119 | +class SmartFan(SwitchBotDevice): |
| 120 | + class Parameters: |
| 121 | + POWER_ON = "on" |
| 122 | + POWER_OFF = "off" |
| 123 | + FAN_MODE_STANDARD = 1 |
| 124 | + FAN_MODE_NATURAL = 2 |
| 125 | + |
| 126 | + def __init__(self, client: SwitchBotAPIClient, device_id: str): |
| 127 | + super().__init__(client, device_id) |
| 128 | + self.check_device_type("SmartFan") |
| 129 | + |
| 130 | + def turn_on(self) -> SwitchBotAPIResponse: |
| 131 | + return self.control(ControlCommand.SmartFan.TURN_ON) |
| 132 | + |
| 133 | + def turn_off(self) -> SwitchBotAPIResponse: |
| 134 | + return self.control(ControlCommand.SmartFan.TURN_OFF) |
| 135 | + |
| 136 | + def set_all_status( |
| 137 | + self, power: str, fan_mode: int, fan_speed: int, shake_range: int |
| 138 | + ) -> SwitchBotAPIResponse: |
| 139 | + """ |
| 140 | + power(Parameters.POWER_XXX): off, on |
| 141 | + fan_mode(Parameters.FAN_MODE_XXX): 1 (Standard), 2 (Natural) |
| 142 | + fan_speed: 1, 2, 3, 4 |
| 143 | + shake_range: 0 ~ 120 |
| 144 | + """ |
| 145 | + return self.control( |
| 146 | + ControlCommand.SmartFan.SET_ALL_STATUS, |
| 147 | + parameter=f"{power},{fan_mode},{fan_speed},{shake_range}", |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +class AirConditioner(SwitchBotDevice): |
| 152 | + class Parameters: |
| 153 | + MODE_AUTO = 1 |
| 154 | + MODE_COOL = 2 |
| 155 | + MODE_DRY = 3 |
| 156 | + MODE_FAN = 4 |
| 157 | + MODE_HEAT = 5 |
| 158 | + FAN_SPEED_AUTO = 1 |
| 159 | + FAN_SPEED_LOW = 2 |
| 160 | + FAN_SPEED_MEDIUM = 3 |
| 161 | + FAN_SPEED_HIGH = 4 |
| 162 | + POWER_ON = "on" |
| 163 | + POWER_OFF = "off" |
| 164 | + |
| 165 | + def __init__(self, client: SwitchBotAPIClient, device_id: str): |
| 166 | + super().__init__(client, device_id) |
| 167 | + self.check_device_type_for_virtual_infrared("Air Conditioner") |
| 168 | + |
| 169 | + def turn_on(self) -> SwitchBotAPIResponse: |
| 170 | + return self.control(ControlCommand.VirtualInfrared.TURN_ON) |
| 171 | + |
| 172 | + def turn_off(self) -> SwitchBotAPIResponse: |
| 173 | + return self.control(ControlCommand.VirtualInfrared.TURN_OFF) |
| 174 | + |
| 175 | + def set_all( |
| 176 | + self, temperature: int, mode: str, fan_speed: str, power: str |
| 177 | + ) -> SwitchBotAPIResponse: |
| 178 | + """ |
| 179 | + temperature: temperature in celsius |
| 180 | + mode(Parameters.MODE_XXX): 1(auto), 2(cool), 3(dry), 4(fan), 5(heat) |
| 181 | + fan_speed(Parameters.FAN_SPEED_XXX): 1(auto), 2(low), 3(medium), 4(high); |
| 182 | + power(Parameters.POWER_XXX): on, off |
| 183 | + """ |
| 184 | + return self.control( |
| 185 | + ControlCommand.VirtualInfrared.SET_ALL, |
| 186 | + parameter=f"{temperature},{mode},{fan_speed},{power}", |
| 187 | + ) |
| 188 | + |
| 189 | + |
| 190 | +class Light(SwitchBotDevice): |
| 191 | + def __init__(self, client: SwitchBotAPIClient, device_id: str): |
| 192 | + super().__init__(client, device_id) |
| 193 | + self.check_device_type_for_virtual_infrared("Light") |
| 194 | + |
| 195 | + def turn_on(self) -> SwitchBotAPIResponse: |
| 196 | + return self.control(ControlCommand.VirtualInfrared.TURN_ON) |
| 197 | + |
| 198 | + def turn_off(self) -> SwitchBotAPIResponse: |
| 199 | + return self.control(ControlCommand.VirtualInfrared.TURN_OFF) |
| 200 | + |
| 201 | + def brightness_up(self) -> SwitchBotAPIResponse: |
| 202 | + return self.control(ControlCommand.VirtualInfrared.BRIGHTNESS_UP) |
| 203 | + |
| 204 | + def brightness_down(self) -> SwitchBotAPIResponse: |
| 205 | + return self.control(ControlCommand.VirtualInfrared.BRIGHTNESS_DOWN) |
0 commit comments