Example #1
0
def send_mp1(device):
    result = {}
    state = True
    host = device['ip']
    port = device['port']
    mac = device['mac']
    name = device['name']
    sid = device['sid']
    wantedstate = device['state']
    if int(wantedstate) == 0:
        state = False
    product = broadlink.gendevice(0x4eb5,
                                  host=(host, int(port)),
                                  mac=bytearray.fromhex(mac))
    logging.debug("Connecting to Broadlink device with name " + name + "....")
    product.auth()
    logging.debug("Connected to Broadlink device with name " + name + "....")
    if sid == 'all':
        for i in range(1, 5):
            product.set_power(i, state)
    else:
        product.set_power(int(sid), state)
    time.sleep(0.1)
    result = read_mp1(device)
    return result
Example #2
0
def learn_rm2_rf(device):
    result = {}
    host = device['ip']
    port = device['port']
    mac = device['mac']
    if mac[-3:] == 'sub':
        logging.debug("This is a child device original mac is " + mac[:-4])
        mac = mac[:-4]
    name = device['name']
    product = broadlink.gendevice(0x2712,
                                  host=(host, int(port)),
                                  mac=bytearray.fromhex(mac))
    logging.debug("Connecting to Broadlink device with name " + name + "....")
    product.auth()
    logging.debug("Connected to Broadlink device with name " + name + "....")
    logging.debug("Enter learning")
    product.sweep_frequency()
    logging.debug(
        "Learning RF Frequency, press and hold the button to learn...")
    start = time.time()
    found = False
    while time.time() - start < 10:
        time.sleep(1)
        if product.check_frequency():
            logging.debug("Found RF Frequency")
            found = True
            globals.JEEDOMCOM.send_change_immediate({'foundfrequency': 1})
            break
    else:
        logging.debug("RF Frequency not found")
        product.cancel_sweep_frequency()
        globals.JEEDOMCOM.send_change_immediate({'foundfrequency': 0})
        return result
    if found:
        time.sleep(2)
        globals.JEEDOMCOM.send_change_immediate({'step2': 1})
        product.find_rf_packet()
        start = time.time()
        data = None
        while time.time() - start < 10:
            time.sleep(1)
            try:
                data = product.check_data()
            except (ReadError, StorageError):
                continue
            else:
                break
        if data == None:
            logging.debug("No button pressed")
            result['hexcode'] = 'no'
        else:
            myhex = data.hex()
            logging.debug("Hex code detected " + myhex)
            result['hexcode'] = myhex
        result['learnedCmd'] = 1
        result['mac'] = mac
        product.stop_learning()
        logging.debug("Quit learning")
    return result
Example #3
0
def read_rm2(device):
    result = {}
    host = device['ip']
    port = device['port']
    mac = device['mac']
    name = device['name']
    product = broadlink.gendevice(0x2712,
                                  host=(host, int(port)),
                                  mac=bytearray.fromhex(mac))
    logging.debug("Connecting to Broadlink device with name " + name + "....")
    product.auth()
    logging.debug("Connected to Broadlink device with name " + name + "....")
    result['mac'] = mac
    result['temperature'] = product.check_temperature()
    logging.debug(str(result))
    return result
Example #4
0
def send_sp2(device):
	result ={}
	state = True
	host = device['ip']
	port = device['port']
	mac = device['mac']
	name = device['name']
	wantedstate = device['state']
	if int(wantedstate) == 0:
		state = False
	product = broadlink.gendevice(0x2711,host=(host,int(port)), mac=bytearray.fromhex(mac))
	logging.debug("Connecting to Broadlink device with name " + name + "....")
	product.auth()
	logging.debug("Connected to Broadlink device with name " + name + "....")
	product.set_power(state)
	time.sleep(0.1)
	result = read_sp2(device)
	return result
Example #5
0
def send_rm2(device):
    result = {}
    host = device['ip']
    port = device['port']
    mac = device['mac']
    if mac[-3:] == 'sub':
        logging.debug("This is a child device original mac is " + mac[:-4])
        mac = mac[:-4]
    name = device['name']
    hex2send = device['hex2send']
    product = broadlink.gendevice(0x2712,
                                  host=(host, int(port)),
                                  mac=bytearray.fromhex(mac))
    logging.debug("Connecting to Broadlink device with name " + name + "....")
    product.auth()
    logging.debug("Connected to Broadlink device with name " + name + "....")
    product.send_data(bytearray.fromhex(hex2send))
    logging.debug("Code Sent....")
    return
