1 """Config flow for Netatmo."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
10 import voluptuous
as vol
22 from .api
import get_api_scopes
35 _LOGGER = logging.getLogger(__name__)
39 config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
41 """Config flow to handle Netatmo OAuth2 authentication."""
48 config_entry: ConfigEntry,
50 """Get the options flow for this handler."""
56 return logging.getLogger(__name__)
60 """Extra data that needs to be appended to the authorize url."""
62 return {
"scope":
" ".join(scopes)}
64 async
def async_step_user(self, user_input: dict |
None =
None) -> ConfigFlowResult:
65 """Handle a flow start."""
66 await self.async_set_unique_id(DOMAIN)
68 if self.source != SOURCE_REAUTH
and self._async_current_entries():
69 return self.async_abort(reason=
"single_instance_allowed")
74 self, entry_data: Mapping[str, Any]
75 ) -> ConfigFlowResult:
76 """Perform reauth upon an API authentication error."""
80 self, user_input: dict |
None =
None
81 ) -> ConfigFlowResult:
82 """Dialog that informs the user that reauth is required."""
83 if user_input
is None:
84 return self.async_show_form(step_id=
"reauth_confirm")
89 """Create an oauth config entry or update existing entry for reauth."""
90 existing_entry = await self.async_set_unique_id(DOMAIN)
92 self.hass.config_entries.async_update_entry(existing_entry, data=data)
93 await self.hass.config_entries.async_reload(existing_entry.entry_id)
94 return self.async_abort(reason=
"reauth_successful")
100 """Handle Netatmo options."""
102 def __init__(self, config_entry: ConfigEntry) ->
None:
103 """Initialize Netatmo options flow."""
105 self.
optionsoptions.setdefault(CONF_WEATHER_AREAS, {})
108 """Manage the Netatmo options."""
112 self, user_input: dict |
None =
None
113 ) -> ConfigFlowResult:
114 """Manage configuration of Netatmo public weather areas."""
117 if user_input
is not None:
118 new_client = user_input.pop(CONF_NEW_AREA,
None)
119 areas = user_input.pop(CONF_WEATHER_AREAS, [])
120 user_input[CONF_WEATHER_AREAS] = {
121 area: self.
optionsoptions[CONF_WEATHER_AREAS][area]
for area
in areas
126 user_input={CONF_NEW_AREA: new_client}
131 weather_areas =
list(self.
optionsoptions[CONF_WEATHER_AREAS])
133 data_schema = vol.Schema(
137 default=weather_areas,
138 ): cv.multi_select({wa:
None for wa
in weather_areas}),
139 vol.Optional(CONF_NEW_AREA): str,
143 step_id=
"public_weather_areas",
144 data_schema=data_schema,
149 """Manage configuration of Netatmo public weather sensors."""
150 if user_input
is not None and CONF_NEW_AREA
not in user_input:
151 self.
optionsoptions[CONF_WEATHER_AREAS][user_input[CONF_AREA_NAME]] = (
155 self.
optionsoptions[CONF_WEATHER_AREAS][user_input[CONF_AREA_NAME]][CONF_UUID] = (
162 user_input[CONF_NEW_AREA], {}
165 default_longitude = self.hass.config.longitude
166 default_latitude = self.hass.config.latitude
169 data_schema = vol.Schema(
171 vol.Optional(CONF_AREA_NAME, default=user_input[CONF_NEW_AREA]): str,
174 default=orig_options.get(
175 CONF_LAT_NE, default_latitude + default_size
180 default=orig_options.get(
181 CONF_LON_NE, default_longitude + default_size
186 default=orig_options.get(
187 CONF_LAT_SW, default_latitude - default_size
192 default=orig_options.get(
193 CONF_LON_SW, default_longitude - default_size
198 default=orig_options.get(CONF_PUBLIC_MODE,
"avg"),
199 ): vol.In([
"avg",
"max",
"min"]),
202 default=orig_options.get(CONF_SHOW_ON_MAP,
False),
207 return self.
async_show_formasync_show_form(step_id=
"public_weather", data_schema=data_schema)
210 """Update config entry options."""
212 title=
"Netatmo Public Weather", data=self.
optionsoptions
217 """Fix coordinates if they don't comply with the Netatmo API."""
219 for coordinate
in (CONF_LAT_NE, CONF_LAT_SW, CONF_LON_NE, CONF_LON_SW):
220 if len(
str(user_input[coordinate]).split(
".")[1]) < 7:
221 user_input[coordinate] = user_input[coordinate] + 0.0000001
224 if user_input[CONF_LAT_NE] < user_input[CONF_LAT_SW]:
225 user_input[CONF_LAT_NE], user_input[CONF_LAT_SW] = (
226 user_input[CONF_LAT_SW],
227 user_input[CONF_LAT_NE],
229 if user_input[CONF_LON_NE] < user_input[CONF_LON_SW]:
230 user_input[CONF_LON_NE], user_input[CONF_LON_SW] = (
231 user_input[CONF_LON_SW],
232 user_input[CONF_LON_NE],
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
logging.Logger logger(self)
ConfigFlowResult async_oauth_create_entry(self, dict data)
ConfigFlowResult async_step_user(self, dict|None user_input=None)
dict extra_authorize_data(self)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_reauth_confirm(self, dict|None user_input=None)
ConfigFlowResult async_step_public_weather(self, dict user_input)
None __init__(self, ConfigEntry config_entry)
ConfigFlowResult async_step_init(self, dict|None user_input=None)
ConfigFlowResult _create_options_entry(self)
ConfigFlowResult async_step_public_weather_areas(self, dict|None user_input=None)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
web.Response get(self, web.Request request, str config_key)
IssData update(pyiss.ISS iss)
Iterable[str] get_api_scopes(str auth_implementation)
dict fix_coordinates(dict user_input)