Add reconfigure flow for Jewish Calendar (#126773)
* Add reconfigure flow for Jewish Calendar * Use async_update_reload_and_abort
This commit is contained in:
parent
f516e538a8
commit
52c358e120
2 changed files with 64 additions and 2 deletions
|
@ -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."""
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue