def switch_device(host, inching, new_state): logger.info("Initialising SonoffSwitch with host %s" % host) async def update_callback(device: SonoffSwitch): if device.basic_info is not None: if device.available: if inching is None: print_device_details(device) if device.is_on: if new_state == "on": device.shutdown_event_loop() else: await device.turn_off() elif device.is_off: if new_state == "off": device.shutdown_event_loop() else: await device.turn_on() else: logger.info("Inching device activated by switching ON for " "%ss" % inching) SonoffSwitch(host=host, callback_after_update=update_callback, inching_seconds=int(inching) if inching else None, logger=logger, device_id=config['device_id'], api_key=config['api_key'])
def switch_device(host, inching, new_state): logger.info("Initialising SonoffSwitch with host %s" % host) async def update_callback(device: SonoffSwitch): if device.basic_info is not None: if inching is None: logger.info("\nInitial state:") print_device_details(device) device.client.keep_running = False if new_state == "on": await device.turn_on() else: await device.turn_off() else: logger.info("Inching device activated by switching ON for " "%ss" % inching) logger.info("\nNew state:") print_device_details(device) SonoffSwitch( host=host, callback_after_update=update_callback, inching_seconds=int(inching) if inching else None, logger=logger )
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)
def __init__(self, hass, host, name): from pysonofflan import SonoffSwitch _LOGGER.setLevel(logging.DEBUG) self._name = name self._state = None self._available = False self._shared_state = {} self._sonoff_device = SonoffSwitch( host=host, callback_after_update=self.device_update_callback, shared_state=self._shared_state, logger=_LOGGER, loop=hass.loop, ping_interval=145 ) _LOGGER.debug("HassSonoffSwitch __init__ finished creating " "SonoffSwitch")