From d519623dd4507d3648bd361b4507171b98d330df Mon Sep 17 00:00:00 2001 From: Victor Ferrer Date: Sun, 4 Dec 2016 01:50:46 +0100 Subject: [PATCH] Add new device support: Broadlink MP1 Smart power strip --- .gitignore | 1 + README.md | 10 +++++++ broadlink/__init__.py | 61 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..0d20b6487 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/README.md b/README.md index 47a4691f5..3d7598f5d 100644 --- a/README.md +++ b/README.md @@ -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() +``` \ No newline at end of file diff --git a/broadlink/__init__.py b/broadlink/__init__.py index f4bb1069c..1ebaf9e50 100755 --- a/broadlink/__init__.py +++ b/broadlink/__init__.py @@ -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)