Use OptionsFlowWithReload in mqtt

This commit is contained in:
G Johansson 2025-07-19 18:49:30 +00:00
parent 665991a3c1
commit a7d17bfad7
3 changed files with 12 additions and 25 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)
@ -607,7 +596,7 @@ async def async_remove_config_entry_device(
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
) -> bool:
"""Remove MQTT config entry from a device."""
from . import device_automation # noqa: PLC0415
from . import device_automation
await device_automation.async_removed_from_device(hass, device_entry.id)
return True

View File

@ -52,7 +52,7 @@ from homeassistant.config_entries import (
ConfigFlow,
ConfigFlowResult,
ConfigSubentryFlow,
OptionsFlow,
OptionsFlowWithReload,
SubentryFlowResult,
)
from homeassistant.const import (
@ -2537,7 +2537,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:
@ -3353,7 +3353,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,
@ -3682,7 +3682,7 @@ def try_connection(
"""Test if we can connect to an MQTT broker."""
# We don't import on the top because some integrations
# should be able to optionally rely on MQTT.
import paho.mqtt.client as mqtt # noqa: PLC0415
import paho.mqtt.client as mqtt
mqtt_client_setup = MqttClientSetup(user_input)
mqtt_client_setup.setup()

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 (
@ -193,8 +196,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
@ -1330,11 +1333,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()
@ -1348,7 +1351,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
@ -1387,10 +1389,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,