Skip to content

Commit 87241ea

Browse files
authored
Add read support for MQTT config entry version to 2.1 (home-assistant#157623)
1 parent a871ec0 commit 87241ea

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

homeassistant/components/mqtt/__init__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,31 +378,33 @@ async def finish_dump(_: datetime) -> None:
378378

379379
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
380380
"""Migrate the options from config entry data."""
381-
_LOGGER.debug("Migrating from version %s:%s", entry.version, entry.minor_version)
381+
_LOGGER.debug("Migrating from version %s.%s", entry.version, entry.minor_version)
382382
data: dict[str, Any] = dict(entry.data)
383383
options: dict[str, Any] = dict(entry.options)
384-
if entry.version > 1:
384+
if entry.version > 2 or (entry.version == 2 and entry.minor_version > 1):
385385
# This means the user has downgraded from a future version
386+
# We allow read support for version 2.1
386387
return False
387388

388389
if entry.version == 1 and entry.minor_version < 2:
389-
# Can be removed when config entry is bumped to version 2.1
390-
# with HA Core 2026.1.0. Read support for version 2.1 is expected before 2026.1
391-
# From 2026.1 we will write version 2.1
390+
# Can be removed when the config entry is bumped to version 2.1
391+
# with HA Core 2026.7.0. Read support for version 2.1 is expected with 2026.1
392+
# From 2026.7 we will write version 2.1
392393
for key in ENTRY_OPTION_FIELDS:
393394
if key not in data:
394395
continue
395396
options[key] = data.pop(key)
397+
# Write version 1.2 for backwards compatibility
396398
hass.config_entries.async_update_entry(
397399
entry,
398400
data=data,
399401
options=options,
400-
version=CONFIG_ENTRY_VERSION,
401-
minor_version=CONFIG_ENTRY_MINOR_VERSION,
402+
version=1,
403+
minor_version=2,
402404
)
403405

404406
_LOGGER.debug(
405-
"Migration to version %s:%s successful", entry.version, entry.minor_version
407+
"Migration to version %s.%s successful", entry.version, entry.minor_version
406408
)
407409
return True
408410

homeassistant/components/mqtt/config_flow.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3952,9 +3952,8 @@ def update_password_from_user_input(
39523952
class FlowHandler(ConfigFlow, domain=DOMAIN):
39533953
"""Handle a config flow."""
39543954

3955-
# Can be bumped to version 2.1 with HA Core 2026.1.0
3956-
VERSION = CONFIG_ENTRY_VERSION # 1
3957-
MINOR_VERSION = CONFIG_ENTRY_MINOR_VERSION # 2
3955+
VERSION = CONFIG_ENTRY_VERSION # 2
3956+
MINOR_VERSION = CONFIG_ENTRY_MINOR_VERSION # 1
39583957

39593958
_hassio_discovery: dict[str, Any] | None = None
39603959
_addon_manager: AddonManager

homeassistant/components/mqtt/const.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,13 @@
381381
PAYLOAD_EMPTY_JSON = "{}"
382382
PAYLOAD_NONE = "None"
383383

384-
CONFIG_ENTRY_VERSION = 1
385-
CONFIG_ENTRY_MINOR_VERSION = 2
384+
CONFIG_ENTRY_VERSION = 2
385+
CONFIG_ENTRY_MINOR_VERSION = 1
386386

387387
# Split mqtt entry data and options
388388
# Can be removed when config entry is bumped to version 2.1
389-
# with HA Core 2026.1.0. Read support for version 2.1 is expected before 2026.1
390-
# From 2026.1 we will write version 2.1
389+
# with HA Core 2026.7.0. Read support for version 2.1 is expected from 2026.1
390+
# From 2026.7 we will write version 2.1
391391
ENTRY_OPTION_FIELDS = (
392392
CONF_DISCOVERY,
393393
CONF_DISCOVERY_PREFIX,

tests/components/mqtt/test_config_flow.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ async def test_user_connection_works(
386386
"port": 1883,
387387
}
388388
# Check we have the latest Config Entry version
389-
assert result["result"].version == 1
390-
assert result["result"].minor_version == 2
389+
assert result["result"].version == 2
390+
assert result["result"].minor_version == 1
391391
# Check we tried the connection
392392
assert len(mock_try_connection.mock_calls) == 1
393393
# Check config entry got setup
@@ -2590,7 +2590,7 @@ async def test_reconfigure_no_changed_password(
25902590
[
25912591
(1, 1, MOCK_ENTRY_DATA | MOCK_ENTRY_OPTIONS, {}, 1, 2),
25922592
(1, 2, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS, 1, 2),
2593-
(1, 3, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS, 1, 3),
2593+
(2, 1, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS, 2, 1),
25942594
],
25952595
)
25962596
@pytest.mark.usefixtures("mock_reload_after_entry_update")
@@ -2631,11 +2631,10 @@ async def test_migrate_config_entry(
26312631
"minor_version",
26322632
"data",
26332633
"options",
2634-
"expected_version",
2635-
"expected_minor_version",
26362634
),
26372635
[
2638-
(2, 1, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS, 2, 1),
2636+
(2, 2, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS),
2637+
(3, 1, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS),
26392638
],
26402639
)
26412640
@pytest.mark.usefixtures("mock_reload_after_entry_update")
@@ -2646,8 +2645,6 @@ async def test_migrate_of_incompatible_config_entry(
26462645
minor_version: int,
26472646
data: dict[str, Any],
26482647
options: dict[str, Any],
2649-
expected_version: int,
2650-
expected_minor_version: int,
26512648
) -> None:
26522649
"""Test migrating a config entry."""
26532650
config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
@@ -2660,8 +2657,6 @@ async def test_migrate_of_incompatible_config_entry(
26602657
minor_version=minor_version,
26612658
)
26622659
await hass.async_block_till_done()
2663-
assert config_entry.version == expected_version
2664-
assert config_entry.minor_version == expected_minor_version
26652660

26662661
# Try to start MQTT with incompatible config entry
26672662
with pytest.raises(AssertionError):

0 commit comments

Comments
 (0)