Set up Almond Web to connect to HA (#28603)

* Set up Almond Web to connect to HA

* Add missing string

* Add type
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-11-12 11:01:19 -08:00
1 parent a1f2b6d402
commit 5961215e6e
5 files changed
+245 -14

No files matched your search

+55 -14
View File
@@ -10,16 +10,18 @@ from aiohttp import ClientSession, ClientError
from pyalmond import AlmondLocalAuth, AbstractAlmondWebAuth, WebAlmondAPI
import voluptuous as vol
from homeassistant import core
from homeassistant.const import CONF_TYPE, CONF_HOST
from homeassistant.core import HomeAssistant, CoreState
from homeassistant.const import CONF_TYPE, CONF_HOST, EVENT_HOMEASSISTANT_START
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.auth.const import GROUP_ID_ADMIN
from homeassistant.helpers import (
config_validation as cv,
config_entry_oauth2_flow,
event,
intent,
aiohttp_client,
storage,
network,
)
from homeassistant import config_entries
from homeassistant.components import conversation
@@ -33,6 +35,8 @@ CONF_CLIENT_SECRET = "client_secret"
STORAGE_VERSION = 1
STORAGE_KEY = DOMAIN
ALMOND_SETUP_DELAY = 30
DEFAULT_OAUTH2_HOST = "https://almond.stanford.edu"
DEFAULT_LOCAL_HOST = "http://localhost:3000"
@@ -93,7 +97,7 @@ async def async_setup(hass, config):
return True
async def async_setup_entry(hass, entry):
async def async_setup_entry(hass: HomeAssistant, entry: config_entries.ConfigEntry):
"""Set up Almond config entry."""
websession = aiohttp_client.async_get_clientsession(hass)
@@ -112,12 +116,50 @@ async def async_setup_entry(hass, entry):
api = WebAlmondAPI(auth)
agent = AlmondAgent(hass, api, entry)
# Hass.io does its own configuration of Almond.
if entry.data.get("is_hassio") or entry.data["type"] != TYPE_LOCAL:
conversation.async_set_agent(hass, agent)
return True
# Hass.io does its own configuration.
if not entry.data.get("is_hassio"):
# If we're not starting or local, set up Almond right away
if hass.state != CoreState.not_running or entry.data["type"] == TYPE_LOCAL:
await _configure_almond_for_ha(hass, entry, api)
# Configure Almond to connect to Home Assistant
else:
# OAuth2 implementations can potentially rely on the HA Cloud url.
# This url is not be available until 30 seconds after boot.
async def configure_almond(_now):
try:
await _configure_almond_for_ha(hass, entry, api)
except ConfigEntryNotReady:
_LOGGER.warning(
"Unable to configure Almond to connect to Home Assistant"
)
async def almond_hass_start(_event):
event.async_call_later(hass, ALMOND_SETUP_DELAY, configure_almond)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, almond_hass_start)
conversation.async_set_agent(hass, agent)
return True
async def _configure_almond_for_ha(
hass: HomeAssistant, entry: config_entries.ConfigEntry, api: WebAlmondAPI
):
"""Configure Almond to connect to HA."""
if entry.data["type"] == TYPE_OAUTH2:
# If we're connecting over OAuth2, we will only set up connection
# with Home Assistant if we're remotely accessible.
hass_url = network.async_get_external_url(hass)
else:
hass_url = hass.config.api.base_url
# If hass_url is None, we're not going to configure Almond to connect to HA.
if hass_url is None:
return
_LOGGER.debug("Configuring Almond to connect to Home Assistant at %s", hass_url)
store = storage.Store(hass, STORAGE_VERSION, STORAGE_KEY)
data = await store.async_load()
@@ -144,11 +186,11 @@ async def async_setup_entry(hass, entry):
# Store token in Almond
try:
with async_timeout.timeout(10):
with async_timeout.timeout(30):
await api.async_create_device(
{
"kind": "io.home-assistant",
"hassUrl": hass.config.api.base_url,
"hassUrl": hass_url,
"accessToken": access_token,
"refreshToken": "",
# 5 years from now in ms.
@@ -169,9 +211,6 @@ async def async_setup_entry(hass, entry):
if token.id != refresh_token.id:
await hass.auth.async_remove_refresh_token(token)
conversation.async_set_agent(hass, agent)
return True
async def async_unload_entry(hass, entry):
"""Unload Almond."""
@@ -203,7 +242,9 @@ class AlmondOAuth(AbstractAlmondWebAuth):
class AlmondAgent(conversation.AbstractConversationAgent):
"""Almond conversation agent."""
def __init__(self, hass: core.HomeAssistant, api: WebAlmondAPI, entry):
def __init__(
self, hass: HomeAssistant, api: WebAlmondAPI, entry: config_entries.ConfigEntry
):
"""Initialize the agent."""
self.hass = hass
self.api = api
@@ -1,5 +1,10 @@
{
"config": {
"step": {
"pick_implementation": {
"title": "Pick Authentication Method"
}
},
"abort": {
"already_setup": "You can only configure one Almond account.",
"cannot_connect": "Unable to connect to the Almond server.",
+38
View File
@@ -0,0 +1,38 @@
"""Network helpers."""
from typing import Optional, cast
from ipaddress import ip_address
import yarl
from homeassistant.core import HomeAssistant, callback
from homeassistant.loader import bind_hass
from homeassistant.util.network import is_local
@bind_hass
@callback
def async_get_external_url(hass: HomeAssistant) -> Optional[str]:
"""Get external url of this instance.
Note: currently it takes 30 seconds after Home Assistant starts for
cloud.async_remote_ui_url to work.
"""
if "cloud" in hass.config.components:
try:
return cast(str, hass.components.cloud.async_remote_ui_url())
except hass.components.cloud.CloudNotAvailable:
pass
if hass.config.api is None:
return None
base_url = yarl.URL(hass.config.api.base_url)
try:
if is_local(ip_address(base_url.host)):
return None
except ValueError:
# ip_address raises ValueError if host is not an IP address
pass
return str(base_url)