Compare commits

...

2 Commits

Author SHA1 Message Date
G Johansson
03c7d14272 Add reload for adding subentry 2025-09-27 19:33:08 +00:00
G Johansson
3bed83005f Use OptionsFlowWithReload in mqtt 2025-09-27 19:33:08 +00:00
3 changed files with 14 additions and 30 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

@@ -60,7 +60,7 @@ from homeassistant.config_entries import (
ConfigFlow,
ConfigFlowResult,
ConfigSubentryFlow,
OptionsFlow,
OptionsFlowWithReload,
SubentryFlowResult,
)
from homeassistant.const import (
@@ -3671,7 +3671,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:
@@ -4184,7 +4184,7 @@ class MQTTSubentryFlowHandler(ConfigSubentryFlow):
full_entity_name = device_name
self._async_update_component_data_defaults()
return self.async_create_entry(
result = self.async_create_entry(
data=self._subentry_data,
title=self._subentry_data[CONF_DEVICE][CONF_NAME],
description_placeholders={
@@ -4192,6 +4192,8 @@ class MQTTSubentryFlowHandler(ConfigSubentryFlow):
CONF_PLATFORM: platform,
},
)
self.hass.config_entries.async_schedule_reload(self._get_entry().entry_id)
return result
async def async_step_availability(
self, user_input: dict[str, Any] | None = None
@@ -4289,7 +4291,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,
@@ -4491,7 +4493,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 (
@@ -202,8 +205,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
@@ -1339,11 +1342,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()
@@ -1357,7 +1360,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
@@ -1396,10 +1398,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,
@@ -1423,7 +1421,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)
@@ -1452,10 +1449,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."""