def find_host_from_device_id(device_id): """Discover a device identified by its device_id""" logger.info( "Trying to discover %s by scanning for devices " "on local network, please wait..." % device_id) found_devices = asyncio.get_event_loop().run_until_complete( Discover.discover(logger)).items() for ip, _ in found_devices: logger.info("Found Sonoff LAN Mode device at IP %s, attempting to " "read state to get device ID" % ip) shared_state = {'device_id_at_current_ip': None} async def device_id_callback(device: SonoffSwitch): if device.basic_info is not None: device.shared_state['device_id_at_current_ip'] = \ device.device_id device.keep_running = False SonoffSwitch( host=ip, callback_after_update=device_id_callback, shared_state=shared_state, logger=logger ) current_device_id = shared_state['device_id_at_current_ip'] if device_id.lower() == current_device_id.lower(): return ip else: logger.info( "Found device ID %s which did not match" % current_device_id ) return None
def state(config: dict): """Connect to device and print current state.""" async def state_callback(device): if device.basic_info is not None: if device.available: print_device_details(device) device.shutdown_event_loop() logger.info("Initialising SonoffSwitch with host %s" % config['host']) SonoffSwitch(host=config['host'], callback_after_update=state_callback, logger=logger)
def listen(config: dict): """Connect to device, print state, then print updates until quit.""" async def state_callback(self): if self.basic_info is not None: print_device_details(self) if self.shared_state['callback_counter'] == 0: logger.info( "Listening for updates forever... Press CTRL+C to quit.") self.shared_state['callback_counter'] += 1 logger.info("Initialising SonoffSwitch with host %s" % config['host']) shared_state = {'callback_counter': 0} SonoffSwitch(host=config['host'], callback_after_update=state_callback, shared_state=shared_state, logger=logger)
async def device_id_callback(device: SonoffSwitch): if device.basic_info is not None: device.shared_state['device_id_at_current_ip'] = \ device.device_id device.keep_running = False
async def device_id_callback(device: SonoffSwitch): if device.basic_info is not None: device.shared_state['device_id_at_current_ip'] = \ device.device_id device.shutdown_event_loop()