From 90eaf726cdc4878d48520ddb73872679198b6e69 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:07:29 +0100 Subject: [PATCH] Document required entry_id (#2436) * Document required entry_id * Apply suggestions from code review Co-authored-by: Martin Hjelmare * Update and rename 2024-11-01-reauth-reconfigure-entry-id.md to 2024-11-03-reauth-reconfigure-entry-id.md * Update 2024-11-03-reauth-reconfigure-entry-id.md * Update 2024-11-03-reauth-reconfigure-entry-id.md * Update 2024-11-03-reauth-reconfigure-entry-id.md * Update blog/2024-11-03-reauth-reconfigure-entry-id.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update 2024-11-03-reauth-reconfigure-entry-id.md * Rename 2024-11-03-reauth-reconfigure-entry-id.md to 2024-11-04-reauth-reconfigure-entry-id.md * Apply suggestions from code review Co-authored-by: Martin Hjelmare --------- Co-authored-by: Martin Hjelmare Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .../2024-11-04-reauth-reconfigure-entry-id.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 blog/2024-11-04-reauth-reconfigure-entry-id.md diff --git a/blog/2024-11-04-reauth-reconfigure-entry-id.md b/blog/2024-11-04-reauth-reconfigure-entry-id.md new file mode 100644 index 00000000..d9515061 --- /dev/null +++ b/blog/2024-11-04-reauth-reconfigure-entry-id.md @@ -0,0 +1,47 @@ +--- +author: epenet +authorURL: https://github.com/epenet +title: "Reauth and reconfigure flows need to be linked to a config entry" +--- + +Starting a reauth or a reconfigure flow without a link to the config entry has been deprecated, and will start failing in 2025.12. + +Custom integrations should be updated to trigger the reauth flow using the `entry.async_start_reauth(hass)` helper. +```python + async def async_press(self) -> None: + """Handle the button press.""" + try: + await self.device.press_button() + except DevicePasswordProtected as ex: + self.entry.async_start_reauth(self.hass) +``` + +Old incorrect code: +```python + async def async_press(self) -> None: + """Handle the button press.""" + try: + await self.device.press_button() + except DevicePasswordProtected as ex: + # old incorrect code: + self.hass.async_create_task( + hass.config_entries.flow.async_init(DOMAIN, context={"source": SOURCE_REAUTH} + ) + ) +``` + +Custom integrations can also raise a `ConfigEntryAuthFailed` exception during the initialization phase, or within the update method of a data update coordinator. + +```python +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + """Set up integration from a config entry.""" + username = entry.data[CONF_USERNAME] + password = entry.data[CONF_PASSWORD] + + if not _credentials_valid(username, password): + raise ConfigEntryAuthFailed() +``` + +Starting a reconfigure flow is only done by the frontend and custom integrations should not need to change anything for these flows. + +More details can be found in the [reconfigure](/docs/config_entries_config_flow_handler#reconfigure) and [reauthentication](/docs/config_entries_config_flow_handler#reauthentication) documentation.