From 52c358e1204f78f2482f3914f5a31556e9121278 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Sat, 28 Sep 2024 14:59:11 +0300 Subject: [PATCH] Add reconfigure flow for Jewish Calendar (#126773) * Add reconfigure flow for Jewish Calendar * Use async_update_reload_and_abort --- .../components/jewish_calendar/config_flow.py | 32 ++++++++++++++++- .../jewish_calendar/test_config_flow.py | 34 ++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/jewish_calendar/config_flow.py b/homeassistant/components/jewish_calendar/config_flow.py index 518db38b3bb..97608fca51e 100644 --- a/homeassistant/components/jewish_calendar/config_flow.py +++ b/homeassistant/components/jewish_calendar/config_flow.py @@ -3,7 +3,7 @@ from __future__ import annotations import logging -from typing import Any +from typing import TYPE_CHECKING, Any import zoneinfo import voluptuous as vol @@ -87,6 +87,7 @@ class JewishCalendarConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for Jewish calendar.""" VERSION = 1 + _config_entry: ConfigEntry @staticmethod @callback @@ -128,6 +129,35 @@ class JewishCalendarConfigFlow(ConfigFlow, domain=DOMAIN): """Import a config entry from configuration.yaml.""" return await self.async_step_user(import_data) + async def async_step_reconfigure( + self, _: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a reconfiguration flow initialized by the user.""" + config_entry = self.hass.config_entries.async_get_entry( + self.context["entry_id"] + ) + if TYPE_CHECKING: + assert config_entry is not None + self._config_entry = config_entry + return await self.async_step_reconfigure_confirm() + + async def async_step_reconfigure_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a reconfiguration flow initialized by the user.""" + if not user_input: + return self.async_show_form( + data_schema=self.add_suggested_values_to_schema( + _get_data_schema(self.hass), + {**self._config_entry.data}, + ), + step_id="reconfigure_confirm", + ) + + return self.async_update_reload_and_abort( + self._config_entry, data=user_input, reason="reconfigure_successful" + ) + class JewishCalendarOptionsFlowHandler(OptionsFlowWithConfigEntry): """Handle Jewish Calendar options.""" diff --git a/tests/components/jewish_calendar/test_config_flow.py b/tests/components/jewish_calendar/test_config_flow.py index 466d3a1e4f0..b9a041261aa 100644 --- a/tests/components/jewish_calendar/test_config_flow.py +++ b/tests/components/jewish_calendar/test_config_flow.py @@ -14,7 +14,7 @@ from homeassistant.components.jewish_calendar.const import ( DEFAULT_LANGUAGE, DOMAIN, ) -from homeassistant.config_entries import SOURCE_USER +from homeassistant.config_entries import SOURCE_RECONFIGURE, SOURCE_USER from homeassistant.const import ( CONF_ELEVATION, CONF_LANGUAGE, @@ -164,3 +164,35 @@ async def test_options_reconfigure( assert ( mock_config_entry.options[CONF_CANDLE_LIGHT_MINUTES] == DEFAULT_CANDLE_LIGHT + 1 ) + + +async def test_reconfigure( + hass: HomeAssistant, mock_config_entry: MockConfigEntry +) -> None: + """Test starting a reconfigure flow.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + # init user flow + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={ + "source": SOURCE_RECONFIGURE, + "entry_id": mock_config_entry.entry_id, + }, + data=mock_config_entry.data, + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure_confirm" + + # success + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_DIASPORA: not DEFAULT_DIASPORA, + }, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert mock_config_entry.data[CONF_DIASPORA] is not DEFAULT_DIASPORA