Clean up OpenUV config flow (#17349)
* Cleaned up OpenUV config flow * Added proper listener removal * Added proper exception * Unnecessary exception message * Moved API key error to correct place * Member-requested changes (part 1) * Hound * Member-requested changes (part 2) * Cleanup * Fixed tests
This commit is contained in:
1 parent
73197c9a6c
commit
29c2b2fe79
6 files changed
+158
-125
No files matched your search
@@ -1,16 +1,15 @@
|
||||
"""Config flow to configure the OpenUV component."""
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY, CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE)
|
||||
CONF_API_KEY, CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE,
|
||||
CONF_SCAN_INTERVAL)
|
||||
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||
|
||||
|
||||
@callback
|
||||
@@ -33,6 +32,24 @@ class OpenUvFlowHandler(config_entries.ConfigFlow):
|
||||
"""Initialize the config flow."""
|
||||
pass
|
||||
|
||||
async def _show_form(self, errors=None):
|
||||
"""Show the form to the user."""
|
||||
data_schema = vol.Schema({
|
||||
vol.Required(CONF_API_KEY): str,
|
||||
vol.Optional(CONF_LATITUDE, default=self.hass.config.latitude):
|
||||
cv.latitude,
|
||||
vol.Optional(CONF_LONGITUDE, default=self.hass.config.longitude):
|
||||
cv.longitude,
|
||||
vol.Optional(CONF_ELEVATION, default=self.hass.config.elevation):
|
||||
vol.Coerce(float),
|
||||
})
|
||||
|
||||
return self.async_show_form(
|
||||
step_id='user',
|
||||
data_schema=data_schema,
|
||||
errors=errors if errors else {},
|
||||
)
|
||||
|
||||
async def async_step_import(self, import_config):
|
||||
"""Import a config entry from configuration.yaml."""
|
||||
return await self.async_step_user(import_config)
|
||||
@@ -41,34 +58,31 @@ class OpenUvFlowHandler(config_entries.ConfigFlow):
|
||||
"""Handle the start of the config flow."""
|
||||
from pyopenuv.util import validate_api_key
|
||||
|
||||
errors = {}
|
||||
if not user_input:
|
||||
return await self._show_form()
|
||||
|
||||
if user_input is not None:
|
||||
identifier = '{0}, {1}'.format(
|
||||
user_input.get(CONF_LATITUDE, self.hass.config.latitude),
|
||||
user_input.get(CONF_LONGITUDE, self.hass.config.longitude))
|
||||
latitude = user_input[CONF_LATITUDE]
|
||||
longitude = user_input[CONF_LONGITUDE]
|
||||
elevation = user_input[CONF_ELEVATION]
|
||||
|
||||
if identifier in configured_instances(self.hass):
|
||||
errors['base'] = 'identifier_exists'
|
||||
else:
|
||||
websession = aiohttp_client.async_get_clientsession(self.hass)
|
||||
api_key_validation = await validate_api_key(
|
||||
user_input[CONF_API_KEY], websession)
|
||||
if api_key_validation:
|
||||
return self.async_create_entry(
|
||||
title=identifier,
|
||||
data=user_input,
|
||||
)
|
||||
errors['base'] = 'invalid_api_key'
|
||||
identifier = '{0}, {1}'.format(latitude, longitude)
|
||||
if identifier in configured_instances(self.hass):
|
||||
return await self._show_form({CONF_LATITUDE: 'identifier_exists'})
|
||||
|
||||
data_schema = OrderedDict()
|
||||
data_schema[vol.Required(CONF_API_KEY)] = str
|
||||
data_schema[vol.Optional(CONF_LATITUDE)] = cv.latitude
|
||||
data_schema[vol.Optional(CONF_LONGITUDE)] = cv.longitude
|
||||
data_schema[vol.Optional(CONF_ELEVATION)] = vol.Coerce(float)
|
||||
websession = aiohttp_client.async_get_clientsession(self.hass)
|
||||
api_key_validation = await validate_api_key(
|
||||
user_input[CONF_API_KEY], websession)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id='user',
|
||||
data_schema=vol.Schema(data_schema),
|
||||
errors=errors,
|
||||
)
|
||||
if not api_key_validation:
|
||||
return await self._show_form({CONF_API_KEY: 'invalid_api_key'})
|
||||
|
||||
scan_interval = user_input.get(
|
||||
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
||||
user_input.update({
|
||||
CONF_LATITUDE: latitude,
|
||||
CONF_LONGITUDE: longitude,
|
||||
CONF_ELEVATION: elevation,
|
||||
CONF_SCAN_INTERVAL: scan_interval.seconds,
|
||||
})
|
||||
|
||||
return self.async_create_entry(title=identifier, data=user_input)
|
||||
Reference in new issue
Block a user