Skip to content

Commit 71a3a21

Browse files
authored
Merge pull request #50 from kzosabe/add-plugs-and-strip-lights
Add new devices(Plug Mini, Strip Light)
2 parents 67f0c43 + d7dfb4b commit 71a3a21

3 files changed

Lines changed: 221 additions & 2 deletions

File tree

switchbot_client/devices/physical.py

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
MeterDeviceStatus,
1313
MotionSensorDeviceStatus,
1414
PlugDeviceStatus,
15+
PlugMiniJpDeviceStatus,
16+
PlugMiniUsDeviceStatus,
1517
SmartFanDeviceStatus,
18+
StripLightDeviceStatus,
1619
)
1720
from switchbot_client.enums import ControlCommand, DeviceType
1821
from switchbot_client.types import APIPhysicalDeviceObject
@@ -48,6 +51,10 @@ def create_by_api_object( # noqa
4851
return Bot(client, device)
4952
if device_type == DeviceType.PLUG:
5053
return Plug(client, device)
54+
if device_type == DeviceType.PLUG_MINI_US:
55+
return PlugMiniUs(client, device)
56+
if device_type == DeviceType.PLUG_MINI_JP:
57+
return PlugMiniJp(client, device)
5158
if device_type == DeviceType.CURTAIN:
5259
return Curtain(client, device)
5360
if device_type == DeviceType.METER:
@@ -62,6 +69,8 @@ def create_by_api_object( # noqa
6269
return Humidifier(client, device)
6370
if device_type == DeviceType.SMART_FAN:
6471
return SmartFan(client, device)
72+
if device_type == DeviceType.STRIP_LIGHT:
73+
return StripLight(client, device)
6574
if device_type == DeviceType.INDOOR_CAM:
6675
return IndoorCam(client, device)
6776
if device_type == DeviceType.REMOTE:
@@ -198,6 +207,106 @@ def turn_off(self) -> SwitchBotCommandResult:
198207
return self.command(ControlCommand.Plug.TURN_OFF)
199208

200209

210+
class PlugMiniUs(SwitchBotPhysicalDevice):
211+
def __init__(self, client: SwitchBotClient, device: APIPhysicalDeviceObject):
212+
super().__init__(client, device)
213+
self._check_device_type(DeviceType.PLUG_MINI_US)
214+
215+
@staticmethod
216+
def create_by_id(client: SwitchBotClient, device_id: str) -> PlugMiniUs:
217+
device = SwitchBotPhysicalDevice.get_device_by_id(client, device_id)
218+
return PlugMiniUs(client, device)
219+
220+
def status(self) -> PlugMiniUsDeviceStatus:
221+
status = super().status()
222+
return PlugMiniUsDeviceStatus(
223+
device_id=status.device_id,
224+
device_type=status.device_type,
225+
device_name=status.device_name,
226+
hub_device_id=status.hub_device_id,
227+
raw_data=status.raw_data,
228+
power=status.raw_data["power"],
229+
voltage=status.raw_data["voltage"],
230+
weight=status.raw_data["weight"],
231+
electricity_of_day=status.raw_data["electricityOfDay"],
232+
electric_current=status.raw_data["electricCurrent"],
233+
)
234+
235+
def power(self) -> str:
236+
return self.status().power
237+
238+
def voltage(self) -> int:
239+
return self.status().voltage
240+
241+
def weight(self) -> int:
242+
return self.status().weight
243+
244+
def electricity_of_day(self) -> int:
245+
return self.status().electricity_of_day
246+
247+
def electric_current(self) -> int:
248+
return self.status().electric_current
249+
250+
def turn_on(self) -> SwitchBotCommandResult:
251+
return self.command(ControlCommand.PlugMiniUs.TURN_ON)
252+
253+
def turn_off(self) -> SwitchBotCommandResult:
254+
return self.command(ControlCommand.PlugMiniUs.TURN_OFF)
255+
256+
def toggle(self) -> SwitchBotCommandResult:
257+
return self.command(ControlCommand.PlugMiniUs.TOGGLE)
258+
259+
260+
class PlugMiniJp(SwitchBotPhysicalDevice):
261+
def __init__(self, client: SwitchBotClient, device: APIPhysicalDeviceObject):
262+
super().__init__(client, device)
263+
self._check_device_type(DeviceType.PLUG_MINI_JP)
264+
265+
@staticmethod
266+
def create_by_id(client: SwitchBotClient, device_id: str) -> PlugMiniJp:
267+
device = SwitchBotPhysicalDevice.get_device_by_id(client, device_id)
268+
return PlugMiniJp(client, device)
269+
270+
def status(self) -> PlugMiniJpDeviceStatus:
271+
status = super().status()
272+
return PlugMiniJpDeviceStatus(
273+
device_id=status.device_id,
274+
device_type=status.device_type,
275+
device_name=status.device_name,
276+
hub_device_id=status.hub_device_id,
277+
raw_data=status.raw_data,
278+
power=status.raw_data["power"],
279+
voltage=status.raw_data["voltage"],
280+
weight=status.raw_data["weight"],
281+
electricity_of_day=status.raw_data["electricityOfDay"],
282+
electric_current=status.raw_data["electricCurrent"],
283+
)
284+
285+
def power(self) -> str:
286+
return self.status().power
287+
288+
def voltage(self) -> int:
289+
return self.status().voltage
290+
291+
def weight(self) -> int:
292+
return self.status().weight
293+
294+
def electricity_of_day(self) -> int:
295+
return self.status().electricity_of_day
296+
297+
def electric_current(self) -> int:
298+
return self.status().electric_current
299+
300+
def turn_on(self) -> SwitchBotCommandResult:
301+
return self.command(ControlCommand.PlugMiniJp.TURN_ON)
302+
303+
def turn_off(self) -> SwitchBotCommandResult:
304+
return self.command(ControlCommand.PlugMiniJp.TURN_OFF)
305+
306+
def toggle(self) -> SwitchBotCommandResult:
307+
return self.command(ControlCommand.PlugMiniJp.TOGGLE)
308+
309+
201310
class Curtain(SwitchBotPhysicalDevice):
202311
class Parameters:
203312
MODE_PERFORMANCE = "0"
@@ -388,10 +497,10 @@ def color_temperature(self) -> int:
388497
return self.status().color_temperature
389498

390499
def turn_on(self) -> SwitchBotCommandResult:
391-
return self.command(ControlCommand.Humidifier.TURN_ON)
500+
return self.command(ControlCommand.ColorBulb.TURN_ON)
392501

393502
def turn_off(self) -> SwitchBotCommandResult:
394-
return self.command(ControlCommand.Humidifier.TURN_OFF)
503+
return self.command(ControlCommand.ColorBulb.TURN_OFF)
395504

396505
def set_brightness(self, brightness: int) -> SwitchBotCommandResult:
397506
"""
@@ -598,6 +707,71 @@ def set_shake_range(self, shake_range: int) -> SwitchBotCommandResult:
598707
)
599708

600709

710+
class StripLight(SwitchBotPhysicalDevice):
711+
def __init__(self, client: SwitchBotClient, device: APIPhysicalDeviceObject):
712+
super().__init__(client, device)
713+
self._check_device_type(DeviceType.STRIP_LIGHT)
714+
715+
@staticmethod
716+
def create_by_id(client: SwitchBotClient, device_id: str) -> StripLight:
717+
device = SwitchBotPhysicalDevice.get_device_by_id(client, device_id)
718+
return StripLight(client, device)
719+
720+
def status(self) -> StripLightDeviceStatus:
721+
status = super().status()
722+
colors = [int(i) for i in status.raw_data["color"].split(":")]
723+
color_hex = f"#{colors[0]:02x}{colors[1]:02x}{colors[2]:02x}"
724+
return StripLightDeviceStatus(
725+
device_id=status.device_id,
726+
device_type=status.device_type,
727+
device_name=status.device_name,
728+
hub_device_id=status.hub_device_id,
729+
raw_data=status.raw_data,
730+
power=status.raw_data["power"],
731+
color_hex=color_hex,
732+
brightness=status.raw_data["brightness"],
733+
)
734+
735+
def power(self) -> str:
736+
return self.status().power
737+
738+
def brightness(self) -> int:
739+
return self.status().brightness
740+
741+
def color_hex(self) -> str:
742+
"""
743+
returns #rrggbb format color string
744+
"""
745+
return self.status().color_hex
746+
747+
def turn_on(self) -> SwitchBotCommandResult:
748+
return self.command(ControlCommand.StripLight.TURN_ON)
749+
750+
def turn_off(self) -> SwitchBotCommandResult:
751+
return self.command(ControlCommand.StripLight.TURN_OFF)
752+
753+
def toggle(self) -> SwitchBotCommandResult:
754+
return self.command(ControlCommand.StripLight.TOGGLE)
755+
756+
def set_brightness(self, brightness: int) -> SwitchBotCommandResult:
757+
"""
758+
brightness: 1 ~ 100
759+
"""
760+
return self.command(ControlCommand.StripLight.SET_BRIGHTNESS, parameter=f"{brightness}")
761+
762+
def set_color_by_number(self, red: int, green: int, blue: int) -> SwitchBotCommandResult:
763+
"""
764+
red: 0 ~ 255
765+
green: 0 ~ 255
766+
blue: 0 ~ 255
767+
"""
768+
return self.command(ControlCommand.StripLight.SET_COLOR, parameter=f"{red}:{green}:{blue}")
769+
770+
def set_color(self, color_hex: str) -> SwitchBotCommandResult:
771+
rgb = tuple(int(color_hex.lstrip("#")[i : i + 2], 16) for i in (0, 2, 4))
772+
return self.set_color_by_number(rgb[0], rgb[1], rgb[2])
773+
774+
601775
class IndoorCam(SwitchBotPhysicalDevice):
602776
def __init__(self, client: SwitchBotClient, device: APIPhysicalDeviceObject):
603777
super().__init__(client, device)

switchbot_client/devices/status.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ class PlugDeviceStatus(DeviceStatus):
3333
power: str
3434

3535

36+
@dataclass()
37+
class PlugMiniUsDeviceStatus(DeviceStatus):
38+
power: str
39+
voltage: int
40+
weight: int
41+
electricity_of_day: int
42+
electric_current: int
43+
44+
45+
@dataclass()
46+
class PlugMiniJpDeviceStatus(DeviceStatus):
47+
power: str
48+
voltage: int
49+
weight: int
50+
electricity_of_day: int
51+
electric_current: int
52+
53+
3654
@dataclass()
3755
class CurtainDeviceStatus(DeviceStatus):
3856
is_calibrated: bool
@@ -70,6 +88,13 @@ class SmartFanDeviceStatus(DeviceStatus):
7088
shake_range: int
7189

7290

91+
@dataclass()
92+
class StripLightDeviceStatus(DeviceStatus):
93+
power: str
94+
color_hex: str
95+
brightness: int
96+
97+
7398
@dataclass()
7499
class MeterDeviceStatus(DeviceStatus):
75100
humidity: int

switchbot_client/enums.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ class DeviceType:
55
BOT = "Bot"
66
CURTAIN = "Curtain"
77
PLUG = "Plug"
8+
PLUG_MINI_US = "Plug Mini (US)"
9+
PLUG_MINI_JP = "Plug Mini (JP)"
810
METER = "Meter"
911
MOTION_SENSOR = "Motion Sensor"
1012
CONTACT_SENSOR = "Contact Sensor"
1113
COLOR_BULB = "Color Bulb"
1214
HUMIDIFIER = "Humidifier"
1315
SMART_FAN = "Smart Fan"
16+
STRIP_LIGHT = "Strip Light"
1417
INDOOR_CAM = "Indoor Cam"
1518
REMOTE = "Remote" # undocumented in official api reference?
1619

@@ -42,6 +45,16 @@ class Plug:
4245
TURN_ON = "turnOn"
4346
TURN_OFF = "turnOff"
4447

48+
class PlugMiniUs:
49+
TURN_ON = "turnOn"
50+
TURN_OFF = "turnOff"
51+
TOGGLE = "toggle"
52+
53+
class PlugMiniJp:
54+
TURN_ON = "turnOn"
55+
TURN_OFF = "turnOff"
56+
TOGGLE = "toggle"
57+
4558
class Curtain:
4659
TURN_ON = "turnOn"
4760
TURN_OFF = "turnOff"
@@ -65,6 +78,13 @@ class SmartFan:
6578
TURN_OFF = "turnOff"
6679
SET_ALL_STATUS = "setAllStatus"
6780

81+
class StripLight:
82+
TURN_ON = "turnOn"
83+
TURN_OFF = "turnOff"
84+
TOGGLE = "toggle"
85+
SET_BRIGHTNESS = "setBrightness"
86+
SET_COLOR = "setColor"
87+
6888
class VirtualInfrared:
6989
TURN_ON = "turnOn"
7090
TURN_OFF = "turnOff"

0 commit comments

Comments
 (0)