diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 4f00c4da958..9e3dc59f852 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -246,6 +246,14 @@ 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.""" @@ -427,6 +435,9 @@ 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) diff --git a/homeassistant/components/mqtt/config_flow.py b/homeassistant/components/mqtt/config_flow.py index 023872d410c..52f00c82c27 100644 --- a/homeassistant/components/mqtt/config_flow.py +++ b/homeassistant/components/mqtt/config_flow.py @@ -52,7 +52,7 @@ from homeassistant.config_entries import ( ConfigFlow, ConfigFlowResult, ConfigSubentryFlow, - OptionsFlowWithReload, + OptionsFlow, SubentryFlowResult, ) from homeassistant.const import ( @@ -2537,7 +2537,7 @@ class FlowHandler(ConfigFlow, domain=DOMAIN): ) -class MQTTOptionsFlowHandler(OptionsFlowWithReload): +class MQTTOptionsFlowHandler(OptionsFlow): """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 | OptionsFlowWithReload, + flow: ConfigFlow | OptionsFlow, fields: OrderedDict[Any, Any], entry_config: MappingProxyType[str, Any] | None, user_input: dict[str, Any] | None, diff --git a/tests/components/mqtt/test_config_flow.py b/tests/components/mqtt/test_config_flow.py index b45a4a66aa9..ce0a0c44a79 100644 --- a/tests/components/mqtt/test_config_flow.py +++ b/tests/components/mqtt/test_config_flow.py @@ -17,10 +17,7 @@ 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, - MQTTOptionsFlowHandler, -) +from homeassistant.components.mqtt.config_flow import PWD_NOT_CHANGED from homeassistant.components.mqtt.util import learn_more_url from homeassistant.config_entries import ConfigSubentry, ConfigSubentryData from homeassistant.const import ( @@ -196,8 +193,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.object( - MQTTOptionsFlowHandler, "automatic_reload", return_value=False + with patch( + "homeassistant.components.mqtt._async_config_entry_updated" ) as mock_reload: yield mock_reload @@ -1333,11 +1330,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,6 +1348,7 @@ 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 @@ -1389,6 +1387,10 @@ 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,