async def async_setup(hass, config): """Track states and offer events for covers.""" component = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_COVERS) await component.async_setup(config) component.async_register_entity_service(SERVICE_OPEN_COVER, ENTITY_SERVICE_SCHEMA, "async_open_cover") component.async_register_entity_service(SERVICE_CLOSE_COVER, ENTITY_SERVICE_SCHEMA, "async_close_cover") component.async_register_entity_service( SERVICE_SET_COVER_POSITION, COVER_SET_COVER_POSITION_SCHEMA, "async_set_cover_position", ) component.async_register_entity_service(SERVICE_STOP_COVER, ENTITY_SERVICE_SCHEMA, "async_stop_cover") component.async_register_entity_service(SERVICE_TOGGLE, ENTITY_SERVICE_SCHEMA, "async_toggle") component.async_register_entity_service(SERVICE_OPEN_COVER_TILT, ENTITY_SERVICE_SCHEMA, "async_open_cover_tilt") component.async_register_entity_service(SERVICE_CLOSE_COVER_TILT, ENTITY_SERVICE_SCHEMA, "async_close_cover_tilt") component.async_register_entity_service(SERVICE_STOP_COVER_TILT, ENTITY_SERVICE_SCHEMA, "async_stop_cover_tilt") component.async_register_entity_service( SERVICE_SET_COVER_TILT_POSITION, COVER_SET_COVER_TILT_POSITION_SCHEMA, "async_set_cover_tilt_position", ) component.async_register_entity_service(SERVICE_TOGGLE_COVER_TILT, ENTITY_SERVICE_SCHEMA, "async_toggle_tilt") hass.helpers.intent.async_register( intent.ServiceIntentHandler(INTENT_OPEN_COVER, DOMAIN, SERVICE_OPEN_COVER, "Opened {}")) hass.helpers.intent.async_register( intent.ServiceIntentHandler(INTENT_CLOSE_COVER, DOMAIN, SERVICE_CLOSE_COVER, "Closed {}")) return True
async def async_setup(hass: HomeAssistant, config: dict): """Set up the Intent component.""" hass.http.register_view(IntentHandleView()) await integration_platform.async_process_integration_platforms( hass, DOMAIN, _async_process_intent ) hass.helpers.intent.async_register( intent.ServiceIntentHandler( intent.INTENT_TURN_ON, HA_DOMAIN, SERVICE_TURN_ON, "Turned {} on" ) ) hass.helpers.intent.async_register( intent.ServiceIntentHandler( intent.INTENT_TURN_OFF, HA_DOMAIN, SERVICE_TURN_OFF, "Turned {} off" ) ) hass.helpers.intent.async_register( intent.ServiceIntentHandler( intent.INTENT_TOGGLE, HA_DOMAIN, SERVICE_TOGGLE, "Toggled {}" ) ) return True
async def async_setup_intents(hass: HomeAssistant) -> None: """Set up the cover intents.""" hass.helpers.intent.async_register( intent.ServiceIntentHandler(INTENT_OPEN_COVER, DOMAIN, SERVICE_OPEN_COVER, "Opened {}")) hass.helpers.intent.async_register( intent.ServiceIntentHandler(INTENT_CLOSE_COVER, DOMAIN, SERVICE_CLOSE_COVER, "Closed {}"))
async def async_setup(hass, config): """Track states and offer events for covers.""" component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_COVERS) await component.async_setup(config) async def async_handle_cover_service(service): """Handle calls to the cover services.""" covers = component.async_extract_from_service(service) method = SERVICE_TO_METHOD.get(service.service) params = service.data.copy() params.pop(ATTR_ENTITY_ID, None) # call method update_tasks = [] for cover in covers: await getattr(cover, method['method'])(**params) if not cover.should_poll: continue update_tasks.append(cover.async_update_ha_state(True)) if update_tasks: await asyncio.wait(update_tasks, loop=hass.loop) for service_name in SERVICE_TO_METHOD: schema = SERVICE_TO_METHOD[service_name].get('schema', COVER_SERVICE_SCHEMA) hass.services.async_register(DOMAIN, service_name, async_handle_cover_service, schema=schema) hass.helpers.intent.async_register( intent.ServiceIntentHandler(INTENT_OPEN_COVER, DOMAIN, SERVICE_OPEN_COVER, "Opened {}")) hass.helpers.intent.async_register( intent.ServiceIntentHandler(INTENT_CLOSE_COVER, DOMAIN, SERVICE_CLOSE_COVER, "Closed {}")) return True
def async_setup(hass: ha.HomeAssistant, config: dict) -> Awaitable[bool]: """Set up general services related to Home Assistant.""" @asyncio.coroutine def async_handle_turn_service(service): """Handle calls to homeassistant.turn_on/off.""" entity_ids = extract_entity_ids(hass, service) # Generic turn on/off method requires entity id if not entity_ids: _LOGGER.error( "homeassistant/%s cannot be called without entity_id", service.service) return # Group entity_ids by domain. groupby requires sorted data. by_domain = it.groupby(sorted(entity_ids), lambda item: ha.split_entity_id(item)[0]) tasks = [] for domain, ent_ids in by_domain: # We want to block for all calls and only return when all calls # have been processed. If a service does not exist it causes a 10 # second delay while we're blocking waiting for a response. # But services can be registered on other HA instances that are # listening to the bus too. So as an in between solution, we'll # block only if the service is defined in the current HA instance. blocking = hass.services.has_service(domain, service.service) # Create a new dict for this call data = dict(service.data) # ent_ids is a generator, convert it to a list. data[ATTR_ENTITY_ID] = list(ent_ids) tasks.append( hass.services.async_call(domain, service.service, data, blocking)) yield from asyncio.wait(tasks, loop=hass.loop) hass.services.async_register(ha.DOMAIN, SERVICE_TURN_OFF, async_handle_turn_service) hass.services.async_register(ha.DOMAIN, SERVICE_TURN_ON, async_handle_turn_service) hass.services.async_register(ha.DOMAIN, SERVICE_TOGGLE, async_handle_turn_service) hass.helpers.intent.async_register( intent.ServiceIntentHandler(intent.INTENT_TURN_ON, ha.DOMAIN, SERVICE_TURN_ON, "Turned {} on")) hass.helpers.intent.async_register( intent.ServiceIntentHandler(intent.INTENT_TURN_OFF, ha.DOMAIN, SERVICE_TURN_OFF, "Turned {} off")) hass.helpers.intent.async_register( intent.ServiceIntentHandler(intent.INTENT_TOGGLE, ha.DOMAIN, SERVICE_TOGGLE, "Toggled {}")) @asyncio.coroutine def async_handle_core_service(call): """Service handler for handling core services.""" if call.service == SERVICE_HOMEASSISTANT_STOP: hass.async_create_task(hass.async_stop()) return try: errors = yield from conf_util.async_check_ha_config_file(hass) except HomeAssistantError: return if errors: _LOGGER.error(errors) hass.components.persistent_notification.async_create( "Config error. See dev-info panel for details.", "Config validating", "{0}.check_config".format(ha.DOMAIN)) return if call.service == SERVICE_HOMEASSISTANT_RESTART: hass.async_create_task(hass.async_stop(RESTART_EXIT_CODE)) hass.services.async_register(ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP, async_handle_core_service) hass.services.async_register(ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART, async_handle_core_service) hass.services.async_register(ha.DOMAIN, SERVICE_CHECK_CONFIG, async_handle_core_service) @asyncio.coroutine def async_handle_reload_config(call): """Service handler for reloading core config.""" try: conf = yield from conf_util.async_hass_config_yaml(hass) except HomeAssistantError as err: _LOGGER.error(err) return yield from conf_util.async_process_ha_core_config( hass, conf.get(ha.DOMAIN) or {}) hass.services.async_register(ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, async_handle_reload_config) return True
async def async_setup(hass: ha.HomeAssistant, config: dict) -> Awaitable[bool]: """Set up general services related to Home Assistant.""" async def async_handle_turn_service(service): """Handle calls to homeassistant.turn_on/off.""" entity_ids = await async_extract_entity_ids(hass, service) # Generic turn on/off method requires entity id if not entity_ids: _LOGGER.error( "homeassistant/%s cannot be called without entity_id", service.service ) return # Group entity_ids by domain. groupby requires sorted data. by_domain = it.groupby( sorted(entity_ids), lambda item: ha.split_entity_id(item)[0] ) tasks = [] for domain, ent_ids in by_domain: # We want to block for all calls and only return when all calls # have been processed. If a service does not exist it causes a 10 # second delay while we're blocking waiting for a response. # But services can be registered on other HA instances that are # listening to the bus too. So as an in between solution, we'll # block only if the service is defined in the current HA instance. blocking = hass.services.has_service(domain, service.service) # Create a new dict for this call data = dict(service.data) # ent_ids is a generator, convert it to a list. data[ATTR_ENTITY_ID] = list(ent_ids) tasks.append( hass.services.async_call(domain, service.service, data, blocking) ) await asyncio.wait(tasks) hass.services.async_register(ha.DOMAIN, SERVICE_TURN_OFF, async_handle_turn_service) hass.services.async_register(ha.DOMAIN, SERVICE_TURN_ON, async_handle_turn_service) hass.services.async_register(ha.DOMAIN, SERVICE_TOGGLE, async_handle_turn_service) hass.helpers.intent.async_register( intent.ServiceIntentHandler( intent.INTENT_TURN_ON, ha.DOMAIN, SERVICE_TURN_ON, "Turned {} on" ) ) hass.helpers.intent.async_register( intent.ServiceIntentHandler( intent.INTENT_TURN_OFF, ha.DOMAIN, SERVICE_TURN_OFF, "Turned {} off" ) ) hass.helpers.intent.async_register( intent.ServiceIntentHandler( intent.INTENT_TOGGLE, ha.DOMAIN, SERVICE_TOGGLE, "Toggled {}" ) ) async def async_handle_core_service(call): """Service handler for handling core services.""" # ais dom ais_command = None if "ais_command" in call.data: ais_command = call.data["ais_command"] if call.service == SERVICE_HOMEASSISTANT_STOP: hass.async_create_task(hass.async_stop(ais_command=ais_command)) return try: errors = await conf_util.async_check_ha_config_file(hass) except HomeAssistantError: return if errors: _LOGGER.error(errors) hass.components.persistent_notification.async_create( "Config error. See [the logs](/developer-tools/logs) for details.", "Config validating", f"{ha.DOMAIN}.check_config", ) return if call.service == SERVICE_HOMEASSISTANT_RESTART: hass.async_create_task( hass.async_stop(RESTART_EXIT_CODE, ais_command=ais_command) ) async def async_handle_update_service(call): """Service handler for updating an entity.""" tasks = [ hass.helpers.entity_component.async_update_entity(entity) for entity in call.data[ATTR_ENTITY_ID] ] if tasks: await asyncio.wait(tasks) hass.services.async_register( ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP, async_handle_core_service ) hass.services.async_register( ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART, async_handle_core_service ) hass.services.async_register( ha.DOMAIN, SERVICE_CHECK_CONFIG, async_handle_core_service ) hass.services.async_register( ha.DOMAIN, SERVICE_UPDATE_ENTITY, async_handle_update_service, schema=SCHEMA_UPDATE_ENTITY, ) async def async_handle_reload_config(call): """Service handler for reloading core config.""" try: conf = await conf_util.async_hass_config_yaml(hass) except HomeAssistantError as err: _LOGGER.error(err) return # auth only processed during startup await conf_util.async_process_ha_core_config(hass, conf.get(ha.DOMAIN) or {}) hass.helpers.service.async_register_admin_service( ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, async_handle_reload_config ) async def async_set_location(call): """Service handler to set location.""" await hass.config.async_update( latitude=call.data[ATTR_LATITUDE], longitude=call.data[ATTR_LONGITUDE] ) hass.helpers.service.async_register_admin_service( ha.DOMAIN, SERVICE_SET_LOCATION, async_set_location, vol.Schema({ATTR_LATITUDE: cv.latitude, ATTR_LONGITUDE: cv.longitude}), ) return True