1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-21 06:40:12 +01:00

Add new device support: Broadlink MP1 Smart power strip

This commit is contained in:
Victor Ferrer 2016-12-04 01:50:46 +01:00 committed by Matthew Garrett
parent 83f1c3fc93
commit d519623dd4
3 changed files with 72 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.pyc

View File

@ -53,3 +53,13 @@ Check power state on a SmartPlug:
```
state = devices[0].check_power()
```
Set power state for S1 on a SmartPowerStrip MP1:
```
devices[0].set_power(1, True)
```
Check power state on a SmartPowerStrip:
```
state = devices[0].check_power()
```

View File

@ -45,6 +45,8 @@ def gendevice(devtype, host, mac):
return rm(host=host, mac=mac)
elif devtype == 0x2714: # A1
return a1(host=host, mac=mac)
elif devtype == 0x4EB5: # MP1
return mp1(host=host, mac=mac)
else:
return device(host=host, mac=mac)
@ -241,6 +243,65 @@ class device:
return bytearray(response[0])
class mp1(device):
def __init__ (self, host, mac):
device.__init__(self, host, mac)
self.type = "MP1"
def set_power_mask(self, sid_mask, state):
"""Sets the power state of the smart power strip."""
packet = bytearray(16)
packet[0x00] = 0x0d
packet[0x02] = 0xa5
packet[0x03] = 0xa5
packet[0x04] = 0x5a
packet[0x05] = 0x5a
packet[0x06] = 0xb2 + ((sid_mask<<1) if state else sid_mask)
packet[0x07] = 0xc0
packet[0x08] = 0x02
packet[0x0a] = 0x03
packet[0x0d] = sid_mask
packet[0x0e] = sid_mask if state else 0
response = self.send_packet(0x6a, packet)
err = response[0x22] | (response[0x23] << 8)
def set_power(self, sid, state):
"""Sets the power state of the smart power strip."""
sid_mask = 0x01 << (sid - 1)
return self.set_power_mask(sid_mask, state)
def check_power(self):
"""Returns the power state of the smart power strip."""
packet = bytearray(16)
packet[0x00] = 0x0a
packet[0x02] = 0xa5
packet[0x03] = 0xa5
packet[0x04] = 0x5a
packet[0x05] = 0x5a
packet[0x06] = 0xae
packet[0x07] = 0xc0
packet[0x08] = 0x01
response = self.send_packet(0x6a, packet)
err = response[0x22] | (response[0x23] << 8)
if err == 0:
aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
payload = aes.decrypt(bytes(response[0x38:]))
if type(payload[0x4]) == int:
state = payload[0x0e]
else:
state = ord(payload[0x0e])
data = {}
data['s1'] = bool(state & 0x01)
data['s2'] = bool(state & 0x02)
data['s3'] = bool(state & 0x04)
data['s4'] = bool(state & 0x08)
return data
class sp1(device):
def __init__ (self, host, mac):
device.__init__(self, host, mac)