def setup(hass, config): """Setup the QSUSB component.""" from pyqwikswitch import (QSUsb, CMD_BUTTONS, QS_NAME, QS_ID, QS_CMD, QS_TYPE, PQS_VALUE, PQS_TYPE, QSType) # Override which cmd's in /&listen packets will fire events # By default only buttons of type [TOGGLE,SCENE EXE,LEVEL] cmd_buttons = config[DOMAIN].get('button_events', ','.join(CMD_BUTTONS)) cmd_buttons = cmd_buttons.split(',') try: url = config[DOMAIN].get('url', 'http://127.0.0.1:2020') dimmer_adjust = float(config[DOMAIN].get('dimmer_adjust', '1')) qsusb = QSUsb(url, _LOGGER, dimmer_adjust) # Ensure qsusb terminates threads correctly hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, lambda event: qsusb.stop()) except ValueError as val_err: _LOGGER.error(str(val_err)) return False qsusb.ha_devices = qsusb.devices() qsusb.ha_objects = {} # Identify switches & remove ' Switch' postfix in name for item in qsusb.ha_devices: if item[PQS_TYPE] == QSType.relay and \ item[QS_NAME].lower().endswith(' switch'): item[QS_TYPE] = 'switch' item[QS_NAME] = item[QS_NAME][:-7] global QSUSB if QSUSB is None: QSUSB = {} QSUSB[id(qsusb)] = qsusb # Load sub-components for qwikswitch for comp_name in ('switch', 'light'): load_platform(hass, comp_name, 'qwikswitch', {'qsusb_id': id(qsusb)}, config) def qs_callback(item): """Typically a button press or update signal.""" # If button pressed, fire a hass event if item.get(QS_CMD, '') in cmd_buttons: hass.bus.fire('qwikswitch.button.' + item.get(QS_ID, '@no_id')) return # Update all ha_objects qsreply = qsusb.devices() if qsreply is False: return for item in qsreply: if item[QS_ID] in qsusb.ha_objects: qsusb.ha_objects[item[QS_ID]].update_value( round(min(item[PQS_VALUE], 100) * 2.55)) qsusb.listen(callback=qs_callback, timeout=30) return True
def setup(hass, config): """Setup the QSUSB component.""" from pyqwikswitch import (QSUsb, CMD_BUTTONS, QS_NAME, QS_ID, QS_CMD, QS_TYPE, PQS_VALUE, PQS_TYPE, QSType) # Override which cmd's in /&listen packets will fire events # By default only buttons of type [TOGGLE,SCENE EXE,LEVEL] cmd_buttons = config[DOMAIN].get('button_events', ','.join(CMD_BUTTONS)) cmd_buttons = cmd_buttons.split(',') try: url = config[DOMAIN].get('url', 'http://127.0.0.1:2020') dimmer_adjust = float(config[DOMAIN].get('dimmer_adjust', '1')) qsusb = QSUsb(url, _LOGGER, dimmer_adjust) # Ensure qsusb terminates threads correctly hass.bus.listen_once(EVENT_BLUMATE_STOP, lambda event: qsusb.stop()) except ValueError as val_err: _LOGGER.error(str(val_err)) return False qsusb.ha_devices = qsusb.devices() qsusb.ha_objects = {} # Identify switches & remove ' Switch' postfix in name for item in qsusb.ha_devices: if item[PQS_TYPE] == QSType.relay and \ item[QS_NAME].lower().endswith(' switch'): item[QS_TYPE] = 'switch' item[QS_NAME] = item[QS_NAME][:-7] global QSUSB if QSUSB is None: QSUSB = {} QSUSB[id(qsusb)] = qsusb # Load sub-components for qwikswitch for comp_name in ('switch', 'light'): load_platform(hass, comp_name, 'qwikswitch', {'qsusb_id': id(qsusb)}, config) def qs_callback(item): """Typically a button press or update signal.""" # If button pressed, fire a hass event if item.get(QS_CMD, '') in cmd_buttons: hass.bus.fire('qwikswitch.button.' + item.get(QS_ID, '@no_id')) return # Update all ha_objects qsreply = qsusb.devices() if qsreply is False: return for item in qsreply: if item[QS_ID] in qsusb.ha_objects: qsusb.ha_objects[item[QS_ID]].update_value( round(min(item[PQS_VALUE], 100) * 2.55)) qsusb.listen(callback=qs_callback, timeout=30) return True
def setup(hass, config): """Setup the QSUSB component.""" from pyqwikswitch import (QSUsb, CMD_BUTTONS, QS_NAME, QS_ID, QS_CMD, PQS_VALUE, PQS_TYPE, QSType) # Override which cmd's in /&listen packets will fire events # By default only buttons of type [TOGGLE,SCENE EXE,LEVEL] cmd_buttons = config[DOMAIN].get('button_events', ','.join(CMD_BUTTONS)) cmd_buttons = cmd_buttons.split(',') url = config[DOMAIN]['url'] dimmer_adjust = config[DOMAIN]['dimmer_adjust'] qsusb = QSUsb(url, _LOGGER, dimmer_adjust) def _stop(event): """Stop the listener queue and clean up.""" nonlocal qsusb qsusb.stop() qsusb = None global QSUSB QSUSB = {} _LOGGER.info("Waiting for long poll to QSUSB to time out") hass.bus.listen(EVENT_HOMEASSISTANT_STOP, _stop) # Discover all devices in QSUSB devices = qsusb.devices() QSUSB['switch'] = [] QSUSB['light'] = [] for item in devices: if item[PQS_TYPE] == QSType.relay and (item[QS_NAME].lower() .endswith(' switch')): item[QS_NAME] = item[QS_NAME][:-7] # Remove ' switch' postfix QSUSB['switch'].append(QSSwitch(item, qsusb)) elif item[PQS_TYPE] in [QSType.relay, QSType.dimmer]: QSUSB['light'].append(QSLight(item, qsusb)) else: _LOGGER.warning("Ignored unknown QSUSB device: %s", item) # Load platforms for comp_name in ('switch', 'light'): if len(QSUSB[comp_name]) > 0: load_platform(hass, comp_name, 'qwikswitch', {}, config) def qs_callback(item): """Typically a button press or update signal.""" if qsusb is None: # Shutting down _LOGGER.info("Done") return # If button pressed, fire a hass event if item.get(QS_CMD, '') in cmd_buttons: hass.bus.fire('qwikswitch.button.' + item.get(QS_ID, '@no_id')) return # Update all ha_objects qsreply = qsusb.devices() if qsreply is False: return for item in qsreply: if item[QS_ID] in QSUSB: QSUSB[item[QS_ID]].update_value( round(min(item[PQS_VALUE], 100) * 2.55)) def _start(event): """Start listening.""" qsusb.listen(callback=qs_callback, timeout=30) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start) return True
def setup(hass, config): """Set up the QSUSB component.""" from pyqwikswitch import ( QSUsb, CMD_BUTTONS, QS_NAME, QS_ID, QS_CMD, PQS_VALUE, PQS_TYPE, QSType) # Override which cmd's in /&listen packets will fire events # By default only buttons of type [TOGGLE,SCENE EXE,LEVEL] cmd_buttons = config[DOMAIN].get(CONF_BUTTON_EVENTS, ','.join(CMD_BUTTONS)) cmd_buttons = cmd_buttons.split(',') url = config[DOMAIN][CONF_URL] dimmer_adjust = config[DOMAIN][CONF_DIMMER_ADJUST] qsusb = QSUsb(url, _LOGGER, dimmer_adjust) def _stop(event): """Stop the listener queue and clean up.""" nonlocal qsusb qsusb.stop() qsusb = None global QSUSB QSUSB = {} _LOGGER.info("Waiting for long poll to QSUSB to time out") hass.bus.listen(EVENT_HOMEASSISTANT_STOP, _stop) # Discover all devices in QSUSB devices = qsusb.devices() QSUSB['switch'] = [] QSUSB['light'] = [] for item in devices: if item[PQS_TYPE] == QSType.relay and (item[QS_NAME].lower() .endswith(' switch')): item[QS_NAME] = item[QS_NAME][:-7] # Remove ' switch' postfix QSUSB['switch'].append(QSSwitch(item, qsusb)) elif item[PQS_TYPE] in [QSType.relay, QSType.dimmer]: QSUSB['light'].append(QSLight(item, qsusb)) else: _LOGGER.warning("Ignored unknown QSUSB device: %s", item) # Load platforms for comp_name in ('switch', 'light'): if QSUSB[comp_name]: load_platform(hass, comp_name, 'qwikswitch', {}, config) def qs_callback(item): """Typically a button press or update signal.""" if qsusb is None: # Shutting down _LOGGER.info("Botton press or updating signal done") return # If button pressed, fire a hass event if item.get(QS_CMD, '') in cmd_buttons: hass.bus.fire('qwikswitch.button.' + item.get(QS_ID, '@no_id')) return # Update all ha_objects qsreply = qsusb.devices() if qsreply is False: return for item in qsreply: if item[QS_ID] in QSUSB: QSUSB[item[QS_ID]].update_value( round(min(item[PQS_VALUE], 100) * 2.55)) def _start(event): """Start listening.""" qsusb.listen(callback=qs_callback, timeout=30) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start) return True