def async_setup(hass, config): """Setup DreamScreen.""" import pydreamscreen config = config.get(DOMAIN, {}) component = EntityComponent(_LOGGER, DOMAIN, hass) @asyncio.coroutine def async_handle_dreamscreen_services(service): """Reusable DreamScreen service caller.""" service_definition = SERVICE_TO_ATTRIBUTE.get(service.service) attribute = service_definition['attribute'] attribute_value = service.data.get(service_definition['param']) target_entities = component.async_extract_from_service(service) updates = [] for entity in target_entities: _LOGGER.debug("setting {} {} to {}".format( entity.entity_id, attribute, attribute_value )) setattr(entity.device, attribute, attribute_value) updates.append(entity.async_update_ha_state(True)) if updates: yield from asyncio.wait(updates, loop=hass.loop) for service_name in SERVICE_TO_ATTRIBUTE: schema = SERVICE_TO_ATTRIBUTE[service_name].get('schema') hass.services.async_register(DOMAIN, service_name, async_handle_dreamscreen_services, schema=schema) entities = [] entity_ids = [] for device in pydreamscreen.get_devices(): entity = DreamScreenEntity(device=device, current_ids=entity_ids) entity_ids.append(entity.entity_id) entities.append(entity) yield from component.async_add_entities(entities) return True
async def async_setup(hass, config): """Setup DreamScreen.""" import pydreamscreen config = config.get(DOMAIN, {}) component = EntityComponent(_LOGGER, DOMAIN, hass) async def async_handle_dreamscreen_services(service): """Reusable DreamScreen service caller.""" service_definition = SERVICE_TO_ATTRIBUTE.get(service.service) attribute = service_definition["attribute"] attribute_value = service.data.get(service_definition["param"]) target_entities = await component.async_extract_from_service(service) updates = [] for entity in target_entities: _LOGGER.debug("setting {} {} to {}".format(entity.entity_id, attribute, attribute_value)) setattr(entity.device, attribute, attribute_value) updates.append(entity.async_update_ha_state(True)) if updates: await asyncio.wait(updates, loop=hass.loop) for service_name in SERVICE_TO_ATTRIBUTE: schema = SERVICE_TO_ATTRIBUTE[service_name].get("schema") hass.services.async_register(DOMAIN, service_name, async_handle_dreamscreen_services, schema=schema) entities = [] entity_ids = [] timeout = config[TIMEOUT_CONF] configured = config[DEVICES_CONF] _LOGGER.debug("Discovery Timeout: %d" % timeout) _LOGGER.debug("Configured devices: %d" % len(configured)) if len(configured) > 0: for deviceConf in configured: deviceName = list(deviceConf.keys())[0] deviceInfo = deviceConf[deviceName] address = deviceInfo[DEVICE_ADDR] timeout = deviceInfo[TIMEOUT_CONF] _LOGGER.debug("Adding %s - %s [Timeout: %d]" % (deviceName, address, timeout)) device_state = pydreamscreen.get_state(ip=address, timeout=timeout) if device_state == None: _LOGGER.warn( "Failed to add device [%s] %s. Try setting a 'timeout' in the device config." % (address, deviceName)) else: _LOGGER.debug("Adding [%s] %s => State: %s" % (address, deviceName, device_state)) device = pydreamscreen.get_device(device_state) entity = DreamScreenEntity( device=device, current_ids=entity_ids, timeout=timeout, name=deviceName, ) entity_ids.append(entity.entity_id) entities.append(entity) else: _LOGGER.debug("DreamScreen will discover devices.") for device in pydreamscreen.get_devices(timeout): _LOGGER.info("Discovered device: %s" % device) entity = DreamScreenEntity(device=device, current_ids=entity_ids) entity_ids.append(entity.entity_id) entities.append(entity) await component.async_add_entities(entities) return True