Do not fail MQTT setup if text's configured via yaml can't be validated (#102322)

Add text
pull/102318/head
Jan Bouwhuis 2023-10-19 18:51:47 +02:00 committed by GitHub
parent 1456809f6a
commit 5eb0a33795
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 28 deletions

View File

@ -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]

View File

@ -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):

View File

@ -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(