diff --git a/homeassistant/components/mqtt/mixins.py b/homeassistant/components/mqtt/mixins.py index 46744c4d65d..34b61d89c48 100644 --- a/homeassistant/components/mqtt/mixins.py +++ b/homeassistant/components/mqtt/mixins.py @@ -244,6 +244,20 @@ class SetupEntity(Protocol): """Define setup_entities type.""" +@callback +def async_handle_schema_error( + discovery_payload: MQTTDiscoveryPayload, err: vol.MultipleInvalid +) -> None: + """Help handling schema errors on MQTT discovery messages.""" + discovery_topic: str = discovery_payload.discovery_data[ATTR_DISCOVERY_TOPIC] + _LOGGER.error( + "Error '%s' when processing MQTT discovery message topic: '%s', message: '%s'", + err, + discovery_topic, + discovery_payload, + ) + + async def async_setup_entry_helper( hass: HomeAssistant, domain: str, @@ -269,8 +283,15 @@ async def async_setup_entry_helper( try: config: DiscoveryInfoType = discovery_schema(discovery_payload) await async_setup(config, discovery_data=discovery_data) + except vol.Invalid as err: + discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] + clear_discovery_hash(hass, discovery_hash) + async_dispatcher_send( + hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None + ) + async_handle_schema_error(discovery_payload, err) except Exception: - discovery_hash: tuple[str, str] = discovery_data[ATTR_DISCOVERY_HASH] + discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] clear_discovery_hash(hass, discovery_hash) async_dispatcher_send( hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None @@ -1037,7 +1058,11 @@ class MqttEntity( async def discovery_update(self, discovery_payload: MQTTDiscoveryPayload) -> None: """Handle updated discovery message.""" - config: DiscoveryInfoType = self.config_schema()(discovery_payload) + try: + config: DiscoveryInfoType = self.config_schema()(discovery_payload) + except vol.Invalid as err: + async_handle_schema_error(discovery_payload, err) + return self._config = config self._setup_common_attributes_from_config(self._config) self._setup_from_config(self._config) diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index 8d3c43744fc..f35af9fb037 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -1115,7 +1115,6 @@ async def test_discovery_expansion_2( @patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SWITCH]) -@pytest.mark.no_fail_on_log_exception async def test_discovery_expansion_3( hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator, @@ -1146,10 +1145,7 @@ async def test_discovery_expansion_3( assert hass.states.get("switch.DiscoveryExpansionTest1") is None # Make sure the malformed availability data does not trip up discovery by asserting # there are schema valdiation errors in the log - assert ( - "voluptuous.error.MultipleInvalid: expected a dictionary @ data['availability'][0]" - in caplog.text - ) + assert "expected a dictionary @ data['availability'][0]" in caplog.text async def test_discovery_expansion_without_encoding_and_value_template_1(