async def async_setup(hass, config): """Initialize the Home Assistant cloud.""" from hass_nabucasa import Cloud from .client import CloudClient # Process configs if DOMAIN in config: kwargs = dict(config[DOMAIN]) else: kwargs = {CONF_MODE: DEFAULT_MODE} # Alexa/Google custom config alexa_conf = kwargs.pop(CONF_ALEXA, None) or ALEXA_SCHEMA({}) google_conf = kwargs.pop(CONF_GOOGLE_ACTIONS, None) or GACTIONS_SCHEMA({}) # Cloud settings prefs = CloudPreferences(hass) await prefs.async_initialize() # Cloud user if not prefs.cloud_user: user = await hass.auth.async_create_system_user( 'Home Assistant Cloud', [GROUP_ID_ADMIN]) await prefs.async_update(cloud_user=user.id) # Initialize Cloud websession = hass.helpers.aiohttp_client.async_get_clientsession() client = CloudClient(hass, prefs, websession, alexa_conf, google_conf) cloud = hass.data[DOMAIN] = Cloud(client, **kwargs) async def _startup(event): """Startup event.""" await cloud.start() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _startup) async def _shutdown(event): """Shutdown event.""" await cloud.stop() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown) async def _service_handler(service): """Handle service for cloud.""" if service.service == SERVICE_REMOTE_CONNECT: await cloud.remote.connect() await prefs.async_update(remote_enabled=True) elif service.service == SERVICE_REMOTE_DISCONNECT: await cloud.remote.disconnect() await prefs.async_update(remote_enabled=False) hass.services.async_register( DOMAIN, SERVICE_REMOTE_CONNECT, _service_handler) hass.services.async_register( DOMAIN, SERVICE_REMOTE_DISCONNECT, _service_handler) await http_api.async_setup(hass) hass.async_create_task(hass.helpers.discovery.async_load_platform( 'binary_sensor', DOMAIN, {}, config)) return True
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Initialize the Home Assistant cloud.""" # Process configs if DOMAIN in config: kwargs = dict(config[DOMAIN]) else: kwargs = {CONF_MODE: DEFAULT_MODE} # Alexa/Google custom config alexa_conf = kwargs.pop(CONF_ALEXA, None) or ALEXA_SCHEMA({}) google_conf = kwargs.pop(CONF_GOOGLE_ACTIONS, None) or GACTIONS_SCHEMA({}) # Cloud settings prefs = CloudPreferences(hass) await prefs.async_initialize() # Initialize Cloud websession = async_get_clientsession(hass) client = CloudClient(hass, prefs, websession, alexa_conf, google_conf) cloud = hass.data[DOMAIN] = Cloud(client, **kwargs) async def _shutdown(event): """Shutdown event.""" await cloud.stop() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown) _remote_handle_prefs_updated(cloud) async def _service_handler(service: ServiceCall) -> None: """Handle service for cloud.""" if service.service == SERVICE_REMOTE_CONNECT: await prefs.async_update(remote_enabled=True) elif service.service == SERVICE_REMOTE_DISCONNECT: await prefs.async_update(remote_enabled=False) hass.helpers.service.async_register_admin_service(DOMAIN, SERVICE_REMOTE_CONNECT, _service_handler) hass.helpers.service.async_register_admin_service( DOMAIN, SERVICE_REMOTE_DISCONNECT, _service_handler) loaded = False async def _on_connect(): """Discover RemoteUI binary sensor.""" nonlocal loaded # Prevent multiple discovery if loaded: return loaded = True await hass.helpers.discovery.async_load_platform( Platform.BINARY_SENSOR, DOMAIN, {}, config) await hass.helpers.discovery.async_load_platform( Platform.STT, DOMAIN, {}, config) await hass.helpers.discovery.async_load_platform( Platform.TTS, DOMAIN, {}, config) cloud.iot.register_on_connect(_on_connect) await cloud.initialize() await http_api.async_setup(hass) account_link.async_setup(hass) return True
async def async_setup(hass, config): """Initialize the Home Assistant cloud.""" from hass_nabucasa import Cloud from .client import CloudClient # Process configs if DOMAIN in config: kwargs = dict(config[DOMAIN]) else: kwargs = {CONF_MODE: DEFAULT_MODE} # Alexa/Google custom config alexa_conf = kwargs.pop(CONF_ALEXA, None) or ALEXA_SCHEMA({}) google_conf = kwargs.pop(CONF_GOOGLE_ACTIONS, None) or GACTIONS_SCHEMA({}) # Cloud settings prefs = CloudPreferences(hass) await prefs.async_initialize() # Cloud user user = None if prefs.cloud_user: # Fetch the user. It can happen that the user no longer exists if # an image was restored without restoring the cloud prefs. user = await hass.auth.async_get_user(prefs.cloud_user) if user is None: user = await hass.auth.async_create_system_user( "Home Assistant Cloud", [GROUP_ID_ADMIN] ) await prefs.async_update(cloud_user=user.id) # Initialize Cloud websession = hass.helpers.aiohttp_client.async_get_clientsession() client = CloudClient(hass, prefs, websession, alexa_conf, google_conf) cloud = hass.data[DOMAIN] = Cloud(client, **kwargs) async def _startup(event): """Startup event.""" await cloud.start() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _startup) async def _shutdown(event): """Shutdown event.""" await cloud.stop() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown) async def _service_handler(service): """Handle service for cloud.""" if service.service == SERVICE_REMOTE_CONNECT: await cloud.remote.connect() await prefs.async_update(remote_enabled=True) elif service.service == SERVICE_REMOTE_DISCONNECT: await cloud.remote.disconnect() await prefs.async_update(remote_enabled=False) hass.helpers.service.async_register_admin_service( DOMAIN, SERVICE_REMOTE_CONNECT, _service_handler ) hass.helpers.service.async_register_admin_service( DOMAIN, SERVICE_REMOTE_DISCONNECT, _service_handler ) loaded_binary_sensor = False async def _on_connect(): """Discover RemoteUI binary sensor.""" nonlocal loaded_binary_sensor if loaded_binary_sensor: return loaded_binary_sensor = True hass.async_create_task( hass.helpers.discovery.async_load_platform( "binary_sensor", DOMAIN, {}, config ) ) cloud.iot.register_on_connect(_on_connect) await http_api.async_setup(hass) return True