Skip to content

Commit

Permalink
Do not fail MQTT setup if binary sensors configured via yaml can't be…
Browse files Browse the repository at this point in the history
… validated (home-assistant#102300)

Add binary_sensor
  • Loading branch information
jbouwh authored Oct 19, 2023
1 parent d149bff commit 651b725
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
27 changes: 10 additions & 17 deletions homeassistant/components/mqtt/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

from datetime import datetime, timedelta
import functools
import logging
from typing import Any

Expand Down Expand Up @@ -31,7 +30,7 @@
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
Expand All @@ -42,7 +41,7 @@
MQTT_ENTITY_COMMON_SCHEMA,
MqttAvailability,
MqttEntity,
async_setup_entry_helper,
async_mqtt_entry_helper,
write_state_on_attr_change,
)
from .models import MqttValueTemplate, ReceiveMessage
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 1 addition & 5 deletions homeassistant/components/mqtt/config_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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,
Expand Down Expand Up @@ -55,10 +54,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,
[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]
Expand Down
5 changes: 2 additions & 3 deletions tests/components/mqtt/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 651b725

Please sign in to comment.