2018-04-26 21:59:22 +00:00
|
|
|
"""Config flow to configure zone component."""
|
|
|
|
|
2019-09-29 17:07:49 +00:00
|
|
|
from typing import Set
|
|
|
|
|
2018-04-26 21:59:22 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-09-17 08:12:46 +00:00
|
|
|
from homeassistant import config_entries
|
2018-04-26 21:59:22 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_ICON,
|
|
|
|
CONF_RADIUS,
|
|
|
|
)
|
2018-04-26 21:59:22 +00:00
|
|
|
from homeassistant.core import callback
|
2019-09-29 17:07:49 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2018-04-26 21:59:22 +00:00
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
|
|
|
from .const import CONF_PASSIVE, DOMAIN, HOME_ZONE
|
|
|
|
|
|
|
|
|
2019-10-19 18:35:57 +00:00
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
2019-09-29 17:07:49 +00:00
|
|
|
|
|
|
|
|
2018-04-26 21:59:22 +00:00
|
|
|
@callback
|
2019-09-29 17:07:49 +00:00
|
|
|
def configured_zones(hass: HomeAssistantType) -> Set[str]:
|
2019-04-01 04:30:45 +00:00
|
|
|
"""Return a set of the configured zones."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return set(
|
|
|
|
(slugify(entry.data[CONF_NAME]))
|
2019-09-29 17:07:49 +00:00
|
|
|
for entry in (
|
|
|
|
hass.config_entries.async_entries(DOMAIN) if hass.config_entries else []
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-04-26 21:59:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@config_entries.HANDLERS.register(DOMAIN)
|
2018-09-17 08:12:46 +00:00
|
|
|
class ZoneFlowHandler(config_entries.ConfigFlow):
|
2018-04-26 21:59:22 +00:00
|
|
|
"""Zone config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize zone configuration flow."""
|
|
|
|
pass
|
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
return await self.async_step_init(user_input)
|
|
|
|
|
2018-04-26 21:59:22 +00:00
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
"""Handle a flow start."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
name = slugify(user_input[CONF_NAME])
|
|
|
|
if name not in configured_zones(self.hass) and name != HOME_ZONE:
|
|
|
|
return self.async_create_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
title=user_input[CONF_NAME], data=user_input
|
2018-04-26 21:59:22 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
errors["base"] = "name_exists"
|
2018-04-26 21:59:22 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="init",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_NAME): str,
|
|
|
|
vol.Required(CONF_LATITUDE): cv.latitude,
|
|
|
|
vol.Required(CONF_LONGITUDE): cv.longitude,
|
|
|
|
vol.Optional(CONF_RADIUS): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_ICON): str,
|
|
|
|
vol.Optional(CONF_PASSIVE): bool,
|
|
|
|
}
|
|
|
|
),
|
2018-04-26 21:59:22 +00:00
|
|
|
errors=errors,
|
|
|
|
)
|