hass-core/homeassistant/components/weatherflow_cloud/config_flow.py
Jeef bc20e7900c
WeatherFlow Forecast (REST API) (#106615)
* rebase off dev

* Update homeassistant/components/weatherflow_cloud/const.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Addressing 1st round of PR Comments

* Update homeassistant/components/weatherflow_cloud/config_flow.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* addressing PR Comments

* fixing last comment that i can see

* Update homeassistant/components/weatherflow_cloud/coordinator.py

OOPS

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Update homeassistant/components/weatherflow_cloud/weather.py

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Update homeassistant/components/weatherflow_cloud/coordinator.py

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* switching to station id

* Update homeassistant/components/weatherflow_cloud/strings.json

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* addressing PR

* Updated tests to be better

* Updated tests accordingly

* REAuth flow and tests added

* Update homeassistant/components/weatherflow_cloud/strings.json

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Update homeassistant/components/weatherflow_cloud/coordinator.py

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Addressing PR comments

* Apply suggestions from code review

* ruff fix

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Co-authored-by: G Johansson <goran.johansson@shiftit.se>
2024-02-26 22:40:21 +01:00

79 lines
2.6 KiB
Python

"""Config flow for WeatherflowCloud integration."""
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
from aiohttp import ClientResponseError
import voluptuous as vol
from weatherflow4py.api import WeatherFlowRestAPI
from homeassistant import config_entries
from homeassistant.const import CONF_API_TOKEN
from homeassistant.data_entry_flow import FlowResult
from .const import DOMAIN
async def _validate_api_token(api_token: str) -> dict[str, Any]:
"""Validate the API token."""
try:
async with WeatherFlowRestAPI(api_token) as api:
await api.async_get_stations()
except ClientResponseError as err:
if err.status == 401:
return {"base": "invalid_api_key"}
return {"base": "cannot_connect"}
return {}
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for WeatherFlowCloud."""
VERSION = 1
async def async_step_reauth(self, user_input: Mapping[str, Any]) -> FlowResult:
"""Handle a flow for reauth."""
errors = {}
if user_input is not None:
api_token = user_input[CONF_API_TOKEN]
errors = await _validate_api_token(api_token)
if not errors:
# Update the existing entry and abort
if existing_entry := self.hass.config_entries.async_get_entry(
self.context["entry_id"]
):
return self.async_update_reload_and_abort(
existing_entry,
data={CONF_API_TOKEN: api_token},
reason="reauth_successful",
)
return self.async_show_form(
step_id="reauth",
data_schema=vol.Schema({vol.Required(CONF_API_TOKEN): str}),
errors=errors,
)
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initialized by the user."""
errors = {}
if user_input is not None:
self._async_abort_entries_match(user_input)
api_token = user_input[CONF_API_TOKEN]
errors = await _validate_api_token(api_token)
if not errors:
return self.async_create_entry(
title="Weatherflow REST",
data={CONF_API_TOKEN: api_token},
)
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({vol.Required(CONF_API_TOKEN): str}),
errors=errors,
)