Fix mqtt is not reloading without yaml config (#103159)

This commit is contained in:
Jan Bouwhuis 2023-11-01 09:25:56 +01:00 committed by GitHub
parent 6ea5af7575
commit daee5baef6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View file

@ -233,7 +233,7 @@ async def async_check_config_schema(
) -> None: ) -> None:
"""Validate manually configured MQTT items.""" """Validate manually configured MQTT items."""
mqtt_data = get_mqtt_data(hass) mqtt_data = get_mqtt_data(hass)
mqtt_config: list[dict[str, list[ConfigType]]] = config_yaml[DOMAIN] mqtt_config: list[dict[str, list[ConfigType]]] = config_yaml.get(DOMAIN, {})
for mqtt_config_item in mqtt_config: for mqtt_config_item in mqtt_config:
for domain, config_items in mqtt_config_item.items(): for domain, config_items in mqtt_config_item.items():
schema = mqtt_data.reload_schema[domain] schema = mqtt_data.reload_schema[domain]

View file

@ -3974,3 +3974,39 @@ async def test_reload_with_invalid_config(
# Test nothing changed as loading the config failed # Test nothing changed as loading the config failed
assert hass.states.get("sensor.test") is not None assert hass.states.get("sensor.test") is not None
@pytest.mark.parametrize(
"hass_config",
[
{
"mqtt": [
{
"sensor": {
"name": "test",
"state_topic": "test-topic",
}
},
]
}
],
)
async def test_reload_with_empty_config(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None:
"""Test reloading yaml config fails."""
await mqtt_mock_entry()
assert hass.states.get("sensor.test") is not None
# Reload with an empty config and assert again
with patch("homeassistant.config.load_yaml_config_file", return_value={}):
await hass.services.async_call(
"mqtt",
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()
assert hass.states.get("sensor.test") is None