def connect(): """Setup connection and hook it into HA for reconnect/shutdown.""" _LOGGER.info('initiating Rflink connection') # rflink create_rflink_connection decides based on the value of host # (string or None) if serial or tcp mode should be used # initiate serial/tcp connection to Rflink gateway connection = create_rflink_connection( port=port, host=host, event_callback=event_callback, disconnect_callback=reconnect, loop=hass.loop, ignore=config[DOMAIN][CONF_IGNORE_DEVICES]) try: transport, protocol = yield from connection except (serial.serialutil.SerialException, ConnectionRefusedError, TimeoutError) as exc: reconnect_interval = config[DOMAIN][CONF_RECONNECT_INTERVAL] _LOGGER.exception('error connecting to Rflink, reconnecting in %s', reconnect_interval) hass.loop.call_later(reconnect_interval, reconnect, exc) return # bind protocol to command class to allow entities to send commands RflinkCommand.set_rflink_protocol(protocol, config[DOMAIN][CONF_WAIT_FOR_ACK]) # handle shutdown of rflink asyncio transport hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, lambda x: transport.close()) _LOGGER.info('connected to Rflink')
async def connect(): """Set up connection and hook it into HA for reconnect/shutdown.""" _LOGGER.info("Initiating Rflink connection") # Rflink create_rflink_connection decides based on the value of host # (string or None) if serial or tcp mode should be used # Initiate serial/tcp connection to Rflink gateway connection = create_rflink_connection( port=port, host=host, keepalive=keepalive_idle_timer, event_callback=event_callback, disconnect_callback=reconnect, loop=hass.loop, ignore=config[DOMAIN][CONF_IGNORE_DEVICES], ) try: with async_timeout.timeout(CONNECTION_TIMEOUT): transport, protocol = await connection except ( SerialException, ConnectionRefusedError, TimeoutError, OSError, asyncio.TimeoutError, ) as exc: reconnect_interval = config[DOMAIN][CONF_RECONNECT_INTERVAL] _LOGGER.exception( "Error connecting to Rflink, reconnecting in %s", reconnect_interval ) # Connection to Rflink device is lost, make entities unavailable async_dispatcher_send(hass, SIGNAL_AVAILABILITY, False) hass.loop.call_later(reconnect_interval, reconnect, exc) return # There is a valid connection to a Rflink device now so # mark entities as available async_dispatcher_send(hass, SIGNAL_AVAILABILITY, True) # Bind protocol to command class to allow entities to send commands RflinkCommand.set_rflink_protocol(protocol, config[DOMAIN][CONF_WAIT_FOR_ACK]) # handle shutdown of Rflink asyncio transport hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, lambda x: transport.close() ) _LOGGER.info("Connected to Rflink")
async def connect(): """Set up connection and hook it into HA for reconnect/shutdown.""" _LOGGER.info('Initiating Rflink connection') # Rflink create_rflink_connection decides based on the value of host # (string or None) if serial or tcp mode should be used # Initiate serial/tcp connection to Rflink gateway connection = create_rflink_connection( port=port, host=host, event_callback=event_callback, disconnect_callback=reconnect, loop=hass.loop, ignore=config[DOMAIN][CONF_IGNORE_DEVICES] ) try: with async_timeout.timeout(CONNECTION_TIMEOUT, loop=hass.loop): transport, protocol = await connection except (serial.serialutil.SerialException, ConnectionRefusedError, TimeoutError, OSError, asyncio.TimeoutError) as exc: reconnect_interval = config[DOMAIN][CONF_RECONNECT_INTERVAL] _LOGGER.exception( "Error connecting to Rflink, reconnecting in %s", reconnect_interval) # Connection to Rflink device is lost, make entities unavailable async_dispatcher_send(hass, SIGNAL_AVAILABILITY, False) hass.loop.call_later(reconnect_interval, reconnect, exc) return # There is a valid connection to a Rflink device now so # mark entities as available async_dispatcher_send(hass, SIGNAL_AVAILABILITY, True) # Bind protocol to command class to allow entities to send commands RflinkCommand.set_rflink_protocol( protocol, config[DOMAIN][CONF_WAIT_FOR_ACK]) # handle shutdown of Rflink asyncio transport hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, lambda x: transport.close()) _LOGGER.info('Connected to Rflink')
def connect(): """Setup connection and hook it into HA for reconnect/shutdown.""" _LOGGER.info('initiating Rflink connection') # rflink create_rflink_connection decides based on the value of host # (string or None) if serial or tcp mode should be used # initiate serial/tcp connection to Rflink gateway connection = create_rflink_connection( port=port, host=host, event_callback=event_callback, disconnect_callback=reconnect, loop=hass.loop, ignore=config[DOMAIN][CONF_IGNORE_DEVICES] ) try: transport, protocol = yield from connection except (serial.serialutil.SerialException, ConnectionRefusedError, TimeoutError) as exc: reconnect_interval = config[DOMAIN][CONF_RECONNECT_INTERVAL] _LOGGER.exception( 'error connecting to Rflink, reconnecting in %s', reconnect_interval) hass.loop.call_later(reconnect_interval, reconnect, exc) return # bind protocol to command class to allow entities to send commands RflinkCommand.set_rflink_protocol( protocol, config[DOMAIN][CONF_WAIT_FOR_ACK]) # handle shutdown of rflink asyncio transport hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, lambda x: transport.close()) _LOGGER.info('connected to Rflink')
def main(argv=sys.argv[1:], loop=None): """Parse argument and setup main program loop.""" args = docopt(__doc__, argv=argv, version=pkg_resources.require('rflink')[0].version) level = logging.ERROR if args['-v']: level = logging.INFO if args['-v'] == 2: level = logging.DEBUG logging.basicConfig(level=level) if not loop: loop = asyncio.get_event_loop() if args['--ignore']: ignore = args['--ignore'].split(',') else: ignore = [] logging.debug("Parsing options " + str(args)) command = next((c for c in ALL_COMMANDS if args[c] is True), None) if command: # Valid command keyword was found and we assume this is a command protocol = PROTOCOLS['command'] else: # It's not a command: event, print, invert, repeat protocol = PROTOCOLS[args['-m']] conn = create_rflink_connection( protocol=protocol, host=args['--host'], port=args['--port'], baud=args['--baud'], loop=loop, ignore=ignore, ) transport, protocol = loop.run_until_complete(conn) try: if command: # device_id is no longer sufficient for containing all the command info. We replace the string device_id by a dict which contains all provided parameters, including a 'type' key indicating the structure the packet should have on the wire to the RFLink. # TODO: construct dict of arguments command, switch, id, type """ Define the presence and structure of parameters in the command we will send to RFLink 'minimal': '{node};{protocol};{id};', 'command': '{node};{protocol};{id};{command};', 'switch_command': '{node};{protocol};{id};{switch};{command};', 'switch_value_command': '{node};{protocol};{id};{switch};{value};{command};' """ # Insert protocol,id,switch,value,<command> key-value pairs in the dict cargs = dict( (k, args[k]) for k in ('<switch>', '<id>', '<value>', '<protocol>')) cargs.update({'<command>': command}) logging.debug("Passing command arguments: " + str(cargs)) if cargs['<value>']: cargs.update({'type': 'switch_value_command'}) elif cargs['<switch>']: cargs.update({'type': 'switch_command'}) elif cargs['<command>']: cargs.update({'type': 'command'}) else: cargs.update({'type': 'minimal'}) logging.debug('command to be sent : ' + cargs['type']) # now that we prepared the dict, let's pass it on. for _ in range(int(args['--repeat'])): loop.run_until_complete( protocol.send_command_ack(cargs, command)) else: loop.run_forever() except KeyboardInterrupt: # cleanup connection transport.close() loop.run_forever() finally: loop.close()