1 """Config flow for Rain Bird."""
3 from __future__
import annotations
6 from collections.abc
import Mapping
10 from pyrainbird.async_client
import AsyncRainbirdClient, AsyncRainbirdController
11 from pyrainbird.data
import WifiParams
12 from pyrainbird.exceptions
import RainbirdApiException, RainbirdAuthException
13 import voluptuous
as vol
29 DEFAULT_TRIGGER_TIME_MINUTES,
33 from .coordinator
import async_create_clientsession
35 _LOGGER = logging.getLogger(__name__)
38 DATA_SCHEMA = vol.Schema(
40 vol.Required(CONF_HOST): selector.TextSelector(),
41 vol.Required(CONF_PASSWORD): selector.TextSelector(
42 selector.TextSelectorConfig(type=selector.TextSelectorType.PASSWORD)
46 REAUTH_SCHEMA = vol.Schema(
48 vol.Required(CONF_PASSWORD): selector.TextSelector(
49 selector.TextSelectorConfig(type=selector.TextSelectorType.PASSWORD)
56 """Error raised during a config flow."""
58 def __init__(self, message: str, error_code: str) ->
None:
59 """Initialize ConfigFlowError."""
65 """Handle a config flow for Rain Bird."""
72 config_entry: ConfigEntry,
73 ) -> RainBirdOptionsFlowHandler:
74 """Define the config flow to handle options."""
78 self, entry_data: Mapping[str, Any]
79 ) -> ConfigFlowResult:
80 """Perform reauthentication upon an API authentication error."""
81 self.
hosthost = entry_data[CONF_HOST]
85 self, user_input: dict[str, Any] |
None =
None
86 ) -> ConfigFlowResult:
87 """Confirm reauthentication dialog."""
88 errors: dict[str, str] = {}
92 except ConfigFlowError
as err:
93 _LOGGER.error(
"Error during config flow: %s", err)
94 errors[
"base"] = err.error_code
98 data_updates={CONF_PASSWORD: user_input[CONF_PASSWORD]},
101 step_id=
"reauth_confirm",
102 data_schema=REAUTH_SCHEMA,
107 self, user_input: dict[str, Any] |
None =
None
108 ) -> ConfigFlowResult:
109 """Configure the Rain Bird device."""
110 error_code: str |
None =
None
114 user_input[CONF_HOST], user_input[CONF_PASSWORD]
116 except ConfigFlowError
as err:
117 _LOGGER.error(
"Error during config flow: %s", err)
118 error_code = err.error_code
122 CONF_HOST: user_input[CONF_HOST],
123 CONF_PASSWORD: user_input[CONF_PASSWORD],
124 CONF_SERIAL_NUMBER: serial_number,
125 CONF_MAC: wifi_params.mac_address,
127 options={ATTR_DURATION: DEFAULT_TRIGGER_TIME_MINUTES},
132 data_schema=DATA_SCHEMA,
133 errors={
"base": error_code}
if error_code
else None,
137 self, host: str, password: str
138 ) -> tuple[str, WifiParams]:
139 """Test the connection and return the device identifiers.
141 Raises a ConfigFlowError on failure.
144 controller = AsyncRainbirdController(
152 async
with asyncio.timeout(TIMEOUT_SECONDS):
153 return await asyncio.gather(
154 controller.get_serial_number(),
155 controller.get_wifi_params(),
157 except TimeoutError
as err:
159 f
"Timeout connecting to Rain Bird controller: {err!s}",
162 except RainbirdAuthException
as err:
164 f
"Authentication error connecting from Rain Bird controller: {err!s}",
167 except RainbirdApiException
as err:
169 f
"Error connecting to Rain Bird controller: {err!s}",
173 await clientsession.close()
177 data: dict[str, Any],
178 options: dict[str, Any],
179 ) -> ConfigFlowResult:
180 """Create the config entry."""
189 CONF_HOST: data[CONF_HOST],
190 CONF_PASSWORD: data[CONF_PASSWORD],
195 CONF_HOST: data[CONF_HOST],
196 CONF_PASSWORD: data[CONF_PASSWORD],
200 title=data[CONF_HOST],
207 """Handle a RainBird options flow."""
210 self, user_input: dict[str, Any] |
None =
None
211 ) -> ConfigFlowResult:
212 """Manage the options."""
213 if user_input
is not None:
218 data_schema=vol.Schema(
None __init__(self, str message, str error_code)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_finish(self, dict[str, Any] data, dict[str, Any] options)
RainBirdOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
tuple[str, WifiParams] _test_connection(self, str host, str password)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry _get_reauth_entry(self)
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_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)
aiohttp.ClientSession async_create_clientsession()