2020-01-11 11:20:00 +00:00
|
|
|
"""Config flow for Netatmo."""
|
2021-07-21 21:36:57 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
import logging
|
2020-08-04 18:46:46 +00:00
|
|
|
import uuid
|
2020-01-11 11:20:00 +00:00
|
|
|
|
2020-07-09 04:39:33 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
from homeassistant import config_entries
|
2020-07-09 04:39:33 +00:00
|
|
|
from homeassistant.const import CONF_SHOW_ON_MAP
|
|
|
|
from homeassistant.core import callback
|
2021-07-21 21:36:57 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2020-07-09 04:39:33 +00:00
|
|
|
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
|
2020-01-11 11:20:00 +00:00
|
|
|
|
2020-07-09 04:39:33 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_AREA_NAME,
|
|
|
|
CONF_LAT_NE,
|
|
|
|
CONF_LAT_SW,
|
|
|
|
CONF_LON_NE,
|
|
|
|
CONF_LON_SW,
|
|
|
|
CONF_NEW_AREA,
|
|
|
|
CONF_PUBLIC_MODE,
|
2020-08-04 18:46:46 +00:00
|
|
|
CONF_UUID,
|
2020-07-09 04:39:33 +00:00
|
|
|
CONF_WEATHER_AREAS,
|
|
|
|
DOMAIN,
|
2021-10-26 14:09:10 +00:00
|
|
|
NETATMO_SCOPES,
|
2020-07-09 04:39:33 +00:00
|
|
|
)
|
2020-01-11 11:20:00 +00:00
|
|
|
|
2021-10-26 14:09:10 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
|
|
|
|
class NetatmoFlowHandler(
|
|
|
|
config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
|
|
|
|
):
|
|
|
|
"""Config flow to handle Netatmo OAuth2 authentication."""
|
|
|
|
|
|
|
|
DOMAIN = DOMAIN
|
|
|
|
|
2020-07-09 04:39:33 +00:00
|
|
|
@staticmethod
|
|
|
|
@callback
|
2021-07-21 21:36:57 +00:00
|
|
|
def async_get_options_flow(
|
|
|
|
config_entry: config_entries.ConfigEntry,
|
|
|
|
) -> config_entries.OptionsFlow:
|
2020-07-09 04:39:33 +00:00
|
|
|
"""Get the options flow for this handler."""
|
|
|
|
return NetatmoOptionsFlowHandler(config_entry)
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
@property
|
|
|
|
def logger(self) -> logging.Logger:
|
|
|
|
"""Return logger."""
|
|
|
|
return logging.getLogger(__name__)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_authorize_data(self) -> dict:
|
|
|
|
"""Extra data that needs to be appended to the authorize url."""
|
2021-10-26 14:09:10 +00:00
|
|
|
return {"scope": " ".join(NETATMO_SCOPES)}
|
2020-01-11 11:20:00 +00:00
|
|
|
|
2021-07-21 21:36:57 +00:00
|
|
|
async def async_step_user(self, user_input: dict | None = None) -> FlowResult:
|
2020-01-11 11:20:00 +00:00
|
|
|
"""Handle a flow start."""
|
2020-07-09 04:39:33 +00:00
|
|
|
await self.async_set_unique_id(DOMAIN)
|
2020-08-04 18:46:46 +00:00
|
|
|
|
2021-10-26 14:09:10 +00:00
|
|
|
if (
|
|
|
|
self.source != config_entries.SOURCE_REAUTH
|
|
|
|
and self._async_current_entries()
|
|
|
|
):
|
2020-08-04 18:46:46 +00:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
return await super().async_step_user(user_input)
|
|
|
|
|
2021-10-26 14:09:10 +00:00
|
|
|
async def async_step_reauth(self, user_input: dict | None = None) -> FlowResult:
|
|
|
|
"""Perform reauth upon an API authentication error."""
|
|
|
|
return await self.async_step_reauth_confirm()
|
|
|
|
|
|
|
|
async def async_step_reauth_confirm(
|
|
|
|
self, user_input: dict | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Dialog that informs the user that reauth is required."""
|
|
|
|
if user_input is None:
|
2022-03-04 15:42:02 +00:00
|
|
|
return self.async_show_form(step_id="reauth_confirm")
|
2021-10-26 14:09:10 +00:00
|
|
|
|
|
|
|
return await self.async_step_user()
|
|
|
|
|
|
|
|
async def async_oauth_create_entry(self, data: dict) -> FlowResult:
|
|
|
|
"""Create an oauth config entry or update existing entry for reauth."""
|
|
|
|
existing_entry = await self.async_set_unique_id(DOMAIN)
|
|
|
|
if existing_entry:
|
|
|
|
self.hass.config_entries.async_update_entry(existing_entry, data=data)
|
|
|
|
await self.hass.config_entries.async_reload(existing_entry.entry_id)
|
|
|
|
return self.async_abort(reason="reauth_successful")
|
|
|
|
|
|
|
|
return await super().async_oauth_create_entry(data)
|
|
|
|
|
2020-07-09 04:39:33 +00:00
|
|
|
|
|
|
|
class NetatmoOptionsFlowHandler(config_entries.OptionsFlow):
|
|
|
|
"""Handle Netatmo options."""
|
|
|
|
|
2021-05-20 12:06:44 +00:00
|
|
|
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
2020-07-09 04:39:33 +00:00
|
|
|
"""Initialize Netatmo options flow."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
self.options = dict(config_entry.options)
|
|
|
|
self.options.setdefault(CONF_WEATHER_AREAS, {})
|
|
|
|
|
2021-07-21 21:36:57 +00:00
|
|
|
async def async_step_init(self, user_input: dict | None = None) -> FlowResult:
|
2020-07-09 04:39:33 +00:00
|
|
|
"""Manage the Netatmo options."""
|
|
|
|
return await self.async_step_public_weather_areas()
|
|
|
|
|
2021-07-21 21:36:57 +00:00
|
|
|
async def async_step_public_weather_areas(
|
|
|
|
self, user_input: dict | None = None
|
|
|
|
) -> FlowResult:
|
2020-07-09 04:39:33 +00:00
|
|
|
"""Manage configuration of Netatmo public weather areas."""
|
2021-07-21 21:36:57 +00:00
|
|
|
errors: dict = {}
|
2020-07-09 04:39:33 +00:00
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
new_client = user_input.pop(CONF_NEW_AREA, None)
|
2021-07-21 21:36:57 +00:00
|
|
|
areas = user_input.pop(CONF_WEATHER_AREAS, [])
|
2020-07-09 04:39:33 +00:00
|
|
|
user_input[CONF_WEATHER_AREAS] = {
|
|
|
|
area: self.options[CONF_WEATHER_AREAS][area] for area in areas
|
|
|
|
}
|
|
|
|
self.options.update(user_input)
|
|
|
|
if new_client:
|
|
|
|
return await self.async_step_public_weather(
|
|
|
|
user_input={CONF_NEW_AREA: new_client}
|
|
|
|
)
|
|
|
|
|
2020-08-07 07:25:59 +00:00
|
|
|
return self._create_options_entry()
|
2020-07-09 04:39:33 +00:00
|
|
|
|
|
|
|
weather_areas = list(self.options[CONF_WEATHER_AREAS])
|
|
|
|
|
|
|
|
data_schema = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
2020-08-27 11:56:20 +00:00
|
|
|
CONF_WEATHER_AREAS,
|
|
|
|
default=weather_areas,
|
2021-07-21 21:36:57 +00:00
|
|
|
): cv.multi_select({wa: None for wa in weather_areas}),
|
2020-07-09 04:39:33 +00:00
|
|
|
vol.Optional(CONF_NEW_AREA): str,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return self.async_show_form(
|
2020-08-27 11:56:20 +00:00
|
|
|
step_id="public_weather_areas",
|
|
|
|
data_schema=data_schema,
|
|
|
|
errors=errors,
|
2020-07-09 04:39:33 +00:00
|
|
|
)
|
|
|
|
|
2021-07-21 21:36:57 +00:00
|
|
|
async def async_step_public_weather(self, user_input: dict) -> FlowResult:
|
2020-07-09 04:39:33 +00:00
|
|
|
"""Manage configuration of Netatmo public weather sensors."""
|
|
|
|
if user_input is not None and CONF_NEW_AREA not in user_input:
|
2020-08-04 18:46:46 +00:00
|
|
|
self.options[CONF_WEATHER_AREAS][
|
|
|
|
user_input[CONF_AREA_NAME]
|
|
|
|
] = fix_coordinates(user_input)
|
|
|
|
|
|
|
|
self.options[CONF_WEATHER_AREAS][user_input[CONF_AREA_NAME]][
|
|
|
|
CONF_UUID
|
|
|
|
] = str(uuid.uuid4())
|
|
|
|
|
2020-07-09 04:39:33 +00:00
|
|
|
return await self.async_step_public_weather_areas()
|
|
|
|
|
|
|
|
orig_options = self.config_entry.options.get(CONF_WEATHER_AREAS, {}).get(
|
|
|
|
user_input[CONF_NEW_AREA], {}
|
|
|
|
)
|
|
|
|
|
|
|
|
default_longitude = self.hass.config.longitude
|
|
|
|
default_latitude = self.hass.config.latitude
|
|
|
|
default_size = 0.04
|
|
|
|
|
|
|
|
data_schema = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_AREA_NAME, default=user_input[CONF_NEW_AREA]): str,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_LAT_NE,
|
|
|
|
default=orig_options.get(
|
|
|
|
CONF_LAT_NE, default_latitude + default_size
|
|
|
|
),
|
|
|
|
): cv.latitude,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_LON_NE,
|
|
|
|
default=orig_options.get(
|
|
|
|
CONF_LON_NE, default_longitude + default_size
|
|
|
|
),
|
|
|
|
): cv.longitude,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_LAT_SW,
|
|
|
|
default=orig_options.get(
|
|
|
|
CONF_LAT_SW, default_latitude - default_size
|
|
|
|
),
|
|
|
|
): cv.latitude,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_LON_SW,
|
|
|
|
default=orig_options.get(
|
|
|
|
CONF_LON_SW, default_longitude - default_size
|
|
|
|
),
|
|
|
|
): cv.longitude,
|
|
|
|
vol.Required(
|
2020-08-27 11:56:20 +00:00
|
|
|
CONF_PUBLIC_MODE,
|
|
|
|
default=orig_options.get(CONF_PUBLIC_MODE, "avg"),
|
2020-07-09 04:39:33 +00:00
|
|
|
): vol.In(["avg", "max"]),
|
|
|
|
vol.Required(
|
2020-08-27 11:56:20 +00:00
|
|
|
CONF_SHOW_ON_MAP,
|
|
|
|
default=orig_options.get(CONF_SHOW_ON_MAP, False),
|
2020-07-09 04:39:33 +00:00
|
|
|
): bool,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(step_id="public_weather", data_schema=data_schema)
|
|
|
|
|
2021-07-21 21:36:57 +00:00
|
|
|
def _create_options_entry(self) -> FlowResult:
|
2020-07-09 04:39:33 +00:00
|
|
|
"""Update config entry options."""
|
|
|
|
return self.async_create_entry(
|
|
|
|
title="Netatmo Public Weather", data=self.options
|
|
|
|
)
|
2020-08-04 18:46:46 +00:00
|
|
|
|
|
|
|
|
2021-07-21 21:36:57 +00:00
|
|
|
def fix_coordinates(user_input: dict) -> dict:
|
2020-08-04 18:46:46 +00:00
|
|
|
"""Fix coordinates if they don't comply with the Netatmo API."""
|
|
|
|
# Ensure coordinates have acceptable length for the Netatmo API
|
2021-07-19 13:57:06 +00:00
|
|
|
for coordinate in (CONF_LAT_NE, CONF_LAT_SW, CONF_LON_NE, CONF_LON_SW):
|
2020-08-04 18:46:46 +00:00
|
|
|
if len(str(user_input[coordinate]).split(".")[1]) < 7:
|
|
|
|
user_input[coordinate] = user_input[coordinate] + 0.0000001
|
|
|
|
|
|
|
|
# Swap coordinates if entered in wrong order
|
|
|
|
if user_input[CONF_LAT_NE] < user_input[CONF_LAT_SW]:
|
|
|
|
user_input[CONF_LAT_NE], user_input[CONF_LAT_SW] = (
|
|
|
|
user_input[CONF_LAT_SW],
|
|
|
|
user_input[CONF_LAT_NE],
|
|
|
|
)
|
|
|
|
if user_input[CONF_LON_NE] < user_input[CONF_LON_SW]:
|
|
|
|
user_input[CONF_LON_NE], user_input[CONF_LON_SW] = (
|
|
|
|
user_input[CONF_LON_SW],
|
|
|
|
user_input[CONF_LON_NE],
|
|
|
|
)
|
|
|
|
|
|
|
|
return user_input
|