2021-02-24 08:34:27 +00:00
|
|
|
"""Config flow for ClimaCell integration."""
|
2021-03-17 22:43:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-02-24 08:34:27 +00:00
|
|
|
import logging
|
2021-03-17 22:43:55 +00:00
|
|
|
from typing import Any
|
2021-02-24 08:34:27 +00:00
|
|
|
|
2021-04-05 17:39:39 +00:00
|
|
|
from pyclimacell import ClimaCellV3
|
2021-02-24 08:34:27 +00:00
|
|
|
from pyclimacell.exceptions import (
|
|
|
|
CantConnectException,
|
|
|
|
InvalidAPIKeyException,
|
|
|
|
RateLimitedException,
|
|
|
|
)
|
2021-04-05 17:39:39 +00:00
|
|
|
from pyclimacell.pyclimacell import ClimaCellV4
|
2021-02-24 08:34:27 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries, core
|
2021-04-05 17:39:39 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_API_VERSION,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_NAME,
|
|
|
|
)
|
2021-04-17 10:48:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-04-29 11:40:51 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2021-02-24 08:34:27 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2021-04-05 17:39:39 +00:00
|
|
|
from .const import (
|
|
|
|
CC_ATTR_TEMPERATURE,
|
|
|
|
CC_V3_ATTR_TEMPERATURE,
|
|
|
|
CONF_TIMESTEP,
|
|
|
|
DEFAULT_NAME,
|
|
|
|
DEFAULT_TIMESTEP,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2021-02-24 08:34:27 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def _get_config_schema(
|
2021-03-17 22:43:55 +00:00
|
|
|
hass: core.HomeAssistant, input_dict: dict[str, Any] = None
|
2021-02-24 08:34:27 +00:00
|
|
|
) -> vol.Schema:
|
|
|
|
"""
|
|
|
|
Return schema defaults for init step based on user input/config dict.
|
|
|
|
|
|
|
|
Retain info already provided for future form views by setting them as
|
|
|
|
defaults in schema.
|
|
|
|
"""
|
|
|
|
if input_dict is None:
|
|
|
|
input_dict = {}
|
|
|
|
|
|
|
|
return vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(
|
|
|
|
CONF_NAME, default=input_dict.get(CONF_NAME, DEFAULT_NAME)
|
|
|
|
): str,
|
|
|
|
vol.Required(CONF_API_KEY, default=input_dict.get(CONF_API_KEY)): str,
|
2021-04-05 17:39:39 +00:00
|
|
|
vol.Required(CONF_API_VERSION, default=4): vol.In([3, 4]),
|
2021-02-24 08:34:27 +00:00
|
|
|
vol.Inclusive(
|
|
|
|
CONF_LATITUDE,
|
|
|
|
"location",
|
|
|
|
default=input_dict.get(CONF_LATITUDE, hass.config.latitude),
|
|
|
|
): cv.latitude,
|
|
|
|
vol.Inclusive(
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
"location",
|
|
|
|
default=input_dict.get(CONF_LONGITUDE, hass.config.longitude),
|
|
|
|
): cv.longitude,
|
|
|
|
},
|
|
|
|
extra=vol.REMOVE_EXTRA,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-04-17 10:48:03 +00:00
|
|
|
def _get_unique_id(hass: HomeAssistant, input_dict: dict[str, Any]):
|
2021-02-24 08:34:27 +00:00
|
|
|
"""Return unique ID from config data."""
|
|
|
|
return (
|
|
|
|
f"{input_dict[CONF_API_KEY]}"
|
|
|
|
f"_{input_dict.get(CONF_LATITUDE, hass.config.latitude)}"
|
|
|
|
f"_{input_dict.get(CONF_LONGITUDE, hass.config.longitude)}"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ClimaCellOptionsConfigFlow(config_entries.OptionsFlow):
|
|
|
|
"""Handle ClimaCell options."""
|
|
|
|
|
|
|
|
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
|
|
|
"""Initialize ClimaCell options flow."""
|
|
|
|
self._config_entry = config_entry
|
|
|
|
|
2021-04-29 11:40:51 +00:00
|
|
|
async def async_step_init(self, user_input: dict[str, Any] = None) -> FlowResult:
|
2021-02-24 08:34:27 +00:00
|
|
|
"""Manage the ClimaCell options."""
|
|
|
|
if user_input is not None:
|
|
|
|
return self.async_create_entry(title="", data=user_input)
|
|
|
|
|
|
|
|
options_schema = {
|
|
|
|
vol.Required(
|
|
|
|
CONF_TIMESTEP,
|
|
|
|
default=self._config_entry.options.get(CONF_TIMESTEP, DEFAULT_TIMESTEP),
|
2021-04-05 17:39:39 +00:00
|
|
|
): vol.In([1, 5, 15, 30]),
|
2021-02-24 08:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="init", data_schema=vol.Schema(options_schema)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ClimaCellConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for ClimaCell Weather API."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@callback
|
|
|
|
def async_get_options_flow(
|
|
|
|
config_entry: config_entries.ConfigEntry,
|
|
|
|
) -> ClimaCellOptionsConfigFlow:
|
|
|
|
"""Get the options flow for this handler."""
|
|
|
|
return ClimaCellOptionsConfigFlow(config_entry)
|
|
|
|
|
2021-04-29 11:40:51 +00:00
|
|
|
async def async_step_user(self, user_input: dict[str, Any] = None) -> FlowResult:
|
2021-02-24 08:34:27 +00:00
|
|
|
"""Handle the initial step."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
await self.async_set_unique_id(
|
|
|
|
unique_id=_get_unique_id(self.hass, user_input)
|
|
|
|
)
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
|
|
|
|
try:
|
2021-04-05 17:39:39 +00:00
|
|
|
if user_input[CONF_API_VERSION] == 3:
|
|
|
|
api_class = ClimaCellV3
|
|
|
|
field = CC_V3_ATTR_TEMPERATURE
|
|
|
|
else:
|
|
|
|
api_class = ClimaCellV4
|
|
|
|
field = CC_ATTR_TEMPERATURE
|
|
|
|
await api_class(
|
2021-02-24 08:34:27 +00:00
|
|
|
user_input[CONF_API_KEY],
|
|
|
|
str(user_input.get(CONF_LATITUDE, self.hass.config.latitude)),
|
|
|
|
str(user_input.get(CONF_LONGITUDE, self.hass.config.longitude)),
|
|
|
|
session=async_get_clientsession(self.hass),
|
2021-04-05 17:39:39 +00:00
|
|
|
).realtime([field])
|
2021-02-24 08:34:27 +00:00
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=user_input[CONF_NAME], data=user_input
|
|
|
|
)
|
|
|
|
except CantConnectException:
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except InvalidAPIKeyException:
|
|
|
|
errors[CONF_API_KEY] = "invalid_api_key"
|
|
|
|
except RateLimitedException:
|
|
|
|
errors[CONF_API_KEY] = "rate_limited"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=_get_config_schema(self.hass, user_input),
|
|
|
|
errors=errors,
|
|
|
|
)
|