From 223f3a434b5c4a75ee7bf5b3cac688fdf5023c5f Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Wed, 4 Oct 2023 19:36:34 +0200 Subject: [PATCH] Raise vol.Invalid for invalid mqtt device_tracker config (#101399) Raise vol.Invalid for invalid mqtt device_tracker --- .../components/mqtt/device_tracker.py | 2 +- tests/components/mqtt/test_discovery.py | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/mqtt/device_tracker.py b/homeassistant/components/mqtt/device_tracker.py index 2270f2b4031..2557a2afb5d 100644 --- a/homeassistant/components/mqtt/device_tracker.py +++ b/homeassistant/components/mqtt/device_tracker.py @@ -52,7 +52,7 @@ DEFAULT_SOURCE_TYPE = SourceType.GPS def valid_config(config: ConfigType) -> ConfigType: """Check if there is a state topic or json_attributes_topic.""" if CONF_STATE_TOPIC not in config and CONF_JSON_ATTRS_TOPIC not in config: - raise vol.MultipleInvalid( + raise vol.Invalid( f"Invalid device tracker config, missing {CONF_STATE_TOPIC} or {CONF_JSON_ATTRS_TOPIC}, got: {config}" ) return config diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index c528687623b..4d0b8457049 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -122,6 +122,28 @@ async def test_invalid_json( assert not mock_dispatcher_send.called +@pytest.mark.no_fail_on_log_exception +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) +async def test_discovery_schema_error( + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test unexpected error JSON config.""" + with patch( + "homeassistant.components.mqtt.binary_sensor.DISCOVERY_SCHEMA", + side_effect=AttributeError("Attribute abc not found"), + ): + await mqtt_mock_entry() + async_fire_mqtt_message( + hass, + "homeassistant/binary_sensor/bla/config", + '{"name": "Beer", "state_topic": "ok"}', + ) + await hass.async_block_till_done() + assert "AttributeError: Attribute abc not found" in caplog.text + + async def test_only_valid_components( hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator,