|
| 1 | +"""Config flow for Nederlandse Spoorwegen integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from ns_api import NSAPI, Station |
| 9 | +from requests.exceptions import ( |
| 10 | + ConnectionError as RequestsConnectionError, |
| 11 | + HTTPError, |
| 12 | + Timeout, |
| 13 | +) |
| 14 | +import voluptuous as vol |
| 15 | + |
| 16 | +from homeassistant.config_entries import ( |
| 17 | + ConfigEntry, |
| 18 | + ConfigFlow, |
| 19 | + ConfigFlowResult, |
| 20 | + ConfigSubentryData, |
| 21 | + ConfigSubentryFlow, |
| 22 | + SubentryFlowResult, |
| 23 | +) |
| 24 | +from homeassistant.const import CONF_API_KEY |
| 25 | +from homeassistant.core import callback |
| 26 | +from homeassistant.helpers.selector import ( |
| 27 | + SelectOptionDict, |
| 28 | + SelectSelector, |
| 29 | + SelectSelectorConfig, |
| 30 | + TimeSelector, |
| 31 | +) |
| 32 | + |
| 33 | +from .const import ( |
| 34 | + CONF_FROM, |
| 35 | + CONF_NAME, |
| 36 | + CONF_ROUTES, |
| 37 | + CONF_TIME, |
| 38 | + CONF_TO, |
| 39 | + CONF_VIA, |
| 40 | + DOMAIN, |
| 41 | +) |
| 42 | + |
| 43 | +_LOGGER = logging.getLogger(__name__) |
| 44 | + |
| 45 | + |
| 46 | +class NSConfigFlow(ConfigFlow, domain=DOMAIN): |
| 47 | + """Handle a config flow for Nederlandse Spoorwegen.""" |
| 48 | + |
| 49 | + VERSION = 1 |
| 50 | + MINOR_VERSION = 1 |
| 51 | + |
| 52 | + async def async_step_user( |
| 53 | + self, user_input: dict[str, Any] | None = None |
| 54 | + ) -> ConfigFlowResult: |
| 55 | + """Handle the initial step of the config flow (API key).""" |
| 56 | + errors: dict[str, str] = {} |
| 57 | + if user_input is not None: |
| 58 | + self._async_abort_entries_match(user_input) |
| 59 | + client = NSAPI(user_input[CONF_API_KEY]) |
| 60 | + try: |
| 61 | + await self.hass.async_add_executor_job(client.get_stations) |
| 62 | + except HTTPError: |
| 63 | + errors["base"] = "invalid_auth" |
| 64 | + except (RequestsConnectionError, Timeout): |
| 65 | + errors["base"] = "cannot_connect" |
| 66 | + except Exception: |
| 67 | + _LOGGER.exception("Unexpected exception validating API key") |
| 68 | + errors["base"] = "unknown" |
| 69 | + if not errors: |
| 70 | + return self.async_create_entry( |
| 71 | + title="Nederlandse Spoorwegen", |
| 72 | + data={CONF_API_KEY: user_input[CONF_API_KEY]}, |
| 73 | + ) |
| 74 | + return self.async_show_form( |
| 75 | + step_id="user", |
| 76 | + data_schema=vol.Schema({vol.Required(CONF_API_KEY): str}), |
| 77 | + errors=errors, |
| 78 | + ) |
| 79 | + |
| 80 | + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: |
| 81 | + """Handle import from YAML configuration.""" |
| 82 | + self._async_abort_entries_match({CONF_API_KEY: import_data[CONF_API_KEY]}) |
| 83 | + |
| 84 | + client = NSAPI(import_data[CONF_API_KEY]) |
| 85 | + try: |
| 86 | + stations = await self.hass.async_add_executor_job(client.get_stations) |
| 87 | + except HTTPError: |
| 88 | + return self.async_abort(reason="invalid_auth") |
| 89 | + except (RequestsConnectionError, Timeout): |
| 90 | + return self.async_abort(reason="cannot_connect") |
| 91 | + except Exception: |
| 92 | + _LOGGER.exception("Unexpected exception validating API key") |
| 93 | + return self.async_abort(reason="unknown") |
| 94 | + |
| 95 | + station_codes = {station.code for station in stations} |
| 96 | + |
| 97 | + subentries: list[ConfigSubentryData] = [] |
| 98 | + for route in import_data.get(CONF_ROUTES, []): |
| 99 | + # Convert station codes to uppercase for consistency with UI routes |
| 100 | + for key in (CONF_FROM, CONF_TO, CONF_VIA): |
| 101 | + if key in route: |
| 102 | + route[key] = route[key].upper() |
| 103 | + if route[key] not in station_codes: |
| 104 | + return self.async_abort(reason="invalid_station") |
| 105 | + |
| 106 | + subentries.append( |
| 107 | + ConfigSubentryData( |
| 108 | + title=route[CONF_NAME], |
| 109 | + subentry_type="route", |
| 110 | + data=route, |
| 111 | + unique_id=None, |
| 112 | + ) |
| 113 | + ) |
| 114 | + |
| 115 | + return self.async_create_entry( |
| 116 | + title="Nederlandse Spoorwegen", |
| 117 | + data={CONF_API_KEY: import_data[CONF_API_KEY]}, |
| 118 | + subentries=subentries, |
| 119 | + ) |
| 120 | + |
| 121 | + @classmethod |
| 122 | + @callback |
| 123 | + def async_get_supported_subentry_types( |
| 124 | + cls, config_entry: ConfigEntry |
| 125 | + ) -> dict[str, type[ConfigSubentryFlow]]: |
| 126 | + """Return subentries supported by this integration.""" |
| 127 | + return {"route": RouteSubentryFlowHandler} |
| 128 | + |
| 129 | + |
| 130 | +class RouteSubentryFlowHandler(ConfigSubentryFlow): |
| 131 | + """Handle subentry flow for adding and modifying routes.""" |
| 132 | + |
| 133 | + def __init__(self) -> None: |
| 134 | + """Initialize route subentry flow.""" |
| 135 | + self.stations: dict[str, Station] = {} |
| 136 | + |
| 137 | + async def async_step_user( |
| 138 | + self, user_input: dict[str, Any] | None = None |
| 139 | + ) -> SubentryFlowResult: |
| 140 | + """Add a new route subentry.""" |
| 141 | + if user_input is not None: |
| 142 | + return self.async_create_entry(title=user_input[CONF_NAME], data=user_input) |
| 143 | + client = NSAPI(self._get_entry().data[CONF_API_KEY]) |
| 144 | + if not self.stations: |
| 145 | + try: |
| 146 | + self.stations = { |
| 147 | + station.code: station |
| 148 | + for station in await self.hass.async_add_executor_job( |
| 149 | + client.get_stations |
| 150 | + ) |
| 151 | + } |
| 152 | + except (RequestsConnectionError, Timeout, HTTPError, ValueError): |
| 153 | + return self.async_abort(reason="cannot_connect") |
| 154 | + |
| 155 | + options = [ |
| 156 | + SelectOptionDict(label=station.names["long"], value=code) |
| 157 | + for code, station in self.stations.items() |
| 158 | + ] |
| 159 | + return self.async_show_form( |
| 160 | + step_id="user", |
| 161 | + data_schema=vol.Schema( |
| 162 | + { |
| 163 | + vol.Required(CONF_NAME): str, |
| 164 | + vol.Required(CONF_FROM): SelectSelector( |
| 165 | + SelectSelectorConfig(options=options, sort=True), |
| 166 | + ), |
| 167 | + vol.Required(CONF_TO): SelectSelector( |
| 168 | + SelectSelectorConfig(options=options, sort=True), |
| 169 | + ), |
| 170 | + vol.Optional(CONF_VIA): SelectSelector( |
| 171 | + SelectSelectorConfig(options=options, sort=True), |
| 172 | + ), |
| 173 | + vol.Optional(CONF_TIME): TimeSelector(), |
| 174 | + } |
| 175 | + ), |
| 176 | + ) |
0 commit comments