1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-30 04:05:01 +01:00
Files
2026-04-30 21:14:48 +02:00

84 lines
2.7 KiB
Python

"""Config flow for WeatherflowCloud integration."""
from collections.abc import Mapping
from typing import Any
from aiohttp import ClientResponseError
import voluptuous as vol
from weatherflow4py.api import WeatherFlowRestAPI
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_API_TOKEN
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 WeatherFlowCloudConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for WeatherFlowCloud."""
VERSION = 1
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle a flow for reauth."""
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a flow initiated by reauthentication."""
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
existing_entry = self._get_reauth_entry()
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_confirm",
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
) -> ConfigFlowResult:
"""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,
)