2019-11-19 13:00:59 +00:00
|
|
|
"""Config flow to configure the GeoNet NZ Volcano integration."""
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_RADIUS,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
CONF_UNIT_SYSTEM,
|
|
|
|
CONF_UNIT_SYSTEM_IMPERIAL,
|
|
|
|
CONF_UNIT_SYSTEM_METRIC,
|
|
|
|
)
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
|
|
|
from .const import DEFAULT_RADIUS, DEFAULT_SCAN_INTERVAL, DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def configured_instances(hass):
|
|
|
|
"""Return a set of configured GeoNet NZ Volcano instances."""
|
2020-04-04 18:05:15 +00:00
|
|
|
return {
|
2019-11-19 13:00:59 +00:00
|
|
|
f"{entry.data[CONF_LATITUDE]}, {entry.data[CONF_LONGITUDE]}"
|
|
|
|
for entry in hass.config_entries.async_entries(DOMAIN)
|
2020-04-04 18:05:15 +00:00
|
|
|
}
|
2019-11-19 13:00:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GeonetnzVolcanoFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a GeoNet NZ Volcano config flow."""
|
|
|
|
|
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
|
|
|
|
|
|
|
async def _show_form(self, errors=None):
|
|
|
|
"""Show the form to the user."""
|
|
|
|
data_schema = vol.Schema(
|
|
|
|
{vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS): cv.positive_int}
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user", data_schema=data_schema, errors=errors or {}
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
latitude = user_input.get(CONF_LATITUDE, self.hass.config.latitude)
|
|
|
|
user_input[CONF_LATITUDE] = latitude
|
|
|
|
longitude = user_input.get(CONF_LONGITUDE, self.hass.config.longitude)
|
|
|
|
user_input[CONF_LONGITUDE] = longitude
|
|
|
|
|
|
|
|
identifier = f"{user_input[CONF_LATITUDE]}, {user_input[CONF_LONGITUDE]}"
|
|
|
|
if identifier in configured_instances(self.hass):
|
2020-10-17 13:07:44 +00:00
|
|
|
return await self._show_form({"base": "already_configured"})
|
2019-11-19 13:00:59 +00:00
|
|
|
|
|
|
|
if self.hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL:
|
|
|
|
user_input[CONF_UNIT_SYSTEM] = CONF_UNIT_SYSTEM_IMPERIAL
|
|
|
|
else:
|
|
|
|
user_input[CONF_UNIT_SYSTEM] = CONF_UNIT_SYSTEM_METRIC
|
|
|
|
|
|
|
|
scan_interval = user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
2021-04-21 00:41:36 +00:00
|
|
|
user_input[CONF_SCAN_INTERVAL] = scan_interval.total_seconds()
|
2019-11-19 13:00:59 +00:00
|
|
|
|
|
|
|
return self.async_create_entry(title=identifier, data=user_input)
|