From 1efbba2631f1c35be28a758aafad4ff6e630782f Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Fri, 20 Oct 2023 10:51:42 +0200 Subject: [PATCH] Do not fail MQTT setup if covers configured via yaml can't be validated (#102304) Add cover --- .../components/mqtt/config_integration.py | 6 +--- homeassistant/components/mqtt/cover.py | 27 +++++++---------- tests/components/mqtt/test_cover.py | 29 +++++++------------ 3 files changed, 21 insertions(+), 41 deletions(-) diff --git a/homeassistant/components/mqtt/config_integration.py b/homeassistant/components/mqtt/config_integration.py index 5c254da0c27..3eca9a12e87 100644 --- a/homeassistant/components/mqtt/config_integration.py +++ b/homeassistant/components/mqtt/config_integration.py @@ -15,7 +15,6 @@ from homeassistant.const import ( from homeassistant.helpers import config_validation as cv from . import ( - cover as cover_platform, event as event_platform, sensor as sensor_platform, ) @@ -40,10 +39,7 @@ CONFIG_SCHEMA_BASE = vol.Schema( Platform.BUTTON.value: vol.All(cv.ensure_list, [dict]), Platform.CAMERA.value: vol.All(cv.ensure_list, [dict]), Platform.CLIMATE.value: vol.All(cv.ensure_list, [dict]), - Platform.COVER.value: vol.All( - cv.ensure_list, - [cover_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] - ), + Platform.COVER.value: vol.All(cv.ensure_list, [dict]), Platform.DEVICE_TRACKER.value: vol.All(cv.ensure_list, [dict]), Platform.EVENT.value: vol.All( cv.ensure_list, diff --git a/homeassistant/components/mqtt/cover.py b/homeassistant/components/mqtt/cover.py index 39c4090109c..367390aefaf 100644 --- a/homeassistant/components/mqtt/cover.py +++ b/homeassistant/components/mqtt/cover.py @@ -2,7 +2,6 @@ from __future__ import annotations from contextlib import suppress -import functools import logging from typing import Any @@ -31,7 +30,7 @@ from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.service_info.mqtt import ReceivePayloadType -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from homeassistant.helpers.typing import ConfigType from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads from . import subscription @@ -48,7 +47,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 MqttCommandTemplate, MqttValueTemplate, ReceiveMessage @@ -220,21 +219,15 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up MQTT cover 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, + MqttCover, + cover.DOMAIN, + async_add_entities, + DISCOVERY_SCHEMA, + PLATFORM_SCHEMA_MODERN, ) - await async_setup_entry_helper(hass, cover.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 Cover.""" - async_add_entities([MqttCover(hass, config, config_entry, discovery_data)]) class MqttCover(MqttEntity, CoverEntity): diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index 74dc48f4402..f3bf92951b0 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -891,11 +891,9 @@ async def test_optimistic_position( mqtt_mock_entry: MqttMockHAClientGenerator, ) -> None: """Test optimistic position is not supported.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() assert ( - "Invalid config for [mqtt]: 'set_position_topic' must be set together with 'position_topic'" - in caplog.text + "'set_position_topic' must be set together with 'position_topic'" in caplog.text ) @@ -2663,9 +2661,8 @@ async def test_invalid_device_class( mqtt_mock_entry: MqttMockHAClientGenerator, ) -> None: """Test the setting of an invalid device class.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() - assert "Invalid config for [mqtt]: expected CoverDeviceClass" in caplog.text + assert await mqtt_mock_entry() + assert "expected CoverDeviceClass" in caplog.text async def test_setting_attribute_via_mqtt_json_message( @@ -3402,8 +3399,7 @@ async def test_set_position_topic_without_get_position_topic_error( mqtt_mock_entry: MqttMockHAClientGenerator, ) -> None: """Test error when set_position_topic is used without position_topic.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() assert ( f"'{CONF_SET_POSITION_TOPIC}' must be set together with '{CONF_GET_POSITION_TOPIC}'." ) in caplog.text @@ -3429,8 +3425,7 @@ async def test_value_template_without_state_topic_error( mqtt_mock_entry: MqttMockHAClientGenerator, ) -> None: """Test error when value_template is used and state_topic is missing.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() assert ( f"'{CONF_VALUE_TEMPLATE}' must be set together with '{CONF_STATE_TOPIC}'." ) in caplog.text @@ -3456,8 +3451,7 @@ async def test_position_template_without_position_topic_error( mqtt_mock_entry: MqttMockHAClientGenerator, ) -> None: """Test error when position_template is used and position_topic is missing.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() assert ( f"'{CONF_GET_POSITION_TEMPLATE}' must be set together with '{CONF_GET_POSITION_TOPIC}'." in caplog.text @@ -3484,8 +3478,7 @@ async def test_set_position_template_without_set_position_topic( mqtt_mock_entry: MqttMockHAClientGenerator, ) -> None: """Test error when set_position_template is used and set_position_topic is missing.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() assert ( f"'{CONF_SET_POSITION_TEMPLATE}' must be set together with '{CONF_SET_POSITION_TOPIC}'." in caplog.text @@ -3512,8 +3505,7 @@ async def test_tilt_command_template_without_tilt_command_topic( mqtt_mock_entry: MqttMockHAClientGenerator, ) -> None: """Test error when tilt_command_template is used and tilt_command_topic is missing.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() assert ( f"'{CONF_TILT_COMMAND_TEMPLATE}' must be set together with '{CONF_TILT_COMMAND_TOPIC}'." in caplog.text @@ -3540,8 +3532,7 @@ async def test_tilt_status_template_without_tilt_status_topic_topic( mqtt_mock_entry: MqttMockHAClientGenerator, ) -> None: """Test error when tilt_status_template is used and tilt_status_topic is missing.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() assert ( f"'{CONF_TILT_STATUS_TEMPLATE}' must be set together with '{CONF_TILT_STATUS_TOPIC}'." in caplog.text