mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 22:37:11 +00:00
Do not fail MQTT setup if binary sensors configured via yaml can't be validated (#102300)
Add binary_sensor
This commit is contained in:
parent
d149bffb07
commit
651b725cc0
@ -2,7 +2,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
import functools
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
@ -31,7 +30,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
import homeassistant.helpers.event as evt
|
||||
from homeassistant.helpers.event import async_call_later
|
||||
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 . import subscription
|
||||
@ -42,7 +41,7 @@ from .mixins import (
|
||||
MQTT_ENTITY_COMMON_SCHEMA,
|
||||
MqttAvailability,
|
||||
MqttEntity,
|
||||
async_setup_entry_helper,
|
||||
async_mqtt_entry_helper,
|
||||
write_state_on_attr_change,
|
||||
)
|
||||
from .models import MqttValueTemplate, ReceiveMessage
|
||||
@ -77,21 +76,15 @@ async def async_setup_entry(
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up MQTT binary sensor 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,
|
||||
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):
|
||||
|
@ -15,7 +15,6 @@ from homeassistant.const import (
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from . import (
|
||||
binary_sensor as binary_sensor_platform,
|
||||
button as button_platform,
|
||||
camera as camera_platform,
|
||||
climate as climate_platform,
|
||||
@ -55,10 +54,7 @@ DEFAULT_TLS_PROTOCOL = "auto"
|
||||
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,
|
||||
[binary_sensor_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
||||
),
|
||||
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]
|
||||
|
@ -580,9 +580,8 @@ async def test_invalid_device_class(
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
) -> None:
|
||||
"""Test the setting of an invalid sensor class."""
|
||||
with pytest.raises(AssertionError):
|
||||
await mqtt_mock_entry()
|
||||
assert "Invalid config for [mqtt]: expected BinarySensorDeviceClass" in caplog.text
|
||||
assert await mqtt_mock_entry()
|
||||
assert "expected BinarySensorDeviceClass" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])
|
||||
|
Loading…
x
Reference in New Issue
Block a user