Add config_flow helper to get reauth/reconfigure config entry (#127115)

* Add config_flow helper to get config entry from context

* Simplify

* Apply to aussie_broadband

* Another example

* Rename and adjust docstring

* Simplify

* Add test

* Refactor to hide context

* Raise

* Improve coverage

* Use AttributeError

* Use ValueError

* Raise UnknownEntry
This commit is contained in:
epenet 2024-10-02 12:00:07 +02:00 committed by GitHub
parent 201b8d9ebf
commit 21266e1c68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 178 additions and 8 deletions

View file

@ -2726,6 +2726,36 @@ class ConfigFlow(ConfigEntryBaseFlow):
"""Return True if other_flow is matching this flow."""
raise NotImplementedError
@property
def _reauth_entry_id(self) -> str:
"""Return reauth entry id."""
if self.source != SOURCE_REAUTH:
raise ValueError(f"Source is {self.source}, expected {SOURCE_REAUTH}")
return self.context["entry_id"] # type: ignore[no-any-return]
@callback
def _get_reauth_entry(self) -> ConfigEntry:
"""Return the reauth config entry linked to the current context."""
if entry := self.hass.config_entries.async_get_entry(self._reauth_entry_id):
return entry
raise UnknownEntry
@property
def _reconfigure_entry_id(self) -> str:
"""Return reconfigure entry id."""
if self.source != SOURCE_RECONFIGURE:
raise ValueError(f"Source is {self.source}, expected {SOURCE_RECONFIGURE}")
return self.context["entry_id"] # type: ignore[no-any-return]
@callback
def _get_reconfigure_entry(self) -> ConfigEntry:
"""Return the reconfigure config entry linked to the current context."""
if entry := self.hass.config_entries.async_get_entry(
self._reconfigure_entry_id
):
return entry
raise UnknownEntry
class OptionsFlowManager(data_entry_flow.FlowManager[ConfigFlowResult]):
"""Flow to set options for a configuration entry."""