Skip to content

Commit dd3c9ab

Browse files
Use OptionsFlowWithReload in mqtt (home-assistant#149092)
1 parent fea2ef1 commit dd3c9ab

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

homeassistant/components/mqtt/__init__.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,6 @@
246246
)
247247

248248

249-
async def _async_config_entry_updated(hass: HomeAssistant, entry: ConfigEntry) -> None:
250-
"""Handle signals of config entry being updated.
251-
252-
Causes for this is config entry options changing.
253-
"""
254-
await hass.config_entries.async_reload(entry.entry_id)
255-
256-
257249
@callback
258250
def _async_remove_mqtt_issues(hass: HomeAssistant, mqtt_data: MqttData) -> None:
259251
"""Unregister open config issues."""
@@ -435,9 +427,6 @@ async def _setup_client() -> tuple[MqttData, dict[str, Any]]:
435427
mqtt_data.subscriptions_to_restore
436428
)
437429
mqtt_data.subscriptions_to_restore = set()
438-
mqtt_data.reload_dispatchers.append(
439-
entry.add_update_listener(_async_config_entry_updated)
440-
)
441430

442431
return (mqtt_data, conf)
443432

homeassistant/components/mqtt/config_flow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
ConfigFlow,
5353
ConfigFlowResult,
5454
ConfigSubentryFlow,
55-
OptionsFlow,
55+
OptionsFlowWithReload,
5656
SubentryFlowResult,
5757
)
5858
from homeassistant.const import (
@@ -2537,7 +2537,7 @@ async def async_step_hassio_confirm(
25372537
)
25382538

25392539

2540-
class MQTTOptionsFlowHandler(OptionsFlow):
2540+
class MQTTOptionsFlowHandler(OptionsFlowWithReload):
25412541
"""Handle MQTT options."""
25422542

25432543
async def async_step_init(self, user_input: None = None) -> ConfigFlowResult:
@@ -3353,7 +3353,7 @@ def _validate_pki_file(
33533353

33543354

33553355
async def async_get_broker_settings( # noqa: C901
3356-
flow: ConfigFlow | OptionsFlow,
3356+
flow: ConfigFlow | OptionsFlowWithReload,
33573357
fields: OrderedDict[Any, Any],
33583358
entry_config: MappingProxyType[str, Any] | None,
33593359
user_input: dict[str, Any] | None,

tests/components/mqtt/test_config_flow.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
from homeassistant import config_entries
1818
from homeassistant.components import mqtt
1919
from homeassistant.components.hassio import AddonError
20-
from homeassistant.components.mqtt.config_flow import PWD_NOT_CHANGED
20+
from homeassistant.components.mqtt.config_flow import (
21+
PWD_NOT_CHANGED,
22+
MQTTOptionsFlowHandler,
23+
)
2124
from homeassistant.components.mqtt.util import learn_more_url
2225
from homeassistant.config_entries import ConfigSubentry, ConfigSubentryData
2326
from homeassistant.const import (
@@ -193,8 +196,8 @@ def mock_ssl_context(mock_context_client_key: bytes) -> Generator[dict[str, Magi
193196
@pytest.fixture
194197
def mock_reload_after_entry_update() -> Generator[MagicMock]:
195198
"""Mock out the reload after updating the entry."""
196-
with patch(
197-
"homeassistant.components.mqtt._async_config_entry_updated"
199+
with patch.object(
200+
MQTTOptionsFlowHandler, "automatic_reload", return_value=False
198201
) as mock_reload:
199202
yield mock_reload
200203

@@ -1330,11 +1333,11 @@ async def test_keepalive_validation(
13301333
assert result["reason"] == "reconfigure_successful"
13311334

13321335

1336+
@pytest.mark.usefixtures("mock_reload_after_entry_update")
13331337
async def test_disable_birth_will(
13341338
hass: HomeAssistant,
13351339
mqtt_mock_entry: MqttMockHAClientGenerator,
13361340
mock_try_connection: MagicMock,
1337-
mock_reload_after_entry_update: MagicMock,
13381341
) -> None:
13391342
"""Test disabling birth and will."""
13401343
await mqtt_mock_entry()
@@ -1348,7 +1351,6 @@ async def test_disable_birth_will(
13481351
},
13491352
)
13501353
await hass.async_block_till_done()
1351-
mock_reload_after_entry_update.reset_mock()
13521354

13531355
result = await hass.config_entries.options.async_init(config_entry.entry_id)
13541356
assert result["type"] is FlowResultType.FORM
@@ -1387,10 +1389,6 @@ async def test_disable_birth_will(
13871389
mqtt.CONF_WILL_MESSAGE: {},
13881390
}
13891391

1390-
await hass.async_block_till_done()
1391-
# assert that the entry was reloaded with the new config
1392-
assert mock_reload_after_entry_update.call_count == 1
1393-
13941392

13951393
async def test_invalid_discovery_prefix(
13961394
hass: HomeAssistant,

0 commit comments

Comments
 (0)