1 """Config flow for Environment Canada integration."""
5 import xml.etree.ElementTree
as ET
8 from env_canada
import ECWeather, ec_exc
9 import voluptuous
as vol
15 from .const
import CONF_STATION, CONF_TITLE, DOMAIN
17 _LOGGER = logging.getLogger(__name__)
21 """Validate the user input allows us to connect."""
22 lat = data.get(CONF_LATITUDE)
23 lon = data.get(CONF_LONGITUDE)
24 station = data.get(CONF_STATION)
25 lang = data.get(CONF_LANGUAGE).lower()
28 weather_data = ECWeather(station_id=station, language=lang)
30 weather_data = ECWeather(coordinates=(lat, lon), language=lang)
31 await weather_data.update()
33 if lat
is None or lon
is None:
34 lat = weather_data.lat
35 lon = weather_data.lon
38 CONF_TITLE: weather_data.metadata.get(
"location"),
39 CONF_STATION: weather_data.station_id,
46 """Handle a config flow for Environment Canada weather."""
51 self, user_input: dict[str, Any] |
None =
None
52 ) -> ConfigFlowResult:
53 """Handle the initial step."""
55 if user_input
is not None:
58 except (ET.ParseError, vol.MultipleInvalid, ec_exc.UnknownStationId):
59 errors[
"base"] =
"bad_station_id"
60 except aiohttp.ClientConnectionError:
61 errors[
"base"] =
"cannot_connect"
62 except aiohttp.ClientResponseError
as err:
64 errors[
"base"] =
"bad_station_id"
66 errors[
"base"] =
"error_response"
68 _LOGGER.exception(
"Unexpected exception")
69 errors[
"base"] =
"unknown"
72 user_input[CONF_STATION] = info[CONF_STATION]
73 user_input[CONF_LATITUDE] = info[CONF_LATITUDE]
74 user_input[CONF_LONGITUDE] = info[CONF_LONGITUDE]
78 f
"{user_input[CONF_STATION]}-{user_input[CONF_LANGUAGE].lower()}"
83 data_schema = vol.Schema(
85 vol.Optional(CONF_STATION): str,
87 CONF_LATITUDE, default=self.hass.config.latitude
90 CONF_LONGITUDE, default=self.hass.config.longitude
92 vol.Required(CONF_LANGUAGE, default=
"English"): vol.In(
99 step_id=
"user", data_schema=data_schema, errors=errors
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)