about summary refs log tree commit diff
path: root/broadlink/__init__.py
diff options
context:
space:
mode:
authorVictor Ferrer <victorfergar@gmail.com>2016-12-04T00·50+0100
committerMatthew Garrett <mjg59@coreos.com>2016-12-25T10·59+0000
commitd519623dd4507d3648bd361b4507171b98d330df (patch)
tree9bfa4d3e40c6e1aff71315f986bb6c8e997f08d5 /broadlink/__init__.py
parent83f1c3fc93344c0264223fbf37ac0198626e006b (diff)
Add new device support: Broadlink MP1 Smart power strip
Diffstat (limited to 'broadlink/__init__.py')
-rwxr-xr-xbroadlink/__init__.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py
index f4bb1069caa1..1ebaf9e50f1a 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)