Fix MQTT config schema to ensure correct validation (#73619)

* Ensure config schema validation

* Use correct schema for device_tracker

* Remove schema validation from the platform setup

* Remove loop to build schema
This commit is contained in:
Jan Bouwhuis 2022-06-20 08:51:12 +02:00 committed by GitHub
parent fcd8859542
commit 57daeaa174
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 258 additions and 176 deletions

View file

@ -14,7 +14,7 @@ import yaml
from homeassistant import config as hass_config
from homeassistant.components import mqtt
from homeassistant.components.mqtt import debug_info
from homeassistant.components.mqtt import CONFIG_SCHEMA, debug_info
from homeassistant.components.mqtt.mixins import MQTT_ENTITY_DEVICE_INFO_SCHEMA
from homeassistant.components.mqtt.models import ReceiveMessage
from homeassistant.const import (
@ -1373,40 +1373,47 @@ async def test_setup_override_configuration(hass, caplog, tmp_path):
assert calls_username_password_set[0][1] == "somepassword"
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT])
@patch("homeassistant.components.mqtt.PLATFORMS", [])
async def test_setup_manual_mqtt_with_platform_key(hass, caplog):
"""Test set up a manual MQTT item with a platform key."""
config = {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
await help_test_setup_manual_entity_from_yaml(hass, "light", config)
with pytest.raises(AssertionError):
await help_test_setup_manual_entity_from_yaml(hass, "light", config)
assert (
"Invalid config for [light]: [platform] is an invalid option for [light]. "
"Check: light->platform. (See ?, line ?)" in caplog.text
"Invalid config for [mqtt]: [platform] is an invalid option for [mqtt]"
in caplog.text
)
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT])
@patch("homeassistant.components.mqtt.PLATFORMS", [])
async def test_setup_manual_mqtt_with_invalid_config(hass, caplog):
"""Test set up a manual MQTT item with an invalid config."""
config = {"name": "test"}
await help_test_setup_manual_entity_from_yaml(hass, "light", config)
with pytest.raises(AssertionError):
await help_test_setup_manual_entity_from_yaml(hass, "light", config)
assert (
"Invalid config for [light]: required key not provided @ data['command_topic']."
"Invalid config for [mqtt]: required key not provided @ data['mqtt']['light'][0]['command_topic']."
" Got None. (See ?, line ?)" in caplog.text
)
@patch("homeassistant.components.mqtt.PLATFORMS", [])
async def test_setup_manual_mqtt_empty_platform(hass, caplog):
"""Test set up a manual MQTT platform without items."""
config = None
config = []
await help_test_setup_manual_entity_from_yaml(hass, "light", config)
assert "voluptuous.error.MultipleInvalid" not in caplog.text
@patch("homeassistant.components.mqtt.PLATFORMS", [])
async def test_setup_mqtt_client_protocol(hass):
"""Test MQTT client protocol setup."""
entry = MockConfigEntry(
domain=mqtt.DOMAIN,
data={mqtt.CONF_BROKER: "test-broker", mqtt.config.CONF_PROTOCOL: "3.1"},
data={
mqtt.CONF_BROKER: "test-broker",
mqtt.config_integration.CONF_PROTOCOL: "3.1",
},
)
with patch("paho.mqtt.client.Client") as mock_client:
mock_client.on_connect(return_value=0)
@ -2612,3 +2619,10 @@ async def test_one_deprecation_warning_per_platform(
):
count += 1
assert count == 1
async def test_config_schema_validation(hass):
"""Test invalid platform options in the config schema do not pass the config validation."""
config = {"mqtt": {"sensor": [{"some_illegal_topic": "mystate/topic/path"}]}}
with pytest.raises(vol.MultipleInvalid):
CONFIG_SCHEMA(config)