Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obtain device name and cloud byte from HELLO_RESPONSE #322

Merged
merged 1 commit into from Mar 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 35 additions & 29 deletions broadlink/__init__.py
Expand Up @@ -14,7 +14,7 @@
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
Expand Down Expand Up @@ -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)
Comment on lines +69 to +70
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed something that I don't completely understand here. Why were we using kwargs? Backwards compatibility? In that case, let me know and I will keep the previous syntax. Otherwise, I don't see why not use args.



def discover(timeout=None, local_ip_address=None, discover_ip_address='255.255.255.255'):
Expand Down Expand Up @@ -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))
Expand All @@ -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(
Expand Down Expand Up @@ -286,8 +292,8 @@ def send_packet(self, command, payload):


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):
Expand Down Expand Up @@ -350,8 +356,8 @@ def check_power(self):


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):
Expand Down Expand Up @@ -417,8 +423,8 @@ def _decode(self, response):
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):
Expand All @@ -428,8 +434,8 @@ def set_power(self, state):


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):
Expand Down Expand Up @@ -494,8 +500,8 @@ def get_energy(self):


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):
Expand Down Expand Up @@ -574,8 +580,8 @@ def check_sensors_raw(self):


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()
Expand Down Expand Up @@ -652,8 +658,8 @@ def check_temperature(self):


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'
Expand All @@ -671,8 +677,8 @@ def discover(self):


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
Expand Down Expand Up @@ -860,8 +866,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):
Expand Down Expand Up @@ -906,8 +912,8 @@ def get_sensors_status(self):


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):
Expand Down