Skip to content

Commit

Permalink
Update tests to use v2 entry and add separate migration test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed Dec 17, 2024
1 parent 069c4ab commit bc26ac0
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 30 deletions.
6 changes: 5 additions & 1 deletion homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@

_LOGGER = logging.getLogger(__name__)

ENTRY_VERSION = 2

# Split mqtt entry data and options
# Can be removed with HA Core 2026.1.0
ENTRY_OPTION_FIELDS = (
Expand Down Expand Up @@ -373,7 +375,9 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if key not in data:
continue
options[key] = data.pop(key)
hass.config_entries.async_update_entry(entry, data=data, options=options, version=2)
hass.config_entries.async_update_entry(
entry, data=data, options=options, version=ENTRY_VERSION
)
_LOGGER.debug("Migration to version %s successful", entry.version)
return True

Expand Down
2 changes: 1 addition & 1 deletion tests/components/mqtt/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from tests.typing import MqttMockPahoClient

ENTRY_DEFAULT_BIRTH_MESSAGE = {
mqtt.CONF_BROKER: "mock-broker",
mqtt.CONF_BIRTH_MESSAGE: {
mqtt.ATTR_TOPIC: "homeassistant/status",
mqtt.ATTR_PAYLOAD: "online",
Expand Down Expand Up @@ -91,6 +90,7 @@ async def setup_with_birth_msg_client_mock(
domain=mqtt.DOMAIN,
data=mqtt_config_entry_data or {mqtt.CONF_BROKER: "test-broker"},
options=mqtt_config_entry_options or {},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
hass.config.components.add(mqtt.DOMAIN)
Expand Down
28 changes: 24 additions & 4 deletions tests/components/mqtt/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class FakeInfo:
mqtt.CONF_BROKER: "test-broker",
mqtt.CONF_DISCOVERY: False,
},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
Expand Down Expand Up @@ -1136,7 +1137,11 @@ async def test_initial_setup_logs_error(
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test for setup failure if initial client connection fails."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "test-broker"},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
mqtt_client_mock.connect.side_effect = MagicMock(return_value=1)
try:
Expand Down Expand Up @@ -1239,7 +1244,11 @@ async def test_publish_error(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test publish error."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "test-broker"},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)

# simulate an Out of memory error
Expand Down Expand Up @@ -1381,7 +1390,9 @@ def _mock_ack(topic: str, qos: int = 0) -> tuple[int, int]:
)

entry = MockConfigEntry(
domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"}
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "test-broker"},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)

Expand All @@ -1407,7 +1418,11 @@ async def test_setup_raises_config_entry_not_ready_if_no_connect_broker(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test for setup failure if connection to broker is missing."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "test-broker"},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)

with patch(
Expand Down Expand Up @@ -1519,6 +1534,7 @@ async def test_custom_birth_message(
domain=mqtt.DOMAIN,
data=mqtt_config_entry_data,
options=mqtt_config_entry_options,
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
hass.config.components.add(mqtt.DOMAIN)
Expand Down Expand Up @@ -1567,6 +1583,7 @@ async def test_no_birth_message(
domain=mqtt.DOMAIN,
data=mqtt_config_entry_data,
options=mqtt_config_entry_options,
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
hass.config.components.add(mqtt.DOMAIN)
Expand Down Expand Up @@ -1605,6 +1622,7 @@ async def test_delayed_birth_message(
domain=mqtt.DOMAIN,
data=mqtt_config_entry_data,
options=mqtt_config_entry_options,
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
hass.config.components.add(mqtt.DOMAIN)
Expand Down Expand Up @@ -1675,6 +1693,7 @@ async def test_custom_will_message(
domain=mqtt.DOMAIN,
data=mqtt_config_entry_data,
options=mqtt_config_entry_options,
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
hass.config.components.add(mqtt.DOMAIN)
Expand Down Expand Up @@ -1711,6 +1730,7 @@ async def test_no_will_message(
domain=mqtt.DOMAIN,
data=mqtt_config_entry_data,
options=mqtt_config_entry_options,
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
hass.config.components.add(mqtt.DOMAIN)
Expand Down
6 changes: 5 additions & 1 deletion tests/components/mqtt/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,11 @@ async def help_test_reloadable(
mqtt.DOMAIN: {domain: [old_config_1, old_config_2]},
}
# Start the MQTT entry with the old config
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "test-broker"},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
mqtt_client_mock.connect.return_value = 0
with patch("homeassistant.config.load_yaml_config_file", return_value=old_config):
Expand Down
78 changes: 66 additions & 12 deletions tests/components/mqtt/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ async def test_manual_config_set(

async def test_user_single_instance(hass: HomeAssistant) -> None:
"""Test we only allow a single config flow."""
MockConfigEntry(domain="mqtt").add_to_hass(hass)
MockConfigEntry(domain="mqtt", version=mqtt.ENTRY_VERSION).add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
"mqtt", context={"source": config_entries.SOURCE_USER}
Expand All @@ -407,7 +407,7 @@ async def test_user_single_instance(hass: HomeAssistant) -> None:

async def test_hassio_already_configured(hass: HomeAssistant) -> None:
"""Test we only allow a single config flow."""
MockConfigEntry(domain="mqtt").add_to_hass(hass)
MockConfigEntry(domain="mqtt", version=mqtt.ENTRY_VERSION).add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
"mqtt", context={"source": config_entries.SOURCE_HASSIO}
Expand All @@ -419,7 +419,9 @@ async def test_hassio_already_configured(hass: HomeAssistant) -> None:
async def test_hassio_ignored(hass: HomeAssistant) -> None:
"""Test we supervisor discovered instance can be ignored."""
MockConfigEntry(
domain=mqtt.DOMAIN, source=config_entries.SOURCE_IGNORE
domain=mqtt.DOMAIN,
source=config_entries.SOURCE_IGNORE,
version=mqtt.ENTRY_VERSION,
).add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
Expand Down Expand Up @@ -1501,7 +1503,9 @@ async def test_step_reauth(
"""Test that the reauth step works."""

# Prepare the config entry
config_entry = MockConfigEntry(domain=mqtt.DOMAIN, data=test_input)
config_entry = MockConfigEntry(
domain=mqtt.DOMAIN, data=test_input, version=mqtt.ENTRY_VERSION
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)

Expand Down Expand Up @@ -1577,7 +1581,9 @@ async def test_step_hassio_reauth(
addon_info["hostname"] = "core-mosquitto"

# Prepare the config entry
config_entry = MockConfigEntry(domain=mqtt.DOMAIN, data=entry_data)
config_entry = MockConfigEntry(
domain=mqtt.DOMAIN, data=entry_data, version=mqtt.ENTRY_VERSION
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)

Expand Down Expand Up @@ -1659,7 +1665,9 @@ async def test_step_hassio_reauth_no_discovery_info(
addon_info["hostname"] = "core-mosquitto"

# Prepare the config entry
config_entry = MockConfigEntry(domain=mqtt.DOMAIN, data=entry_data)
config_entry = MockConfigEntry(
domain=mqtt.DOMAIN, data=entry_data, version=mqtt.ENTRY_VERSION
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)

Expand All @@ -1685,7 +1693,7 @@ async def test_reconfigure_user_connection_fails(
hass: HomeAssistant, mock_try_connection_time_out: MagicMock
) -> None:
"""Test if connection cannot be made."""
config_entry = MockConfigEntry(domain=mqtt.DOMAIN)
config_entry = MockConfigEntry(domain=mqtt.DOMAIN, version=mqtt.ENTRY_VERSION)
config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(
config_entry,
Expand Down Expand Up @@ -1726,7 +1734,7 @@ async def test_options_bad_birth_message_fails(
hass: HomeAssistant, mock_try_connection: MqttMockPahoClient
) -> None:
"""Test bad birth message."""
config_entry = MockConfigEntry(domain=mqtt.DOMAIN)
config_entry = MockConfigEntry(domain=mqtt.DOMAIN, version=mqtt.ENTRY_VERSION)
config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(
config_entry,
Expand Down Expand Up @@ -1760,7 +1768,7 @@ async def test_options_bad_will_message_fails(
hass: HomeAssistant, mock_try_connection: MagicMock
) -> None:
"""Test bad will message."""
config_entry = MockConfigEntry(domain=mqtt.DOMAIN)
config_entry = MockConfigEntry(domain=mqtt.DOMAIN, version=mqtt.ENTRY_VERSION)
config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(
config_entry,
Expand Down Expand Up @@ -1795,7 +1803,7 @@ async def test_try_connection_with_advanced_parameters(
hass: HomeAssistant, mock_try_connection_success: MqttMockPahoClient
) -> None:
"""Test config flow with advanced parameters from config."""
config_entry = MockConfigEntry(domain=mqtt.DOMAIN)
config_entry = MockConfigEntry(domain=mqtt.DOMAIN, version=mqtt.ENTRY_VERSION)
config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(
config_entry,
Expand Down Expand Up @@ -1913,7 +1921,7 @@ async def test_setup_with_advanced_settings(
"""Test config flow setup with advanced parameters."""
file_id = mock_process_uploaded_file.file_id

config_entry = MockConfigEntry(domain=mqtt.DOMAIN)
config_entry = MockConfigEntry(domain=mqtt.DOMAIN, version=mqtt.ENTRY_VERSION)
config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(
config_entry,
Expand Down Expand Up @@ -2065,7 +2073,7 @@ async def test_change_websockets_transport_to_tcp(
hass: HomeAssistant, mock_try_connection: MagicMock
) -> None:
"""Test reconfiguration flow changing websockets transport settings."""
config_entry = MockConfigEntry(domain=mqtt.DOMAIN)
config_entry = MockConfigEntry(domain=mqtt.DOMAIN, version=mqtt.ENTRY_VERSION)
config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(
config_entry,
Expand Down Expand Up @@ -2156,3 +2164,49 @@ async def test_reconfigure_flow_form(
mqtt.CONF_WS_PATH: "/some_new_path",
}
await hass.async_block_till_done(wait_background_tasks=True)


@pytest.mark.usefixtures("mock_reload_after_entry_update")
async def test_migrate_config_entry(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test migrating a v1 config entry."""
data = {
mqtt.CONF_BROKER: "test-broker",
CONF_PORT: 1234,
CONF_USERNAME: "user",
CONF_PASSWORD: "pass",
}
options = {
mqtt.CONF_DISCOVERY: True,
mqtt.CONF_BIRTH_MESSAGE: {
mqtt.ATTR_TOPIC: "ha_state/online",
mqtt.ATTR_PAYLOAD: "online",
mqtt.ATTR_QOS: 1,
mqtt.ATTR_RETAIN: True,
},
mqtt.CONF_WILL_MESSAGE: {
mqtt.ATTR_TOPIC: "ha_state/offline",
mqtt.ATTR_PAYLOAD: "offline",
mqtt.ATTR_QOS: 2,
mqtt.ATTR_RETAIN: False,
},
}

config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
# Revert to v1 config entry
hass.config_entries.async_update_entry(
config_entry,
data=data | options,
options={},
version=1,
)
await hass.async_block_till_done()
assert config_entry.data == data | options
assert config_entry.options == {}
assert config_entry.version == 1
await mqtt_mock_entry()
await hass.async_block_till_done()
assert config_entry.data == data
assert config_entry.options == options
assert config_entry.version == mqtt.ENTRY_VERSION
18 changes: 14 additions & 4 deletions tests/components/mqtt/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ async def test_cleanup_device_multiple_config_entries(
mqtt_mock = await mqtt_mock_entry()
ws_client = await hass_ws_client(hass)

config_entry = MockConfigEntry(domain="test", data={})
config_entry = MockConfigEntry(domain="test", data={}, version=mqtt.ENTRY_VERSION)
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
Expand Down Expand Up @@ -2042,7 +2042,7 @@ async def test_cleanup_device_multiple_config_entries_mqtt(
) -> None:
"""Test discovered device is cleaned up when removed through MQTT."""
mqtt_mock = await mqtt_mock_entry()
config_entry = MockConfigEntry(domain="test", data={})
config_entry = MockConfigEntry(domain="test", data={}, version=mqtt.ENTRY_VERSION)
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
Expand Down Expand Up @@ -2499,7 +2499,12 @@ def wait_birth(msg: ReceiveMessage) -> None:
"""Handle birth message."""
birth.set()

entry = MockConfigEntry(domain=mqtt.DOMAIN, data=ENTRY_DEFAULT_BIRTH_MESSAGE)
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "mock-broker"},
options=ENTRY_DEFAULT_BIRTH_MESSAGE,
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
with (
patch(
Expand Down Expand Up @@ -2564,7 +2569,12 @@ def wait_birth(msg: ReceiveMessage) -> None:
"""Handle birth message."""
birth.set()

entry = MockConfigEntry(domain=mqtt.DOMAIN, data=ENTRY_DEFAULT_BIRTH_MESSAGE)
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "mock-broker"},
options=ENTRY_DEFAULT_BIRTH_MESSAGE,
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)

with (
Expand Down
11 changes: 9 additions & 2 deletions tests/components/mqtt/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,11 @@ async def test_reload_entry_with_restored_subscriptions(
) -> None:
"""Test reloading the config entry with with subscriptions restored."""
# Setup the MQTT entry
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "test-broker"},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
hass.config.components.add(mqtt.DOMAIN)
with patch("homeassistant.config.load_yaml_config_file", return_value={}):
Expand Down Expand Up @@ -982,7 +986,9 @@ async def test_default_entry_setting_are_applied(

# Config entry data is incomplete but valid according the schema
entry = MockConfigEntry(
domain=mqtt.DOMAIN, data={"broker": "test-broker", "port": 1234}
domain=mqtt.DOMAIN,
data={"broker": "test-broker", "port": 1234},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)
hass.config.components.add(mqtt.DOMAIN)
Expand Down Expand Up @@ -1849,6 +1855,7 @@ async def test_unload_config_entry(
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "test-broker"},
version=mqtt.ENTRY_VERSION,
)
entry.add_to_hass(hass)

Expand Down
Loading

0 comments on commit bc26ac0

Please sign in to comment.