Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
start_time = time.time()
with self.lock:
while True:
try:
self.cs.sendto(packet, self.host)
self.cs.settimeout(1)
response = self.cs.recvfrom(2048)
break
except socket.timeout:
if (time.time() - start_time) > self.timeout:
raise
return bytearray(response[0])
class mp1(device):
def __init__(self, host, mac, devtype):
device.__init__(self, host, mac, devtype)
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
data['s4'] = bool(state & 0x08)
return data
class sp1(device):
def __init__(self, host, mac, devtype):
device.__init__(self, host, mac, devtype)
self.type = "SP1"
def set_power(self, state):
packet = bytearray(4)
packet[0] = state
self.send_packet(0x66, packet)
class sp2(device):
def __init__(self, host, mac, devtype):
device.__init__(self, host, mac, devtype)
self.type = "SP2"
def set_power(self, state):
"""Sets the power state of the smart plug."""
packet = bytearray(16)
packet[0] = 2
if self.check_nightlight():
packet[4] = 3 if state else 2
else:
packet[4] = 1 if state else 0
self.send_packet(0x6a, packet)
def set_nightlight(self, state):
"""Sets the night light state of the smart plug"""
if isinstance(payload[0x4], int):
data['temperature'] = (payload[0x4] * 10 + payload[0x5]) / 10.0
data['humidity'] = (payload[0x6] * 10 + payload[0x7]) / 10.0
data['light'] = payload[0x8]
data['air_quality'] = payload[0x0a]
data['noise'] = payload[0xc]
else:
data['temperature'] = (ord(payload[0x4]) * 10 + ord(payload[0x5])) / 10.0
data['humidity'] = (ord(payload[0x6]) * 10 + ord(payload[0x7])) / 10.0
data['light'] = ord(payload[0x8])
data['air_quality'] = ord(payload[0x0a])
data['noise'] = ord(payload[0xc])
return data
class rm(device):
def __init__(self, host, mac, devtype):
device.__init__(self, host, mac, devtype)
self.type = "RM2"
def check_data(self):
packet = bytearray(16)
packet[0] = 4
response = self.send_packet(0x6a, packet)
err = response[0x22] | (response[0x23] << 8)
if err != 0:
return None
payload = self.decrypt(bytes(response[0x38:]))
return payload[0x04:]
def send_data(self, data):
packet = bytearray([0x02, 0x00, 0x00, 0x00])
'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
class dooya(device):
def __init__(self, host, mac, devtype):
device.__init__(self, host, mac, devtype)
self.type = "Dooya DT360E"
def _send(self, magic1, magic2):
packet = bytearray(16)
packet[0] = 0x09
packet[2] = 0xbb
packet[3] = magic1
packet[4] = magic2
packet[9] = 0xfa
packet[10] = 0x44
response = self.send_packet(0x6a, packet)
err = response[0x22] | (response[0x23] << 8)
if err != 0:
return None
0x278f, # RM Mini Shate
0x27c2 # RM Mini 3
],
a1: [0x2714], # A1
mp1: [0x4EB5, # MP1
0x4EF7 # Honyar oem mp1
],
hysen: [0x4EAD], # Hysen controller
S1C: [0x2722], # S1 (SmartOne Alarm Kit)
dooya: [0x4E4D] # Dooya DT360E (DOOYA_CURTAIN_V2)
}
# 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 state
def check_power(self):
"""Returns the power state of the smart power strip."""
state = self.check_power_raw()
if state is None:
return {'s1': None, 's2': None, 's3': None, 's4': None}
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, devtype):
device.__init__(self, host, mac, devtype)
self.type = "SP1"
def set_power(self, state):
packet = bytearray(4)
packet[0] = state
self.send_packet(0x66, packet)
class sp2(device):
def __init__(self, host, mac, devtype):
device.__init__(self, host, mac, devtype)
self.type = "SP2"
def set_power(self, state):
# weekend temperatures
for i in range(0, 2):
input_payload.append(int(weekend[i]['temp'] * 2))
self.send_request(input_payload)
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, host, mac, devtype):
device.__init__(self, host, mac, devtype)
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:
return None