From 04fdcbe5fafa951b6bab99b2d17450aa61182fd8 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Fri, 20 Oct 2023 08:13:59 +0200 Subject: [PATCH] Do not fail MQTT setup if buttons configured via yaml can't be validated (#102301) --- homeassistant/components/mqtt/button.py | 28 +++++++------------ .../components/mqtt/config_integration.py | 6 +--- tests/components/mqtt/test_button.py | 3 +- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/mqtt/button.py b/homeassistant/components/mqtt/button.py index 47ac12386f7..a6bc43bece2 100644 --- a/homeassistant/components/mqtt/button.py +++ b/homeassistant/components/mqtt/button.py @@ -1,8 +1,6 @@ """Support for MQTT buttons.""" from __future__ import annotations -import functools - import voluptuous as vol from homeassistant.components import button @@ -12,7 +10,7 @@ from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME from homeassistant.core import HomeAssistant import homeassistant.helpers.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 .config import DEFAULT_RETAIN, MQTT_BASE_SCHEMA from .const import ( @@ -22,7 +20,7 @@ from .const import ( CONF_QOS, CONF_RETAIN, ) -from .mixins import MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, async_setup_entry_helper +from .mixins import MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, async_mqtt_entry_helper from .models import MqttCommandTemplate from .util import valid_publish_topic @@ -50,21 +48,15 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up MQTT button 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, + MqttButton, + button.DOMAIN, + async_add_entities, + DISCOVERY_SCHEMA, + PLATFORM_SCHEMA_MODERN, ) - await async_setup_entry_helper(hass, button.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 button.""" - async_add_entities([MqttButton(hass, config, config_entry, discovery_data)]) class MqttButton(MqttEntity, ButtonEntity): diff --git a/homeassistant/components/mqtt/config_integration.py b/homeassistant/components/mqtt/config_integration.py index b72391a55ae..a6ed1dae54d 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 ( - button as button_platform, cover as cover_platform, event as event_platform, number as number_platform, @@ -41,10 +40,7 @@ CONFIG_SCHEMA_BASE = vol.Schema( { Platform.ALARM_CONTROL_PANEL.value: vol.All(cv.ensure_list, [dict]), Platform.BINARY_SENSOR.value: vol.All(cv.ensure_list, [dict]), - Platform.BUTTON.value: vol.All( - cv.ensure_list, - [button_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] - ), + 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( diff --git a/tests/components/mqtt/test_button.py b/tests/components/mqtt/test_button.py index 35b6561895d..f2f91c5ca75 100644 --- a/tests/components/mqtt/test_button.py +++ b/tests/components/mqtt/test_button.py @@ -456,8 +456,7 @@ async def test_invalid_device_class( caplog: pytest.LogCaptureFixture, ) -> None: """Test device_class option with invalid value.""" - with pytest.raises(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() assert "expected ButtonDeviceClass" in caplog.text