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): """Your controller/hub specific code.""" from pyAqara.gateway import AqaraGateway gateway = AqaraGateway() gateway.initGateway() gateway.listen(timeout=5) gateway.password = config.get(DOMAIN, {}).get(CONF_GATEWAY_PASSWORD, '') hass.data['AqaraGateway'] = gateway def _stop(event): """Stop the listener queue and clean up.""" print('stop event') nonlocal gateway gateway.stop() gateway = None _LOGGER.info("Waiting for long poll to Aqara Gateway to time out") hass.bus.listen(EVENT_HOMEASSISTANT_STOP, _stop) for component in AQARA_COMPONENTS: load_platform(hass, component, DOMAIN, {}, config) return True
def _new_device(device): """Called when new device is detected by HDMI network.""" key = DOMAIN + '.' + device.name hass.data[key] = device discovery.load_platform(hass, base_config.get(core.DOMAIN).get( CONF_CUSTOMIZE, {}).get(key, {}).get(CONF_PLATFORM, platform), DOMAIN, discovered={ATTR_NEW: [key]}, hass_config=base_config)
def _new_device(device): """Called when new device is detected by HDMI network.""" key = DOMAIN + '.' + device.name hass.data[key] = device discovery.load_platform( hass, customize.get_overrides(hass, core.DOMAIN, key).get( CONF_PLATFORM, platform), DOMAIN, discovered={ATTR_NEW: [key]}, hass_config=base_config)
def _new_device(device): """Called when new device is detected by HDMI network.""" key = DOMAIN + '.' + device.name hass.data[key] = device ent_platform = base_config[DOMAIN][CONF_TYPES].get(key, platform) discovery.load_platform(hass, ent_platform, DOMAIN, discovered={ATTR_NEW: [key]}, hass_config=base_config)
def _new_device(device): """Called when new device is detected by HDMI network.""" key = DOMAIN + '.' + device.name hass.data[key] = device discovery.load_platform(hass, customize.get_overrides( hass, core.DOMAIN, key).get(CONF_PLATFORM, platform), DOMAIN, discovered={ATTR_NEW: [key]}, hass_config=base_config)
def setup(hass, base_config): """Common setup for Envisalink devices.""" from pyenvisalink import EnvisalinkAlarmPanel from pydispatch import dispatcher global EVL_CONTROLLER config = base_config.get(DOMAIN) _host = config.get(CONF_EVL_HOST) _port = config.get(CONF_EVL_PORT) _code = config.get(CONF_CODE) _panel_type = config.get(CONF_PANEL_TYPE) _panic_type = config.get(CONF_PANIC) _version = config.get(CONF_EVL_VERSION) _user = config.get(CONF_USERNAME) _pass = config.get(CONF_PASS) _keep_alive = config.get(CONF_EVL_KEEPALIVE) _zone_dump = config.get(CONF_ZONEDUMP_INTERVAL) _zones = config.get(CONF_ZONES) _partitions = config.get(CONF_PARTITIONS) _connect_status = {} EVL_CONTROLLER = EnvisalinkAlarmPanel(_host, _port, _panel_type, _version, _user, _pass, _zone_dump, _keep_alive, hass.loop) def login_fail_callback(data): """Callback for when the evl rejects our login.""" _LOGGER.error("The envisalink rejected your credentials.") _connect_status['fail'] = 1 def connection_fail_callback(data): """Network failure callback.""" _LOGGER.error("Could not establish a connection with the envisalink.") _connect_status['fail'] = 1 def connection_success_callback(data): """Callback for a successful connection.""" _LOGGER.info("Established a connection with the envisalink.") _connect_status['success'] = 1 def zones_updated_callback(data): """Handle zone timer updates.""" _LOGGER.info("Envisalink sent a zone update event. Updating zones...") dispatcher.send(signal=SIGNAL_ZONE_UPDATE, sender=None, zone=data) def alarm_data_updated_callback(data): """Handle non-alarm based info updates.""" _LOGGER.info("Envisalink sent new alarm info. Updating alarms...") dispatcher.send(signal=SIGNAL_KEYPAD_UPDATE, sender=None, partition=data) def partition_updated_callback(data): """Handle partition changes thrown by evl (including alarms).""" _LOGGER.info("The envisalink sent a partition update event.") dispatcher.send(signal=SIGNAL_PARTITION_UPDATE, sender=None, partition=data) def stop_envisalink(event): """Shutdown envisalink connection and thread on exit.""" _LOGGER.info("Shutting down envisalink.") EVL_CONTROLLER.stop() def start_envisalink(event): """Startup process for the Envisalink.""" hass.loop.call_soon_threadsafe(EVL_CONTROLLER.start) for _ in range(10): if 'success' in _connect_status: hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_envisalink) return True elif 'fail' in _connect_status: return False else: time.sleep(1) _LOGGER.error("Timeout occurred while establishing evl connection.") return False EVL_CONTROLLER.callback_zone_timer_dump = zones_updated_callback EVL_CONTROLLER.callback_zone_state_change = zones_updated_callback EVL_CONTROLLER.callback_partition_state_change = partition_updated_callback EVL_CONTROLLER.callback_keypad_update = alarm_data_updated_callback EVL_CONTROLLER.callback_login_failure = login_fail_callback EVL_CONTROLLER.callback_login_timeout = connection_fail_callback EVL_CONTROLLER.callback_login_success = connection_success_callback _result = start_envisalink(None) if not _result: return False # Load sub-components for Envisalink if _partitions: load_platform(hass, 'alarm_control_panel', 'envisalink', {CONF_PARTITIONS: _partitions, CONF_CODE: _code, CONF_PANIC: _panic_type}, base_config) load_platform(hass, 'sensor', 'envisalink', {CONF_PARTITIONS: _partitions, CONF_CODE: _code}, base_config) if _zones: load_platform(hass, 'binary_sensor', 'envisalink', {CONF_ZONES: _zones}, base_config) return True
def setup(hass, base_config): """Common setup for Envisalink devices.""" from pyenvisalink import EnvisalinkAlarmPanel from pydispatch import dispatcher global EVL_CONTROLLER config = base_config.get(DOMAIN) _host = config.get(CONF_EVL_HOST) _port = config.get(CONF_EVL_PORT) _code = config.get(CONF_CODE) _panel_type = config.get(CONF_PANEL_TYPE) _panic_type = config.get(CONF_PANIC) _version = config.get(CONF_EVL_VERSION) _user = config.get(CONF_USERNAME) _pass = config.get(CONF_PASS) _keep_alive = config.get(CONF_EVL_KEEPALIVE) _zone_dump = config.get(CONF_ZONEDUMP_INTERVAL) _zones = config.get(CONF_ZONES) _partitions = config.get(CONF_PARTITIONS) _connect_status = {} EVL_CONTROLLER = EnvisalinkAlarmPanel(_host, _port, _panel_type, _version, _user, _pass, _zone_dump, _keep_alive, hass.loop) def login_fail_callback(data): """Callback for when the evl rejects our login.""" _LOGGER.error("The envisalink rejected your credentials.") _connect_status['fail'] = 1 def connection_fail_callback(data): """Network failure callback.""" _LOGGER.error("Could not establish a connection with the envisalink.") _connect_status['fail'] = 1 def connection_success_callback(data): """Callback for a successful connection.""" _LOGGER.info("Established a connection with the envisalink.") _connect_status['success'] = 1 def zones_updated_callback(data): """Handle zone timer updates.""" _LOGGER.info("Envisalink sent a zone update event. Updating zones...") dispatcher.send(signal=SIGNAL_ZONE_UPDATE, sender=None, zone=data) def alarm_data_updated_callback(data): """Handle non-alarm based info updates.""" _LOGGER.info("Envisalink sent new alarm info. Updating alarms...") dispatcher.send(signal=SIGNAL_KEYPAD_UPDATE, sender=None, partition=data) def partition_updated_callback(data): """Handle partition changes thrown by evl (including alarms).""" _LOGGER.info("The envisalink sent a partition update event.") dispatcher.send(signal=SIGNAL_PARTITION_UPDATE, sender=None, partition=data) def stop_envisalink(event): """Shutdown envisalink connection and thread on exit.""" _LOGGER.info("Shutting down envisalink.") EVL_CONTROLLER.stop() def start_envisalink(event): """Startup process for the Envisalink.""" hass.loop.call_soon_threadsafe(EVL_CONTROLLER.start) for _ in range(10): if 'success' in _connect_status: hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_envisalink) return True elif 'fail' in _connect_status: return False else: time.sleep(1) _LOGGER.error("Timeout occurred while establishing evl connection.") return False EVL_CONTROLLER.callback_zone_timer_dump = zones_updated_callback EVL_CONTROLLER.callback_zone_state_change = zones_updated_callback EVL_CONTROLLER.callback_partition_state_change = partition_updated_callback EVL_CONTROLLER.callback_keypad_update = alarm_data_updated_callback EVL_CONTROLLER.callback_login_failure = login_fail_callback EVL_CONTROLLER.callback_login_timeout = connection_fail_callback EVL_CONTROLLER.callback_login_success = connection_success_callback _result = start_envisalink(None) if not _result: return False # Load sub-components for Envisalink if _partitions: load_platform( hass, 'alarm_control_panel', 'envisalink', { CONF_PARTITIONS: _partitions, CONF_CODE: _code, CONF_PANIC: _panic_type }, base_config) load_platform(hass, 'sensor', 'envisalink', { CONF_PARTITIONS: _partitions, CONF_CODE: _code }, base_config) if _zones: load_platform(hass, 'binary_sensor', 'envisalink', {CONF_ZONES: _zones}, base_config) return True