1
mirror of https://github.com/home-assistant/core synced 2024-08-28 03:36:46 +02:00
ha-core/homeassistant/components/hko/config_flow.py
MisterCommand 0d7627da22
Add Hong Kong Observatory integration (#98703)
* Add Hong Kong Observatory integration

* Move coordinator to a separate file

* Map icons to conditions

* Fix code for review

* Skip name

* Add typings to data_coordinator

* Some small fixes

* Rename coordinator.py
2024-01-05 14:52:46 +01:00

71 lines
2.1 KiB
Python

"""Config flow for Hong Kong Observatory integration."""
from __future__ import annotations
from asyncio import timeout
from typing import Any
from hko import HKO, LOCATIONS, HKOError
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_LOCATION
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
from .const import API_RHRREAD, DEFAULT_LOCATION, DOMAIN, KEY_LOCATION
def get_loc_name(item):
"""Return an array of supported locations."""
return item[KEY_LOCATION]
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_LOCATION, default=DEFAULT_LOCATION): SelectSelector(
SelectSelectorConfig(options=list(map(get_loc_name, LOCATIONS)), sort=True)
)
}
)
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Hong Kong Observatory."""
VERSION = 1
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA
)
errors = {}
try:
websession = async_get_clientsession(self.hass)
hko = HKO(websession)
async with timeout(60):
await hko.weather(API_RHRREAD)
except HKOError:
errors["base"] = "cannot_connect"
except Exception: # pylint: disable=broad-except
errors["base"] = "unknown"
else:
await self.async_set_unique_id(
user_input[CONF_LOCATION], raise_on_progress=False
)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=user_input[CONF_LOCATION], data=user_input
)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)