From d0341c9754ffd1beab981a3a76e2fdc8524dcca9 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Thu, 19 Oct 2023 18:50:02 +0200 Subject: [PATCH] Do not fail MQTT setup if images configured via yaml can't be validated (#102313) Add image --- .../components/mqtt/config_integration.py | 6 +---- homeassistant/components/mqtt/image.py | 25 +++++++------------ tests/components/mqtt/test_image.py | 6 ++--- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/mqtt/config_integration.py b/homeassistant/components/mqtt/config_integration.py index d0a5f10fc4b..291c1af3276 100644 --- a/homeassistant/components/mqtt/config_integration.py +++ b/homeassistant/components/mqtt/config_integration.py @@ -20,7 +20,6 @@ from . import ( cover as cover_platform, event as event_platform, humidifier as humidifier_platform, - image as image_platform, lawn_mower as lawn_mower_platform, lock as lock_platform, number as number_platform, @@ -74,10 +73,7 @@ CONFIG_SCHEMA_BASE = vol.Schema( cv.ensure_list, [humidifier_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] ), - Platform.IMAGE.value: vol.All( - cv.ensure_list, - [image_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] - ), + Platform.IMAGE.value: vol.All(cv.ensure_list, [dict]), Platform.LAWN_MOWER.value: vol.All( cv.ensure_list, [lawn_mower_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] diff --git a/homeassistant/components/mqtt/image.py b/homeassistant/components/mqtt/image.py index da526575a77..bf4ca584c47 100644 --- a/homeassistant/components/mqtt/image.py +++ b/homeassistant/components/mqtt/image.py @@ -4,7 +4,6 @@ from __future__ import annotations from base64 import b64decode import binascii from collections.abc import Callable -import functools import logging from typing import TYPE_CHECKING, Any @@ -27,7 +26,7 @@ from . import subscription from .config import MQTT_BASE_SCHEMA from .const import CONF_ENCODING, CONF_QOS from .debug_info import log_messages -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 MessageCallbackType, MqttValueTemplate, ReceiveMessage from .util import get_mqtt_data, valid_subscribe_topic @@ -79,21 +78,15 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up MQTT image 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, + MqttImage, + image.DOMAIN, + async_add_entities, + DISCOVERY_SCHEMA, + PLATFORM_SCHEMA_MODERN, ) - await async_setup_entry_helper(hass, image.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 Image.""" - async_add_entities([MqttImage(hass, config, config_entry, discovery_data)]) class MqttImage(MqttEntity, ImageEntity): diff --git a/tests/components/mqtt/test_image.py b/tests/components/mqtt/test_image.py index 621be984b7b..5ca9bbbc297 100644 --- a/tests/components/mqtt/test_image.py +++ b/tests/components/mqtt/test_image.py @@ -1,6 +1,5 @@ """The tests for mqtt image component.""" from base64 import b64encode -from contextlib import suppress from http import HTTPStatus import json import ssl @@ -504,7 +503,7 @@ async def test_image_from_url_fails( } } }, - "Invalid config for [mqtt]: Expected one of [`image_topic`, `url_topic`], got none", + "Expected one of [`image_topic`, `url_topic`], got none", ), ], ) @@ -516,8 +515,7 @@ async def test_image_config_fails( error_msg: str, ) -> None: """Test setup with minimum configuration.""" - with suppress(AssertionError): - await mqtt_mock_entry() + assert await mqtt_mock_entry() assert error_msg in caplog.text