2019-01-28 23:35:39 +00:00
|
|
|
"""Config flow to configure the Ambient PWS component."""
|
2019-12-05 11:50:53 +00:00
|
|
|
from aioambient import Client
|
|
|
|
from aioambient.errors import AmbientError
|
2019-01-28 23:35:39 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_API_KEY
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers import aiohttp_client
|
|
|
|
|
|
|
|
from .const import CONF_APP_KEY, DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def configured_instances(hass):
|
|
|
|
"""Return a set of configured Ambient PWS instances."""
|
|
|
|
return set(
|
2019-07-31 19:25:30 +00:00
|
|
|
entry.data[CONF_APP_KEY] for entry in hass.config_entries.async_entries(DOMAIN)
|
|
|
|
)
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@config_entries.HANDLERS.register(DOMAIN)
|
|
|
|
class AmbientStationFlowHandler(config_entries.ConfigFlow):
|
|
|
|
"""Handle an Ambient PWS config flow."""
|
|
|
|
|
2019-07-31 17:31:40 +00:00
|
|
|
VERSION = 2
|
2019-01-28 23:35:39 +00:00
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH
|
|
|
|
|
|
|
|
async def _show_form(self, errors=None):
|
|
|
|
"""Show the form to the user."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data_schema = vol.Schema(
|
|
|
|
{vol.Required(CONF_API_KEY): str, vol.Required(CONF_APP_KEY): str}
|
|
|
|
)
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="user", data_schema=data_schema, errors=errors if errors else {}
|
2019-01-28 23:35:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_import(self, import_config):
|
|
|
|
"""Import a config entry from configuration.yaml."""
|
|
|
|
return await self.async_step_user(import_config)
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle the start of the config flow."""
|
|
|
|
|
|
|
|
if not user_input:
|
|
|
|
return await self._show_form()
|
|
|
|
|
|
|
|
if user_input[CONF_APP_KEY] in configured_instances(self.hass):
|
2019-07-31 19:25:30 +00:00
|
|
|
return await self._show_form({CONF_APP_KEY: "identifier_exists"})
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
client = Client(user_input[CONF_API_KEY], user_input[CONF_APP_KEY], session)
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
devices = await client.api.get_devices()
|
|
|
|
except AmbientError:
|
2019-07-31 19:25:30 +00:00
|
|
|
return await self._show_form({"base": "invalid_key"})
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
if not devices:
|
2019-07-31 19:25:30 +00:00
|
|
|
return await self._show_form({"base": "no_devices"})
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
# The Application Key (which identifies each config entry) is too long
|
|
|
|
# to show nicely in the UI, so we take the first 12 characters (similar
|
|
|
|
# to how GitHub does it):
|
|
|
|
return self.async_create_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
title=user_input[CONF_APP_KEY][:12], data=user_input
|
|
|
|
)
|