Example #6
0
def learn_rm2(device):
    if device['mode'] == 'rf':
        result = learn_rm2_rf(device)
        return result
    result = {}
    host = device['ip']
    port = device['port']
    mac = device['mac']
    if mac[-3:] == 'sub':
        logging.debug("This is a child device original mac is " + mac[:-4])
        mac = mac[:-4]
    name = device['name']
    product = broadlink.gendevice(0x2712,
                                  host=(host, int(port)),
                                  mac=bytearray.fromhex(mac))
    logging.debug("Connecting to Broadlink device with name " + name + "....")
    product.auth()
    logging.debug("Connected to Broadlink device with name " + name + "....")
    logging.debug("Enter learning")
    product.enter_learning()
    start = time.time()
    data = None
    while time.time() - start < 10:
        time.sleep(1)
        try:
            data = product.check_data()
        except (ReadError, StorageError):
            continue
        else:
            break
    if data == None:
        logging.debug("No button pressed")
        result['hexcode'] = 'no'
    else:
        myhex = data.hex()
        logging.debug("Hex code detected " + myhex)
        result['hexcode'] = myhex
    result['learnedCmd'] = 1
    result['mac'] = mac
    product.stop_learning()
    logging.debug("Quit learning")
    return result
Example #7
0
def read_mp1(device):
    result = {}
    host = device['ip']
    port = device['port']
    mac = device['mac']
    name = device['name']
    product = broadlink.gendevice(0x4eb5,
                                  host=(host, int(port)),
                                  mac=bytearray.fromhex(mac))
    logging.debug("Connecting to Broadlink device with name " + name + "....")
    product.auth()
    logging.debug("Connected to Broadlink device with name " + name + "....")
    result['mac'] = mac
    data = product.check_power()
    for x in data:
        if data[x]:
            result[x] = 1
        else:
            result[x] = 0
    logging.debug(str(result))
    return result
Example #8
0
def on_message(client, userdata, message):
    json_data = json.loads(message.payload.decode("utf-8"))
    # print("\nReceived:", json_data)

    key = str(json_data['ip']) + '-' + str(json_data['mac'])

    # Connect to device when the first request is made
    if key not in devices:
        try:
            print(' > Connecting to ' + json_data['ip'] + ' [' +
                  json_data['mac'] + ']')
            device = broadlink.gendevice(int(json_data['type'],
                                             16), (json_data['ip'], 80),
                                         json_data['mac'].replace(':', ''))

            print(' > Authenticating')
            if device.auth():
                devices[key] = device
            else:
                print('ERROR while authenticating!')
                return

        except Exception as e:
            print("ERROR:", e)
            return

    try:
        packet = b64decode(json_data['packet'])
    except Exception as e:
        print("ERROR decoding packet:", e)
        return

    try:
        print('Sending packet:', json_data['packet'])
        devices[key].send_data(packet)
    except Exception as e:
        print("ERROR sending packet:", e)
        return
Example #9
0
def read_a1(device):
	result ={}
	host = device['ip']
	port = device['port']
	mac = device['mac']
	name = device['name']
	product = broadlink.gendevice(0x2714,host=(host,int(port)), mac=bytearray.fromhex(mac))
	logging.debug("Connecting to Broadlink device with name " + name + "....")
	product.auth()
	logging.debug("Connected to Broadlink device with name " + name + "....")
	result['mac']=mac
	raw = product.check_sensors_raw()
	human = product.check_sensors()
	result['temperature'] = raw['temperature']
	result['humidity'] = raw['humidity']
	result['luminosity'] = raw['light']
	result['air'] = raw['air_quality']
	result['noise'] = raw['noise']
	result['luminosity_human'] = human['light']
	result['air_human'] = human['air_quality']
	result['noise_human'] = human['noise']
	logging.debug(str(result))
	return result
Example #10
0
def read_sp2(device):
	result ={}
	host = device['ip']
	port = device['port']
	mac = device['mac']
	name = device['name']
	product = broadlink.gendevice(0x2711,host=(host,int(port)), mac=bytearray.fromhex(mac))
	logging.debug("Connecting to Broadlink device with name " + name + "....")
	product.auth()
	logging.debug("Connected to Broadlink device with name " + name + "....")
	result['mac']=mac
	data = product.check_power()
	if data == True:
		result['s1']=1
	else:
		result['s1']=0
	logging.debug(str(result))
	data_poweroutput = product.get_energy()
	if data_poweroutput != None and result['s1']==1:
		result['power_output']=data_poweroutput
	else:
		result['power_output']=0
	logging.debug("value from sp2="+str(result))
	return result