hass-core/homeassistant/components/met/config_flow.py

64 lines
2.1 KiB
Python
Raw Normal View History

"""Config flow to configure Met component."""
import voluptuous as vol
from homeassistant import config_entries, data_entry_flow
from homeassistant.const import (
CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from .const import DOMAIN, HOME_LOCATION_NAME
@callback
def configured_instances(hass):
"""Return a set of configured SimpliSafe instances."""
return set(
entry.data[CONF_NAME]
for entry in hass.config_entries.async_entries(DOMAIN))
@config_entries.HANDLERS.register(DOMAIN)
class MetFlowHandler(data_entry_flow.FlowHandler):
"""Config flow for Met component."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
def __init__(self):
"""Init MetFlowHandler."""
self._errors = {}
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
self._errors = {}
if user_input is not None:
if user_input[CONF_NAME] not in configured_instances(self.hass):
return self.async_create_entry(
title=user_input[CONF_NAME],
data=user_input,
)
self._errors[CONF_NAME] = 'name_exists'
return await self._show_config_form(
name=HOME_LOCATION_NAME,
latitude=self.hass.config.latitude,
longitude=self.hass.config.longitude,
elevation=self.hass.config.elevation)
async def _show_config_form(self, name=None, latitude=None,
longitude=None, elevation=None):
"""Show the configuration form to edit location data."""
return self.async_show_form(
step_id='user',
data_schema=vol.Schema({
vol.Required(CONF_NAME, default=name): str,
vol.Required(CONF_LATITUDE, default=latitude): cv.latitude,
vol.Required(CONF_LONGITUDE, default=longitude): cv.longitude,
vol.Required(CONF_ELEVATION, default=elevation): int
}),
errors=self._errors,
)