Skip to content

Commit

Permalink
Add new device support: Broadlink MP1 Smart power strip
Browse files Browse the repository at this point in the history
  • Loading branch information
vicfergar authored and Matthew Garrett committed Dec 25, 2016
1 parent 83f1c3f commit d519623
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
```
61 changes: 61 additions & 0 deletions broadlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -241,6 +243,65 @@ def send_packet(self, command, payload):
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)
Expand Down

0 comments on commit d519623

Please sign in to comment.