Bump MQTT config flow to version 2.1 (#173094)

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Jan Bouwhuis
2026-06-10 17:31:23 +02:00
committed by GitHub
co-authored by Copilot Autofix powered by AI
parent d8182508bb
commit f5b8e8ba81
3 changed files with 19 additions and 68 deletions
+16 -15
View File
@@ -87,7 +87,6 @@ from .const import (
DEFAULT_RETAIN,
DOMAIN,
ENTITY_PLATFORMS,
ENTRY_OPTION_FIELDS,
MQTT_CONNECTION_STATE,
PROTOCOL_5,
PROTOCOL_311,
@@ -154,7 +153,6 @@ __all__ = [
"DEFAULT_RETAIN",
"DOMAIN",
"ENTITY_PLATFORMS",
"ENTRY_OPTION_FIELDS",
"MQTT",
"MQTT_BASE_SCHEMA",
"MQTT_CONNECTION_STATE",
@@ -468,27 +466,30 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrate the options from config entry data."""
"""Migrate the config entry to the latest version."""
_LOGGER.debug("Migrating from version %s.%s", entry.version, entry.minor_version)
data: dict[str, Any] = dict(entry.data)
options: dict[str, Any] = dict(entry.options)
if entry.version == 1 and entry.minor_version < 2:
# Can be removed when the config entry is bumped to version 2.1
# with HA Core 2026.7.0. Read support for version 2.1 is expected with 2026.1
# From 2026.7 we will write version 2.1
for key in ENTRY_OPTION_FIELDS:
for key in (
CONF_DISCOVERY,
CONF_DISCOVERY_PREFIX,
"birth_message",
"will_message",
):
if key not in data:
continue
options[key] = data.pop(key)
# Write version 1.2 for backwards compatibility
hass.config_entries.async_update_entry(
entry,
data=data,
options=options,
version=1,
minor_version=2,
)
# Bump config entry to version 2.1
hass.config_entries.async_update_entry(
entry,
data=data,
options=options,
version=2,
minor_version=1,
)
_LOGGER.debug(
"Migration to version %s.%s successful", entry.version, entry.minor_version
+1 -12
View File
@@ -5,7 +5,7 @@ import logging
import jinja2
from homeassistant.components.alarm_control_panel import AlarmControlPanelEntityFeature
from homeassistant.const import CONF_DISCOVERY, CONF_PAYLOAD, Platform
from homeassistant.const import CONF_PAYLOAD, Platform
from homeassistant.exceptions import TemplateError
ATTR_DISCOVERY_HASH = "discovery_hash"
@@ -385,17 +385,6 @@ PAYLOAD_NONE = "None"
CONFIG_ENTRY_VERSION = 2
CONFIG_ENTRY_MINOR_VERSION = 1
# Split mqtt entry data and options
# Can be removed when config entry is bumped to version 2.1
# with HA Core 2026.7.0. Read support for version 2.1 is expected from 2026.1
# From 2026.7 we will write version 2.1
ENTRY_OPTION_FIELDS = (
CONF_DISCOVERY,
CONF_DISCOVERY_PREFIX,
"birth_message",
"will_message",
)
ENTITY_PLATFORMS = [
Platform.ALARM_CONTROL_PANEL,
Platform.BINARY_SENSOR,
+2 -41
View File
@@ -2586,8 +2586,8 @@ async def test_reconfigure_no_changed_password(
"expected_minor_version",
),
[
(1, 1, MOCK_ENTRY_DATA | MOCK_ENTRY_OPTIONS, {}, 1, 2),
(1, 2, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS, 1, 2),
(1, 1, MOCK_ENTRY_DATA | MOCK_ENTRY_OPTIONS, {}, 2, 1),
(1, 2, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS, 2, 1),
(2, 1, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS, 2, 1),
],
)
@@ -2623,45 +2623,6 @@ async def test_migrate_config_entry(
assert config_entry.minor_version == expected_minor_version
@pytest.mark.parametrize(
(
"version",
"minor_version",
"data",
"options",
),
[
(3, 1, MOCK_ENTRY_DATA, MOCK_ENTRY_OPTIONS),
],
)
@pytest.mark.usefixtures("mock_reload_after_entry_update")
async def test_migrate_of_incompatible_config_entry(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
version: int,
minor_version: int,
data: dict[str, Any],
options: dict[str, Any],
) -> None:
"""Test migrating a config entry."""
config_entry = hass.config_entries.async_entries(DOMAIN)[0]
# Mock an incompatible config entry version
hass.config_entries.async_update_entry(
config_entry,
data=data,
options=options,
version=version,
minor_version=minor_version,
)
await hass.async_block_till_done()
# Try to start MQTT with incompatible config entry
with pytest.raises(AssertionError):
await mqtt_mock_entry()
assert config_entry.state is config_entries.ConfigEntryState.MIGRATION_ERROR
@pytest.mark.parametrize(
(
"config_subentries_data",