Do not fail MQTT setup if binary sensors configured via yaml can't be validated (#102300)

Add binary_sensor
This commit is contained in:
Jan Bouwhuis 2023-10-19 18:15:21 +02:00 committed by GitHub
parent d149bffb07
commit 651b725cc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 25 deletions

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
from datetime import datetime, timedelta from datetime import datetime, timedelta
import functools
import logging import logging
from typing import Any from typing import Any
@ -31,7 +30,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.helpers.event as evt import homeassistant.helpers.event as evt
from homeassistant.helpers.event import async_call_later from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from . import subscription from . import subscription
@ -42,7 +41,7 @@ from .mixins import (
MQTT_ENTITY_COMMON_SCHEMA, MQTT_ENTITY_COMMON_SCHEMA,
MqttAvailability, MqttAvailability,
MqttEntity, MqttEntity,
async_setup_entry_helper, async_mqtt_entry_helper,
write_state_on_attr_change, write_state_on_attr_change,
) )
from .models import MqttValueTemplate, ReceiveMessage from .models import MqttValueTemplate, ReceiveMessage
@ -77,21 +76,15 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up MQTT binary sensor through YAML and through MQTT discovery.""" """Set up MQTT binary sensor through YAML and through MQTT discovery."""
setup = functools.partial( await async_mqtt_entry_helper(
_async_setup_entity, hass, async_add_entities, config_entry=config_entry hass,
config_entry,
MqttBinarySensor,
binary_sensor.DOMAIN,
async_add_entities,
DISCOVERY_SCHEMA,
PLATFORM_SCHEMA_MODERN,
) )
await async_setup_entry_helper(hass, binary_sensor.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 binary sensor."""
async_add_entities([MqttBinarySensor(hass, config, config_entry, discovery_data)])
class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity): class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):

View File

@ -15,7 +15,6 @@ from homeassistant.const import (
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from . import ( from . import (
binary_sensor as binary_sensor_platform,
button as button_platform, button as button_platform,
camera as camera_platform, camera as camera_platform,
climate as climate_platform, climate as climate_platform,
@ -55,10 +54,7 @@ DEFAULT_TLS_PROTOCOL = "auto"
CONFIG_SCHEMA_BASE = vol.Schema( CONFIG_SCHEMA_BASE = vol.Schema(
{ {
Platform.ALARM_CONTROL_PANEL.value: vol.All(cv.ensure_list, [dict]), Platform.ALARM_CONTROL_PANEL.value: vol.All(cv.ensure_list, [dict]),
Platform.BINARY_SENSOR.value: vol.All( Platform.BINARY_SENSOR.value: vol.All(cv.ensure_list, [dict]),
cv.ensure_list,
[binary_sensor_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
),
Platform.BUTTON.value: vol.All( Platform.BUTTON.value: vol.All(
cv.ensure_list, cv.ensure_list,
[button_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] [button_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]

View File

@ -580,9 +580,8 @@ async def test_invalid_device_class(
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test the setting of an invalid sensor class.""" """Test the setting of an invalid sensor class."""
with pytest.raises(AssertionError): assert await mqtt_mock_entry()
await mqtt_mock_entry() assert "expected BinarySensorDeviceClass" in caplog.text
assert "Invalid config for [mqtt]: expected BinarySensorDeviceClass" in caplog.text
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG]) @pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])