This commit is contained in:
G Johansson
2025-08-03 13:06:10 +00:00
parent 0172020ab5
commit d783a8f975
2 changed files with 43 additions and 10 deletions

View File

@@ -3384,6 +3384,34 @@ class ConfigSubentryFlow(
return result return result
@callback
def _async_update(
self,
entry: ConfigEntry,
subentry: ConfigSubentry,
*,
unique_id: str | None | UndefinedType = UNDEFINED,
title: str | UndefinedType = UNDEFINED,
data: Mapping[str, Any] | UndefinedType = UNDEFINED,
data_updates: Mapping[str, Any] | UndefinedType = UNDEFINED,
) -> bool:
"""Update config subentry and return result.
Internal to be used by update_and_abort and update_reload_and_abort methods only.
"""
if data_updates is not UNDEFINED:
if data is not UNDEFINED:
raise ValueError("Cannot set both data and data_updates")
data = subentry.data | data_updates
return self.hass.config_entries.async_update_subentry(
entry=entry,
subentry=subentry,
unique_id=unique_id,
title=title,
data=data,
)
@callback @callback
def async_update_and_abort( def async_update_and_abort(
self, self,
@@ -3403,16 +3431,13 @@ class ConfigSubentryFlow(
:param title: replace the title of the subentry :param title: replace the title of the subentry
:param unique_id: replace the unique_id of the subentry :param unique_id: replace the unique_id of the subentry
""" """
if data_updates is not UNDEFINED: self._async_update(
if data is not UNDEFINED:
raise ValueError("Cannot set both data and data_updates")
data = subentry.data | data_updates
self.hass.config_entries.async_update_subentry(
entry=entry, entry=entry,
subentry=subentry, subentry=subentry,
unique_id=unique_id, unique_id=unique_id,
title=title, title=title,
data=data, data=data,
data_updates=data_updates,
) )
return self.async_abort(reason="reconfigure_successful") return self.async_abort(reason="reconfigure_successful")
@@ -3438,7 +3463,7 @@ class ConfigSubentryFlow(
:param reload_even_if_entry_is_unchanged: set this to `False` if the entry :param reload_even_if_entry_is_unchanged: set this to `False` if the entry
should not be reloaded if it is unchanged should not be reloaded if it is unchanged
""" """
result = self.async_update_and_abort( result = self._async_update(
entry=entry, entry=entry,
subentry=subentry, subentry=subentry,
unique_id=unique_id, unique_id=unique_id,
@@ -3446,9 +3471,9 @@ class ConfigSubentryFlow(
data=data, data=data,
data_updates=data_updates, data_updates=data_updates,
) )
if reload_even_if_entry_is_unchanged or result: if reload_even_if_entry_is_unchanged or result is True:
self.hass.config_entries.async_schedule_reload(entry.entry_id) self.hass.config_entries.async_schedule_reload(entry.entry_id)
return result return self.async_abort(reason="reconfigure_successful")
@property @property
def _entry_id(self) -> str: def _entry_id(self) -> str:

View File

@@ -6527,6 +6527,7 @@ async def test_update_subentry_and_abort(
"expected_data", "expected_data",
"raises", "raises",
"reload", # True is default "reload", # True is default
"setup_call_count",
), ),
[ [
( (
@@ -6540,6 +6541,7 @@ async def test_update_subentry_and_abort(
{"vendor": "data2"}, {"vendor": "data2"},
None, None,
True, True,
2,
), ),
( (
{ {
@@ -6552,6 +6554,7 @@ async def test_update_subentry_and_abort(
{"vendor": "data"}, {"vendor": "data"},
None, None,
True, True,
2,
), ),
( (
{ {
@@ -6564,6 +6567,7 @@ async def test_update_subentry_and_abort(
{"vendor": "data"}, {"vendor": "data"},
None, None,
False, False,
1,
), ),
( (
{}, {},
@@ -6572,6 +6576,7 @@ async def test_update_subentry_and_abort(
{"vendor": "data"}, {"vendor": "data"},
None, None,
True, True,
2,
), ),
( (
{ {
@@ -6582,6 +6587,7 @@ async def test_update_subentry_and_abort(
{"buyer": "me"}, {"buyer": "me"},
None, None,
True, True,
2,
), ),
( (
{"data_updates": {"buyer": "me"}}, {"data_updates": {"buyer": "me"}},
@@ -6590,6 +6596,7 @@ async def test_update_subentry_and_abort(
{"vendor": "data", "buyer": "me"}, {"vendor": "data", "buyer": "me"},
None, None,
True, True,
2,
), ),
( (
{ {
@@ -6603,6 +6610,7 @@ async def test_update_subentry_and_abort(
{"vendor": "data"}, {"vendor": "data"},
ValueError, ValueError,
True, True,
1,
), ),
], ],
ids=[ ids=[
@@ -6623,6 +6631,7 @@ async def test_update_subentry_reload_and_abort(
kwargs: dict[str, Any], kwargs: dict[str, Any],
raises: type[Exception] | None, raises: type[Exception] | None,
reload: bool, reload: bool,
setup_call_count: int,
) -> None: ) -> None:
"""Test updating an entry and reloading.""" """Test updating an entry and reloading."""
subentry_id = "blabla" subentry_id = "blabla"
@@ -6686,11 +6695,10 @@ async def test_update_subentry_reload_and_abort(
assert subentry.title == expected_title assert subentry.title == expected_title
assert subentry.unique_id == expected_unique_id assert subentry.unique_id == expected_unique_id
assert subentry.data == expected_data assert subentry.data == expected_data
assert setup_entry.call_count == setup_call_count
if raises: if raises:
assert setup_entry.call_count == 1
assert isinstance(err, raises) assert isinstance(err, raises)
else: else:
assert setup_entry.call_count == 2
assert result["type"] == FlowResultType.ABORT assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful" assert result["reason"] == "reconfigure_successful"