2020-11-11 19:36:16 +00:00
|
|
|
"""Config flow for SpaceX Launches and Starman."""
|
2022-06-13 11:17:59 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-11-11 19:36:16 +00:00
|
|
|
import logging
|
|
|
|
|
2021-02-06 07:05:39 +00:00
|
|
|
from aiohttp import ClientError
|
2020-11-11 19:36:16 +00:00
|
|
|
from auroranoaa import AuroraForecast
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers import aiohttp_client
|
2022-11-25 15:09:09 +00:00
|
|
|
from homeassistant.helpers.schema_config_entry_flow import (
|
|
|
|
SchemaFlowFormStep,
|
|
|
|
SchemaOptionsFlowHandler,
|
|
|
|
)
|
2020-11-11 19:36:16 +00:00
|
|
|
|
2021-03-30 04:02:56 +00:00
|
|
|
from .const import CONF_THRESHOLD, DEFAULT_NAME, DEFAULT_THRESHOLD, DOMAIN
|
2020-11-11 19:36:16 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2022-11-25 15:09:09 +00:00
|
|
|
OPTIONS_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_THRESHOLD, default=DEFAULT_THRESHOLD): vol.All(
|
|
|
|
vol.Coerce(int), vol.Range(min=0, max=100)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
OPTIONS_FLOW = {
|
|
|
|
"init": SchemaFlowFormStep(OPTIONS_SCHEMA),
|
|
|
|
}
|
|
|
|
|
2020-11-11 19:36:16 +00:00
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for NOAA Aurora Integration."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@callback
|
2022-06-13 11:17:59 +00:00
|
|
|
def async_get_options_flow(
|
|
|
|
config_entry: config_entries.ConfigEntry,
|
2022-11-25 15:09:09 +00:00
|
|
|
) -> SchemaOptionsFlowHandler:
|
2020-11-11 19:36:16 +00:00
|
|
|
"""Get the options flow for this handler."""
|
2022-11-25 15:09:09 +00:00
|
|
|
return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
|
2020-11-11 19:36:16 +00:00
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle the initial step."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
name = user_input[CONF_NAME]
|
|
|
|
longitude = user_input[CONF_LONGITUDE]
|
|
|
|
latitude = user_input[CONF_LATITUDE]
|
|
|
|
|
|
|
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
|
|
|
api = AuroraForecast(session=session)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await api.get_forecast_data(longitude, latitude)
|
2021-02-06 07:05:39 +00:00
|
|
|
except ClientError:
|
2020-11-11 19:36:16 +00:00
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
else:
|
|
|
|
await self.async_set_unique_id(
|
2021-02-06 07:05:39 +00:00
|
|
|
f"{user_input[CONF_LONGITUDE]}_{user_input[CONF_LATITUDE]}"
|
2020-11-11 19:36:16 +00:00
|
|
|
)
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=f"Aurora - {name}", data=user_input
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_NAME, default=DEFAULT_NAME): str,
|
|
|
|
vol.Required(
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
default=self.hass.config.longitude,
|
|
|
|
): vol.All(
|
|
|
|
vol.Coerce(float),
|
|
|
|
vol.Range(min=-180, max=180),
|
|
|
|
),
|
|
|
|
vol.Required(
|
|
|
|
CONF_LATITUDE,
|
|
|
|
default=self.hass.config.latitude,
|
|
|
|
): vol.All(
|
|
|
|
vol.Coerce(float),
|
|
|
|
vol.Range(min=-90, max=90),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=errors,
|
|
|
|
)
|