From 5eb0a337956b3d53aa3bee115e0d03e771caa143 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Thu, 19 Oct 2023 18:51:47 +0200 Subject: [PATCH] Do not fail MQTT setup if text's configured via yaml can't be validated (#102322) Add text --- .../components/mqtt/config_integration.py | 6 +---- homeassistant/components/mqtt/text.py | 27 +++++++------------ tests/components/mqtt/test_text.py | 16 ++++++----- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/mqtt/config_integration.py b/homeassistant/components/mqtt/config_integration.py index 82f83fd50a0..f5005390dc3 100644 --- a/homeassistant/components/mqtt/config_integration.py +++ b/homeassistant/components/mqtt/config_integration.py @@ -26,7 +26,6 @@ from . import ( select as select_platform, sensor as sensor_platform, switch as switch_platform, - text as text_platform, update as update_platform, vacuum as vacuum_platform, water_heater as water_heater_platform, @@ -100,10 +99,7 @@ CONFIG_SCHEMA_BASE = vol.Schema( cv.ensure_list, [switch_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] ), - Platform.TEXT.value: vol.All( - cv.ensure_list, - [text_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] - ), + Platform.TEXT.value: vol.All(cv.ensure_list, [dict]), Platform.UPDATE.value: vol.All( cv.ensure_list, [update_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] diff --git a/homeassistant/components/mqtt/text.py b/homeassistant/components/mqtt/text.py index 630951f171e..3fd0f9a4198 100644 --- a/homeassistant/components/mqtt/text.py +++ b/homeassistant/components/mqtt/text.py @@ -2,7 +2,6 @@ from __future__ import annotations from collections.abc import Callable -import functools import logging import re from typing import Any @@ -22,7 +21,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from homeassistant.helpers.typing import ConfigType from . import subscription from .config import MQTT_RW_SCHEMA @@ -38,7 +37,7 @@ from .debug_info import log_messages from .mixins import ( MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, - async_setup_entry_helper, + async_mqtt_entry_helper, write_state_on_attr_change, ) from .models import ( @@ -108,21 +107,15 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up MQTT text through YAML and through MQTT discovery.""" - setup = functools.partial( - _async_setup_entity, hass, async_add_entities, config_entry=config_entry + await async_mqtt_entry_helper( + hass, + config_entry, + MqttTextEntity, + text.DOMAIN, + async_add_entities, + DISCOVERY_SCHEMA, + PLATFORM_SCHEMA_MODERN, ) - await async_setup_entry_helper(hass, text.DOMAIN, setup, DISCOVERY_SCHEMA) - - -async def _async_setup_entity( - hass: HomeAssistant, - async_add_entities: AddEntitiesCallback, - config: ConfigType, - config_entry: ConfigEntry, - discovery_data: DiscoveryInfoType | None = None, -) -> None: - """Set up the MQTT text.""" - async_add_entities([MqttTextEntity(hass, config, config_entry, discovery_data)]) class MqttTextEntity(MqttEntity, TextEntity): diff --git a/tests/components/mqtt/test_text.py b/tests/components/mqtt/test_text.py index bf6fe1b0130..80f38dffcf9 100644 --- a/tests/components/mqtt/test_text.py +++ b/tests/components/mqtt/test_text.py @@ -205,11 +205,13 @@ async def test_controlling_validation_state_via_topic( ], ) async def test_attribute_validation_max_greater_then_min( - hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, ) -> None: """Test the validation of min and max configuration attributes.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() + assert "not a valid value" in caplog.text @pytest.mark.parametrize( @@ -228,11 +230,13 @@ async def test_attribute_validation_max_greater_then_min( ], ) async def test_attribute_validation_max_not_greater_then_max_state_length( - hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, ) -> None: """Test the max value of of max configuration attribute.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() + assert "not a valid value" in caplog.text @pytest.mark.parametrize(