def climate(self): import miio if not self._climate: _LOGGER.info("initializing with host %s token %s" % (self.host, self.token)) self._climate = miio.Device(self.host, self.token) return self._climate
def _learn_command(call): ir_remote = miio.Device(host, token) if not ir_remote: _LOGGER.error("Failed to connect to device.") return key = randint(1, 1000000) ir_remote.send("miIO.ir_learn", {'key': str(key)}) _LOGGER.info("Press the key you want HASS to learn") start_time = utcnow() while (utcnow() - start_time) < timedelta(seconds=DEFAULT_TIMEOUT): res = ir_remote.send("miIO.ir_read", {'key': str(key)}) _LOGGER.error(type(res["code"])) _LOGGER.error(res["code"]) if res["code"]: log_msg = 'Recieved packet is: %s' % res["code"] _LOGGER.info(log_msg) persistent_notification.async_create(hass, log_msg, title='Chuangmi switch') return yield from asyncio.sleep(1, loop=hass.loop) _LOGGER.error('Did not received any signal.') persistent_notification.async_create(hass, "Did not received any signal", title='Chuangmi switch')
def _send_packet(call): ir_remote = miio.Device(host, token) if not ir_remote: _LOGGER.error("Failed to connect to device.") return packets = call.data.get('packet', []) for packet in packets: for retry in range(DEFAULT_RETRY): try: ir_remote.send("miIO.ir_play", {'freq':38400, 'code': str(packet)}) break except (socket.timeout, ValueError): _LOGGER.error("Failed to send packet to device.")
def __init__(self, device_ip, device_token, did): self.did = did self.miio_device = miio.Device(device_ip, device_token)
##### print("connecting to broker ", mqtt_broker) client.connect(mqtt_broker) #connect client.loop_start() #start loop to process received messages client.subscribe(mqtt_prefix + "power") #subscribe client.subscribe(mqtt_prefix + "mode") #subscribe client.subscribe(mqtt_prefix + "fanspeed") #subscribe client.subscribe(mqtt_prefix + "light") #subscribe client.subscribe(mqtt_prefix + "sound") #subscribe client.subscribe(mqtt_prefix + "childlock") #subscribe count_idle_messages = 0 count_interval_messages = 0 interval_messages = 10 ap = miio.Device(ip=miot_broker, token=miot_token) while True: while not q.empty() and count_interval_messages == 0: print("Something in the queue") # req : topic , miio_msg req = q.get() print("Sending: " + str(req[1]) + " - " + str(req[2])) try: ret = ap.raw_command(req[1], req[2]) ret = ret[0] if req[1] == "get_properties": val = ret["value"] if req[0] == "fanspeed": val = (val * 100) // 14 # val=(val*100)//3000;
def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the smart mi fan platform.""" import miio host = config.get(CONF_HOST) name = config.get(CONF_NAME) token = config.get(CONF_TOKEN) devices = config.get(CONF_SWITCHES, {}) persistent_notification = loader.get_component('persistent_notification') @asyncio.coroutine def _learn_command(call): ir_remote = miio.Device(host, token) if not ir_remote: _LOGGER.error("Failed to connect to device.") return key = randint(1, 1000000) ir_remote.send("miIO.ir_learn", {'key': str(key)}) _LOGGER.info("Press the key you want HASS to learn") start_time = utcnow() while (utcnow() - start_time) < timedelta(seconds=DEFAULT_TIMEOUT): res = ir_remote.send("miIO.ir_read", {'key': str(key)}) _LOGGER.error(type(res["code"])) _LOGGER.error(res["code"]) if res["code"]: log_msg = 'Recieved packet is: %s' % res["code"] _LOGGER.info(log_msg) persistent_notification.async_create(hass, log_msg, title='Chuangmi switch') return yield from asyncio.sleep(1, loop=hass.loop) _LOGGER.error('Did not received any signal.') persistent_notification.async_create(hass, "Did not received any signal", title='Chuangmi switch') @asyncio.coroutine def _send_packet(call): ir_remote = miio.Device(host, token) if not ir_remote: _LOGGER.error("Failed to connect to device.") return packets = call.data.get('packet', []) for packet in packets: for retry in range(DEFAULT_RETRY): try: ir_remote.send("miIO.ir_play", { 'freq': 38400, 'code': str(packet) }) break except (socket.timeout, ValueError): _LOGGER.error("Failed to send packet to device.") ir_remote = miio.Device(host, token) if not ir_remote: _LOGGER.error("Failed to connect to device.") hass.services.register(DOMAIN, SERVICE_LEARN + '_' + host.replace('.', '_'), _learn_command) hass.services.register(DOMAIN, SERVICE_SEND + '_' + host.replace('.', '_'), _send_packet) switches = [] for object_id, device_config in devices.items(): switches.append( ChuangmiIRSwitch(ir_remote, device_config.get(CONF_NAME, object_id), device_config.get(CONF_COMMAND_ON), device_config.get(CONF_COMMAND_OFF))) add_devices(switches)
client.subscribe(mqtt_prefix + "mode") #subscribe client.subscribe(mqtt_prefix + "fanspeed") #subscribe client.subscribe(mqtt_prefix + "rotate") #subscribe client.subscribe(mqtt_prefix + "sound") #subscribe client.subscribe(mqtt_prefix + "childlock") #subscribe client.subscribe(mqtt_prefix + "indicator") #subscribe count_idle_messages = 0 count_interval_messages = 0 # 6 seconds between set and get commands (time for value to be updated) interval_messages = 60 miot_retry = 0 ap = miio.Device(ip=miot_broker, token=miot_token, lazy_discover=True) # miio old ap.do_discover() ap.send_handshake() while True: while not q.empty() and count_interval_messages == 0: print("Something in the queue") # req : topic , miio_msg req = q.get() print("Sending: " + str(req[1]) + " - " + str(req[2])) miot_retry = 3 while miot_retry > 0: try: ap._discovered = True ret = ap.raw_command(req[1], req[2]) ret = ret[0]