Fix updating options in Jewish Calendar (#118643)

This commit is contained in:
Tsvi Mostovicz 2024-06-04 21:55:38 +03:00 committed by GitHub
parent f18ddb628c
commit 98455cbd93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 13 deletions

View File

@ -119,10 +119,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
"""Set up a configuration entry for Jewish calendar."""
language = config_entry.data.get(CONF_LANGUAGE, DEFAULT_LANGUAGE)
diaspora = config_entry.data.get(CONF_DIASPORA, DEFAULT_DIASPORA)
candle_lighting_offset = config_entry.data.get(
candle_lighting_offset = config_entry.options.get(
CONF_CANDLE_LIGHT_MINUTES, DEFAULT_CANDLE_LIGHT
)
havdalah_offset = config_entry.data.get(
havdalah_offset = config_entry.options.get(
CONF_HAVDALAH_OFFSET_MINUTES, DEFAULT_HAVDALAH_OFFSET_MINUTES
)
@ -154,6 +154,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
async_update_unique_ids(ent_reg, config_entry.entry_id, old_prefix)
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
# Trigger update of states for all platforms
await hass.config_entries.async_reload(config_entry.entry_id)
config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
return True

View File

@ -100,10 +100,23 @@ class JewishCalendarConfigFlow(ConfigFlow, domain=DOMAIN):
) -> ConfigFlowResult:
"""Handle the initial step."""
if user_input is not None:
_options = {}
if CONF_CANDLE_LIGHT_MINUTES in user_input:
_options[CONF_CANDLE_LIGHT_MINUTES] = user_input[
CONF_CANDLE_LIGHT_MINUTES
]
del user_input[CONF_CANDLE_LIGHT_MINUTES]
if CONF_HAVDALAH_OFFSET_MINUTES in user_input:
_options[CONF_HAVDALAH_OFFSET_MINUTES] = user_input[
CONF_HAVDALAH_OFFSET_MINUTES
]
del user_input[CONF_HAVDALAH_OFFSET_MINUTES]
if CONF_LOCATION in user_input:
user_input[CONF_LATITUDE] = user_input[CONF_LOCATION][CONF_LATITUDE]
user_input[CONF_LONGITUDE] = user_input[CONF_LOCATION][CONF_LONGITUDE]
return self.async_create_entry(title=DEFAULT_NAME, data=user_input)
return self.async_create_entry(
title=DEFAULT_NAME, data=user_input, options=_options
)
return self.async_show_form(
step_id="user",

View File

@ -9,9 +9,7 @@ from homeassistant.components.jewish_calendar.const import (
CONF_CANDLE_LIGHT_MINUTES,
CONF_DIASPORA,
CONF_HAVDALAH_OFFSET_MINUTES,
DEFAULT_CANDLE_LIGHT,
DEFAULT_DIASPORA,
DEFAULT_HAVDALAH_OFFSET_MINUTES,
DEFAULT_LANGUAGE,
DOMAIN,
)
@ -73,10 +71,8 @@ async def test_import_no_options(hass: HomeAssistant, language, diaspora) -> Non
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].data == conf[DOMAIN] | {
CONF_CANDLE_LIGHT_MINUTES: DEFAULT_CANDLE_LIGHT,
CONF_HAVDALAH_OFFSET_MINUTES: DEFAULT_HAVDALAH_OFFSET_MINUTES,
}
for entry_key, entry_val in entries[0].data.items():
assert entry_val == conf[DOMAIN][entry_key]
async def test_import_with_options(hass: HomeAssistant) -> None:
@ -99,7 +95,10 @@ async def test_import_with_options(hass: HomeAssistant) -> None:
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].data == conf[DOMAIN]
for entry_key, entry_val in entries[0].data.items():
assert entry_val == conf[DOMAIN][entry_key]
for entry_key, entry_val in entries[0].options.items():
assert entry_val == conf[DOMAIN][entry_key]
async def test_single_instance_allowed(
@ -135,5 +134,7 @@ async def test_options(hass: HomeAssistant, mock_config_entry: MockConfigEntry)
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"][CONF_CANDLE_LIGHT_MINUTES] == 25
assert result["data"][CONF_HAVDALAH_OFFSET_MINUTES] == 34
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].options[CONF_CANDLE_LIGHT_MINUTES] == 25
assert entries[0].options[CONF_HAVDALAH_OFFSET_MINUTES] == 34

View File

@ -58,7 +58,10 @@ async def test_import_unique_id_migration(hass: HomeAssistant) -> None:
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].data == yaml_conf[DOMAIN]
for entry_key, entry_val in entries[0].data.items():
assert entry_val == yaml_conf[DOMAIN][entry_key]
for entry_key, entry_val in entries[0].options.items():
assert entry_val == yaml_conf[DOMAIN][entry_key]
# Assert that the unique_id was updated
new_unique_id = ent_reg.async_get(sample_entity.entity_id).unique_id

View File

@ -519,6 +519,8 @@ async def test_shabbat_times_sensor(
data={
CONF_LANGUAGE: language,
CONF_DIASPORA: diaspora,
},
options={
CONF_CANDLE_LIGHT_MINUTES: candle_lighting,
CONF_HAVDALAH_OFFSET_MINUTES: havdalah,
},