1 """Config flow for flume integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
10 from pyflume
import FlumeAuth, FlumeDeviceList
11 from requests.exceptions
import RequestException
12 import voluptuous
as vol
24 from .const
import BASE_TOKEN_FILENAME, DOMAIN
26 _LOGGER = logging.getLogger(__name__)
33 DATA_SCHEMA = vol.Schema(
35 vol.Required(CONF_USERNAME): str,
36 vol.Required(CONF_PASSWORD): str,
37 vol.Required(CONF_CLIENT_ID): str,
38 vol.Required(CONF_CLIENT_SECRET): str,
44 hass: HomeAssistant, data: dict[str, Any], clear_token_file: bool
46 """Validate in the executor."""
47 flume_token_full_path = hass.config.path(
48 f
"{BASE_TOKEN_FILENAME}-{data[CONF_USERNAME]}"
50 if clear_token_file
and os.path.exists(flume_token_full_path):
51 os.unlink(flume_token_full_path)
53 return FlumeDeviceList(
58 data[CONF_CLIENT_SECRET],
59 flume_token_file=flume_token_full_path,
65 hass: HomeAssistant, data: dict[str, Any], clear_token_file: bool =
False
67 """Validate the user input allows us to connect.
69 Data has the keys from DATA_SCHEMA with values provided by the user.
72 flume_devices = await hass.async_add_executor_job(
73 _validate_input, hass, data, clear_token_file
75 except RequestException
as err:
76 raise CannotConnect
from err
77 except Exception
as err:
78 _LOGGER.exception(
"Auth exception")
79 raise InvalidAuth
from err
80 if not flume_devices
or not flume_devices.device_list:
84 return {
"title": data[CONF_USERNAME]}
88 """Handle a config flow for flume."""
93 """Init flume config flow."""
97 self, user_input: dict[str, Any] |
None =
None
98 ) -> ConfigFlowResult:
99 """Handle the initial step."""
100 errors: dict[str, str] = {}
101 if user_input
is not None:
108 except CannotConnect:
109 errors[
"base"] =
"cannot_connect"
111 errors[CONF_PASSWORD] =
"invalid_auth"
114 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
118 self, entry_data: Mapping[str, Any]
119 ) -> ConfigFlowResult:
125 self, user_input: dict[str, Any] |
None =
None
126 ) -> ConfigFlowResult:
127 """Handle reauth input."""
128 errors: dict[str, str] = {}
130 assert existing_entry
131 if user_input
is not None:
132 new_data = {**existing_entry.data, CONF_PASSWORD: user_input[CONF_PASSWORD]}
135 except CannotConnect:
136 errors[
"base"] =
"cannot_connect"
138 errors[CONF_PASSWORD] =
"invalid_auth"
140 self.hass.config_entries.async_update_entry(
141 existing_entry, data=new_data
143 await self.hass.config_entries.async_reload(existing_entry.entry_id)
147 description_placeholders={
148 CONF_USERNAME: existing_entry.data[CONF_USERNAME]
150 step_id=
"reauth_confirm",
151 data_schema=vol.Schema(
153 vol.Required(CONF_PASSWORD): str,
161 """Error to indicate we cannot connect."""
164 class InvalidAuth(HomeAssistantError):
165 """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
dict[str, Any] validate_input(HomeAssistant hass, dict[str, Any] data, bool clear_token_file=False)
FlumeDeviceList _validate_input(HomeAssistant hass, dict[str, Any] data, bool clear_token_file)