1 """Config flow for Ring integration."""
3 from collections.abc
import Mapping
8 from ring_doorbell
import Auth, AuthenticationError, Requires2FAError
9 import voluptuous
as vol
30 from .
import get_auth_user_agent
31 from .const
import CONF_2FA, CONF_CONFIG_ENTRY_MINOR_VERSION, DOMAIN
33 _LOGGER = logging.getLogger(__name__)
35 STEP_USER_DATA_SCHEMA = vol.Schema(
36 {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
38 STEP_REAUTH_DATA_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
40 STEP_RECONFIGURE_DATA_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
42 UNKNOWN_RING_ACCOUNT =
"unknown_ring_account"
46 hass: HomeAssistant, hardware_id: str, data: dict[str, str]
48 """Validate the user input allows us to connect."""
54 hardware_id=hardware_id,
58 token = await auth.async_fetch_token(
63 except Requires2FAError
as err:
64 raise Require2FA
from err
65 except AuthenticationError
as err:
66 raise InvalidAuth
from err
72 """Handle a config flow for Ring."""
75 MINOR_VERSION = CONF_CONFIG_ENTRY_MINOR_VERSION
77 user_pass: dict[str, Any] = {}
78 hardware_id: str |
None =
None
81 self, discovery_info: dhcp.DhcpServiceInfo
82 ) -> ConfigFlowResult:
83 """Handle discovery via dhcp."""
90 if self.hass.config_entries.async_has_entries(DOMAIN):
91 device_registry = dr.async_get(self.hass)
92 if device_registry.async_get_device(
93 identifiers={(DOMAIN, discovery_info.macaddress)}
100 self, user_input: dict[str, Any] |
None =
None
101 ) -> ConfigFlowResult:
102 """Handle the initial step."""
103 errors: dict[str, str] = {}
104 if user_input
is not None:
114 return await self.async_step_2fa()
116 errors[
"base"] =
"invalid_auth"
118 _LOGGER.exception(
"Unexpected exception")
119 errors[
"base"] =
"unknown"
122 title=user_input[CONF_USERNAME],
125 CONF_USERNAME: user_input[CONF_USERNAME],
131 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
134 async
def async_step_2fa(
135 self, user_input: dict[str, Any] |
None =
None
136 ) -> ConfigFlowResult:
137 """Handle 2fa step."""
140 return await self.async_step_reauth_confirm(
141 {**self.
user_passuser_pass, **user_input}
145 return await self.async_step_reconfigure(
146 {**self.
user_passuser_pass, **user_input}
153 data_schema=vol.Schema({vol.Required(CONF_2FA): str}),
156 async
def async_step_reauth(
157 self, entry_data: Mapping[str, Any]
158 ) -> ConfigFlowResult:
159 """Handle reauth upon an API authentication error."""
160 return await self.async_step_reauth_confirm()
162 async
def async_step_reauth_confirm(
163 self, user_input: dict[str, Any] |
None =
None
164 ) -> ConfigFlowResult:
165 """Dialog that informs the user that reauth is required."""
166 errors: dict[str, str] = {}
170 user_input[CONF_USERNAME] = reauth_entry.data[CONF_USERNAME]
174 self.
hardware_idhardware_id = reauth_entry.data[CONF_DEVICE_ID]
180 return await self.async_step_2fa()
182 errors[
"base"] =
"invalid_auth"
184 _LOGGER.exception(
"Unexpected exception")
185 errors[
"base"] =
"unknown"
188 CONF_USERNAME: user_input[CONF_USERNAME],
195 step_id=
"reauth_confirm",
196 data_schema=STEP_REAUTH_DATA_SCHEMA,
198 description_placeholders={
199 CONF_USERNAME: reauth_entry.data[CONF_USERNAME],
200 CONF_NAME: reauth_entry.data[CONF_USERNAME],
204 async
def async_step_reconfigure(
205 self, user_input: dict[str, Any] |
None =
None
206 ) -> ConfigFlowResult:
207 """Trigger a reconfiguration flow."""
208 errors: dict[str, str] = {}
210 username = reconfigure_entry.data[CONF_USERNAME]
213 user_input[CONF_USERNAME] = username
223 return await self.async_step_2fa()
225 errors[
"base"] =
"invalid_auth"
227 _LOGGER.exception(
"Unexpected exception")
228 errors[
"base"] =
"unknown"
231 CONF_USERNAME: username,
238 step_id=
"reconfigure",
239 data_schema=STEP_RECONFIGURE_DATA_SCHEMA,
241 description_placeholders={
242 CONF_USERNAME: username,
248 """Error to indicate we require 2FA."""
252 """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
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)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
ConfigEntry _get_reconfigure_entry(self)
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, str hardware_id, dict[str, str] data)
str get_auth_user_agent()
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)