Use OptionsFlowWithReload in mqtt

This commit is contained in:
G Johansson
2025-08-13 12:45:45 +00:00
parent b40f381164
commit ad5bfdca69
3 changed files with 11 additions and 29 deletions

View File

@@ -246,14 +246,6 @@ MQTT_PUBLISH_SCHEMA = vol.Schema(
)
async def _async_config_entry_updated(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle signals of config entry being updated.
Causes for this is config entry options changing.
"""
await hass.config_entries.async_reload(entry.entry_id)
@callback
def _async_remove_mqtt_issues(hass: HomeAssistant, mqtt_data: MqttData) -> None:
"""Unregister open config issues."""
@@ -435,9 +427,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
mqtt_data.subscriptions_to_restore
)
mqtt_data.subscriptions_to_restore = set()
mqtt_data.reload_dispatchers.append(
entry.add_update_listener(_async_config_entry_updated)
)
return (mqtt_data, conf)

View File

@@ -59,7 +59,7 @@ from homeassistant.config_entries import (
ConfigFlow,
ConfigFlowResult,
ConfigSubentryFlow,
OptionsFlow,
OptionsFlowWithReload,
SubentryFlowResult,
)
from homeassistant.const import (
@@ -3313,7 +3313,7 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
)
class MQTTOptionsFlowHandler(OptionsFlow):
class MQTTOptionsFlowHandler(OptionsFlowWithReload):
"""Handle MQTT options."""
async def async_step_init(self, user_input: None = None) -> ConfigFlowResult:
@@ -3927,7 +3927,7 @@ class MQTTSubentryFlowHandler(ConfigSubentryFlow):
):
entity_registry.async_remove(entity_id)
return self.async_update_and_abort(
return self.async_update_reload_and_abort(
entry,
subentry,
data=self._subentry_data,
@@ -4129,7 +4129,7 @@ def _validate_pki_file(
async def async_get_broker_settings( # noqa: C901
flow: ConfigFlow | OptionsFlow,
flow: ConfigFlow | OptionsFlowWithReload,
fields: OrderedDict[Any, Any],
entry_config: MappingProxyType[str, Any] | None,
user_input: dict[str, Any] | None,

View File

@@ -17,7 +17,10 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components import mqtt
from homeassistant.components.hassio import AddonError
from homeassistant.components.mqtt.config_flow import PWD_NOT_CHANGED
from homeassistant.components.mqtt.config_flow import (
PWD_NOT_CHANGED,
MQTTOptionsFlowHandler,
)
from homeassistant.components.mqtt.util import learn_more_url
from homeassistant.config_entries import ConfigSubentry, ConfigSubentryData
from homeassistant.const import (
@@ -196,8 +199,8 @@ def mock_ssl_context(mock_context_client_key: bytes) -> Generator[dict[str, Magi
@pytest.fixture
def mock_reload_after_entry_update() -> Generator[MagicMock]:
"""Mock out the reload after updating the entry."""
with patch(
"homeassistant.components.mqtt._async_config_entry_updated"
with patch.object(
MQTTOptionsFlowHandler, "automatic_reload", return_value=False
) as mock_reload:
yield mock_reload
@@ -1333,11 +1336,11 @@ async def test_keepalive_validation(
assert result["reason"] == "reconfigure_successful"
@pytest.mark.usefixtures("mock_reload_after_entry_update")
async def test_disable_birth_will(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
mock_try_connection: MagicMock,
mock_reload_after_entry_update: MagicMock,
) -> None:
"""Test disabling birth and will."""
await mqtt_mock_entry()
@@ -1351,7 +1354,6 @@ async def test_disable_birth_will(
},
)
await hass.async_block_till_done()
mock_reload_after_entry_update.reset_mock()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] is FlowResultType.FORM
@@ -1390,10 +1392,6 @@ async def test_disable_birth_will(
mqtt.CONF_WILL_MESSAGE: {},
}
await hass.async_block_till_done()
# assert that the entry was reloaded with the new config
assert mock_reload_after_entry_update.call_count == 1
async def test_invalid_discovery_prefix(
hass: HomeAssistant,
@@ -1417,7 +1415,6 @@ async def test_invalid_discovery_prefix(
},
)
await hass.async_block_till_done()
mock_reload_after_entry_update.reset_mock()
mqtt_mock.async_connect.reset_mock()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
@@ -1446,10 +1443,6 @@ async def test_invalid_discovery_prefix(
mqtt.CONF_DISCOVERY_PREFIX: "homeassistant",
}
await hass.async_block_till_done()
# assert that the entry was not reloaded with the new config
assert mock_reload_after_entry_update.call_count == 0
def get_default(schema: vol.Schema, key: str) -> Any | None:
"""Get default value for key in voluptuous schema."""