Mend incorrectly imported MQTT config entries (#68987)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Erik Montnemery
2022-04-01 17:11:31 +02:00
committed by GitHub
parent 2c82befc78
commit 9b21a48048
2 changed files with 114 additions and 13 deletions

View File

@@ -23,7 +23,7 @@ from homeassistant.const import (
TEMP_CELSIUS,
)
import homeassistant.core as ha
from homeassistant.core import CoreState, callback
from homeassistant.core import CoreState, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, template
from homeassistant.helpers.entity import Entity
@@ -1620,6 +1620,57 @@ async def test_setup_entry_with_config_override(hass, device_reg, mqtt_client_mo
assert device_entry is not None
async def test_update_incomplete_entry(
hass: HomeAssistant, device_reg, mqtt_client_mock, caplog
):
"""Test if the MQTT component loads when config entry data is incomplete."""
data = (
'{ "device":{"identifiers":["0AFFD2"]},'
' "state_topic": "foobar/sensor",'
' "unique_id": "unique" }'
)
# Config entry data is incomplete
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={"port": 1234})
entry.add_to_hass(hass)
# Mqtt present in yaml config
config = {"broker": "yaml_broker"}
await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
# Config entry data should now be updated
assert entry.data == {
"port": 1234,
"broker": "yaml_broker",
}
# Warnings about broker deprecated, but not about other keys with default values
assert (
"The 'broker' option is deprecated, please remove it from your configuration"
in caplog.text
)
assert (
"Deprecated configuration settings found in configuration.yaml. These settings "
"from your configuration entry will override: {'broker': 'yaml_broker'}"
in caplog.text
)
# Discover a device to verify the entry was setup correctly
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)
await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is not None
async def test_fail_no_broker(hass, device_reg, mqtt_client_mock, caplog):
"""Test if the MQTT component loads when broker configuration is missing."""
# Config entry data is incomplete
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={})
entry.add_to_hass(hass)
assert not await hass.config_entries.async_setup(entry.entry_id)
assert "MQTT broker is not configured, please configure it" in caplog.text
@pytest.mark.no_fail_on_log_exception
async def test_message_callback_exception_gets_logged(hass, caplog, mqtt_mock):
"""Test exception raised by message handler."""