1 """Config flow for National Weather Service (NWS) integration."""
3 from __future__
import annotations
9 from pynws
import SimpleNWS
10 import voluptuous
as vol
19 from .
import base_unique_id
20 from .const
import CONF_STATION, DOMAIN
22 _LOGGER = logging.getLogger(__name__)
25 async
def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]:
26 """Validate the user input allows us to connect.
28 Data has the keys from DATA_SCHEMA with values provided by the user.
30 latitude = data[CONF_LATITUDE]
31 longitude = data[CONF_LONGITUDE]
32 api_key = data[CONF_API_KEY]
33 station = data.get(CONF_STATION)
36 ha_api_key = f
"{api_key} homeassistant"
37 nws = SimpleNWS(latitude, longitude, ha_api_key, client_session)
40 await nws.set_station(station)
41 except aiohttp.ClientError
as err:
42 _LOGGER.error(
"Could not connect: %s", err)
43 raise CannotConnect
from err
45 return {
"title": nws.station}
49 """Handle a config flow for National Weather Service (NWS)."""
54 self, user_input: dict[str, Any] |
None =
None
55 ) -> ConfigFlowResult:
56 """Handle the initial step."""
57 errors: dict[str, str] = {}
58 if user_input
is not None:
60 base_unique_id(user_input[CONF_LATITUDE], user_input[CONF_LONGITUDE])
65 user_input[CONF_STATION] = info[
"title"]
68 errors[
"base"] =
"cannot_connect"
70 _LOGGER.exception(
"Unexpected exception")
71 errors[
"base"] =
"unknown"
73 data_schema = vol.Schema(
75 vol.Required(CONF_API_KEY): str,
77 CONF_LATITUDE, default=self.hass.config.latitude
80 CONF_LONGITUDE, default=self.hass.config.longitude
82 vol.Optional(CONF_STATION): str,
87 step_id=
"user", data_schema=data_schema, errors=errors
92 """Error to indicate we cannot connect."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult 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_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)
dict[str, str] validate_input(HomeAssistant hass, dict[str, Any] data)
str base_unique_id(float latitude, float longitude)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)