about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFelipe Martins Diel <41558831+felipediel@users.noreply.github.com>2020-03-24T15·09-0300
committerGitHub <noreply@github.com>2020-03-24T15·09+0100
commit446496cc24642e5c6ca0dbda6c2f9e3116b9f7ca (patch)
tree1089fc636012b8e9e2d31d6b112f396349d19f3d
parent4a3950a7c50f574530d9085782d2cff1c3df5c4f (diff)
Obtain device name and cloud byte from HELLO_RESPONSE (#322)
-rw-r--r--broadlink/__init__.py64
1 files changed, 35 insertions, 29 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py
index 627389bd9462..a8010574e365 100644
--- a/broadlink/__init__.py
+++ b/broadlink/__init__.py
@@ -14,7 +14,7 @@ from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
 
 
-def gendevice(devtype, host, mac):
+def gendevice(devtype, host, mac, name=None, cloud=None):
     devices = {
         sp1: [0],
         sp2: [0x2711,  # SP2
@@ -66,8 +66,8 @@ def gendevice(devtype, host, mac):
     # Look for the class associated to devtype in devices
     [device_class] = [dev for dev in devices if devtype in devices[dev]] or [None]
     if device_class is None:
-        return device(host=host, mac=mac, devtype=devtype)
-    return device_class(host=host, mac=mac, devtype=devtype)
+        return device(host, mac, devtype, name=name, cloud=cloud)
+    return device_class(host, mac, devtype, name=name, cloud=cloud)
 
 
 def discover(timeout=None, local_ip_address=None, discover_ip_address='255.255.255.255'):
@@ -128,10 +128,12 @@ def discover(timeout=None, local_ip_address=None, discover_ip_address='255.255.2
         response = cs.recvfrom(1024)
         responsepacket = bytearray(response[0])
         host = response[1]
-        mac = responsepacket[0x3a:0x40]
         devtype = responsepacket[0x34] | responsepacket[0x35] << 8
-
-        return gendevice(devtype, host, mac)
+        mac = responsepacket[0x3a:0x40]
+        name = responsepacket[0x40:].split(b'\x00')[0].decode('utf-8')
+        cloud = bool(responsepacket[-1])
+        device = gendevice(devtype, host, mac, name=name, cloud=cloud)
+        return device
 
     while (time.time() - starttime) < timeout:
         cs.settimeout(timeout - (time.time() - starttime))
@@ -143,16 +145,20 @@ def discover(timeout=None, local_ip_address=None, discover_ip_address='255.255.2
         host = response[1]
         devtype = responsepacket[0x34] | responsepacket[0x35] << 8
         mac = responsepacket[0x3a:0x40]
-        dev = gendevice(devtype, host, mac)
-        devices.append(dev)
+        name = responsepacket[0x40:].split(b'\x00')[0].decode('utf-8')
+        cloud = bool(responsepacket[-1])
+        device = gendevice(devtype, host, mac, name=name, cloud=cloud)
+        devices.append(device)
     return devices
 
 
 class device:
-    def __init__(self, host, mac, devtype, timeout=10):
+    def __init__(self, host, mac, devtype, timeout=10, name=None, cloud=None):
         self.host = host
         self.mac = mac.encode() if isinstance(mac, str) else mac
         self.devtype = devtype if devtype is not None else 0x272a
+        self.name = name
+        self.cloud = cloud
         self.timeout = timeout
         self.count = random.randrange(0xffff)
         self.iv = bytearray(
@@ -286,8 +292,8 @@ class device:
 
 
 class mp1(device):
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = "MP1"
 
     def set_power_mask(self, sid_mask, state):
@@ -350,8 +356,8 @@ class mp1(device):
 
 
 class bg1(device):
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = "BG1"
 
     def get_state(self):
@@ -417,8 +423,8 @@ class bg1(device):
         return state
 
 class sp1(device):
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = "SP1"
 
     def set_power(self, state):
@@ -428,8 +434,8 @@ class sp1(device):
 
 
 class sp2(device):
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = "SP2"
 
     def set_power(self, state):
@@ -494,8 +500,8 @@ class sp2(device):
 
 
 class a1(device):
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = "A1"
 
     def check_sensors(self):
@@ -574,8 +580,8 @@ class a1(device):
 
 
 class rm(device):
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = "RM2"
         self._request_header = bytes()
         self._code_sending_header = bytes()
@@ -654,8 +660,8 @@ class rm(device):
         return self._read_sensor( 0x01, 4, 10.0 )
 
 class rm4(rm):
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = "RM4"
         self._request_header = b'\x04\x00'
         self._code_sending_header = b'\xd0\x00'
@@ -684,8 +690,8 @@ class rm2(rm):
 
 
 class hysen(device):
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = "Hysen heating controller"
 
     # Send a request
@@ -873,8 +879,8 @@ class S1C(device):
     Its VERY VERY VERY DIRTY IMPLEMENTATION of S1C
     """
 
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = 'S1C'
 
     def get_sensors_status(self):
@@ -919,8 +925,8 @@ class S1C(device):
 
 
 class dooya(device):
-    def __init__(self, host, mac, devtype):
-        device.__init__(self, host, mac, devtype)
+    def __init__(self, *args, **kwargs):
+        device.__init__(self, *args, **kwargs)
         self.type = "Dooya DT360E"
 
     def _send(self, magic1, magic2):