about summary refs log tree commit diff
path: root/broadlink/__init__.py
diff options
context:
space:
mode:
authorjazzina <jazz@strannoe.ru>2017-11-25T20·20-0800
committerMatthew Garrett <mjg59@google.com>2017-11-25T20·20-0800
commitb8cf8d073e409fafef50ad0fb5435cb23d016bb2 (patch)
tree85c7d9d658387cbbaae7a47af6d03390d21a2a37 /broadlink/__init__.py
parentc85f6ac213c5bf12749ab3460700e051ad194125 (diff)
Add support for SmartOne Alarm Kit
Diffstat (limited to 'broadlink/__init__.py')
-rw-r--r--broadlink/__init__.py62
1 files changed, 61 insertions, 1 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py
index ee74b8843fd0..ea4cbc5fab34 100644
--- a/broadlink/__init__.py
+++ b/broadlink/__init__.py
@@ -11,6 +11,7 @@ import random
 import socket
 import sys
 import threading
+import codecs
 
 def gendevice(devtype, host, mac):
   if devtype == 0: # SP1
@@ -55,6 +56,8 @@ def gendevice(devtype, host, mac):
     return a1(host=host, mac=mac)
   elif devtype == 0x4EB5 or devtype == 0x4EF7: # MP1: 0x4eb5, honyar oem mp1: 0x4ef7
     return mp1(host=host, mac=mac)
+  elif devtype == 0x2722: # S1 (SmartOne Alarm Kit)
+    return S1C(host=host, mac=mac)
   else:
     return device(host=host, mac=mac)
 
@@ -278,7 +281,7 @@ class device:
         try:
           self.cs.sendto(packet, self.host)
           self.cs.settimeout(1)
-          response = self.cs.recvfrom(1024)
+          response = self.cs.recvfrom(2048)
           break
         except socket.timeout:
           if (time.time() - starttime) > self.timeout:
@@ -521,6 +524,63 @@ class rm2(rm):
     self.host = dev.host
     self.mac = dev.mac
 
+
+S1C_SENSORS_TYPES = {
+    0x31: 'Door Sensor',  # 49 as hex
+    0x91: 'Key Fob',  # 145 as hex, as serial on fob corpse
+    0x21: 'Motion Sensor'  # 33 as hex
+}
+
+
+class S1C(device):
+  """
+  Its VERY VERY VERY DIRTY IMPLEMENTATION of S1C
+  """
+  def __init__(self, *a, **kw):
+    device.__init__(self, *a, **kw)
+    self.type = 'S1C'
+
+  def get_sensors_status(self):
+    packet = bytearray(16)
+    packet[0] = 0x06  # 0x06 - get sensors info, 0x07 - probably add sensors
+    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 payload:
+        head = payload[:4]
+        count = payload[0x4] #need to fix for python 2.x
+        sensors = payload[0x6:]
+        sensors_a = [bytearray(sensors[i * 83:(i + 1) * 83]) for i in range(len(sensors) // 83)]
+
+        sens_res = []
+        for sens in sensors_a:
+          status = ord(chr(sens[0]))
+          _name = str(bytes(sens[4:26]).decode())
+          _order = ord(chr(sens[1]))
+          _type = ord(chr(sens[3]))
+          _serial = bytes(codecs.encode(sens[26:30],"hex")).decode()
+
+          type_str = S1C_SENSORS_TYPES.get(_type, 'Unknown')
+
+          r = {
+            'status': status,
+            'name': _name.strip('\x00'),
+            'type': type_str,
+            'order': _order,
+            'serial': _serial,
+          }
+          if r['serial'] != '00000000':
+            sens_res.append(r)
+        result = {
+          'count': count,
+          'sensors': sens_res
+        }
+        return result
+
+
 # Setup a new Broadlink device via AP Mode. Review the README to see how to enter AP Mode.
 # Only tested with Broadlink RM3 Mini (Blackbean)
 def setup(ssid, password, security_mode):