Do not fail MQTT setup if humidifiers configured via yaml can't be validated (#102312)

Add humidifier
This commit is contained in:
Jan Bouwhuis 2023-10-19 23:29:49 +02:00 committed by GitHub
parent 6baa8082d5
commit f4e7c5aed3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 54 deletions

View file

@ -18,7 +18,6 @@ from . import (
button as button_platform, button as button_platform,
cover as cover_platform, cover as cover_platform,
event as event_platform, event as event_platform,
humidifier as humidifier_platform,
lawn_mower as lawn_mower_platform, lawn_mower as lawn_mower_platform,
lock as lock_platform, lock as lock_platform,
number as number_platform, number as number_platform,
@ -60,10 +59,7 @@ CONFIG_SCHEMA_BASE = vol.Schema(
[event_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type] [event_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
), ),
Platform.FAN.value: vol.All(cv.ensure_list, [dict]), Platform.FAN.value: vol.All(cv.ensure_list, [dict]),
Platform.HUMIDIFIER.value: vol.All( Platform.HUMIDIFIER.value: vol.All(cv.ensure_list, [dict]),
cv.ensure_list,
[humidifier_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
),
Platform.IMAGE.value: vol.All(cv.ensure_list, [dict]), Platform.IMAGE.value: vol.All(cv.ensure_list, [dict]),
Platform.LAWN_MOWER.value: vol.All( Platform.LAWN_MOWER.value: vol.All(
cv.ensure_list, cv.ensure_list,

View file

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
import functools
import logging import logging
from typing import Any from typing import Any
@ -33,7 +32,7 @@ from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.template import Template from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType
from . import subscription from . import subscription
from .config import MQTT_RW_SCHEMA from .config import MQTT_RW_SCHEMA
@ -55,7 +54,7 @@ from .debug_info import log_messages
from .mixins import ( from .mixins import (
MQTT_ENTITY_COMMON_SCHEMA, MQTT_ENTITY_COMMON_SCHEMA,
MqttEntity, MqttEntity,
async_setup_entry_helper, async_mqtt_entry_helper,
write_state_on_attr_change, write_state_on_attr_change,
) )
from .models import ( from .models import (
@ -192,21 +191,15 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up MQTT humidifier through YAML and through MQTT discovery.""" """Set up MQTT humidifier 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,
MqttHumidifier,
humidifier.DOMAIN,
async_add_entities,
DISCOVERY_SCHEMA,
PLATFORM_SCHEMA_MODERN,
) )
await async_setup_entry_helper(hass, humidifier.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 humidifier."""
async_add_entities([MqttHumidifier(hass, config, config_entry, discovery_data)])
class MqttHumidifier(MqttEntity, HumidifierEntity): class MqttHumidifier(MqttEntity, HumidifierEntity):

View file

@ -142,9 +142,8 @@ async def test_fail_setup_if_no_command_topic(
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test if command fails with command topic.""" """Test if command fails with command topic."""
with pytest.raises(AssertionError): assert await mqtt_mock_entry()
await mqtt_mock_entry() assert "required key not provided" in caplog.text
assert "Invalid config for [mqtt]: required key not provided" in caplog.text
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -934,11 +933,11 @@ async def test_attributes(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("hass_config", "valid"), ("hass_config", "valid"),
[ [
( ( # test valid case 1
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
humidifier.DOMAIN: { humidifier.DOMAIN: {
"name": "test_valid_1", "name": "test",
"command_topic": "command-topic", "command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic", "target_humidity_command_topic": "humidity-command-topic",
} }
@ -946,11 +945,11 @@ async def test_attributes(
}, },
True, True,
), ),
( ( # test valid case 2
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
humidifier.DOMAIN: { humidifier.DOMAIN: {
"name": "test_valid_2", "name": "test",
"command_topic": "command-topic", "command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic", "target_humidity_command_topic": "humidity-command-topic",
"device_class": "humidifier", "device_class": "humidifier",
@ -959,11 +958,11 @@ async def test_attributes(
}, },
True, True,
), ),
( ( # test valid case 3
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
humidifier.DOMAIN: { humidifier.DOMAIN: {
"name": "test_valid_3", "name": "test",
"command_topic": "command-topic", "command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic", "target_humidity_command_topic": "humidity-command-topic",
"device_class": "dehumidifier", "device_class": "dehumidifier",
@ -972,11 +971,11 @@ async def test_attributes(
}, },
True, True,
), ),
( ( # test valid case 4
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
humidifier.DOMAIN: { humidifier.DOMAIN: {
"name": "test_valid_4", "name": "test",
"command_topic": "command-topic", "command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic", "target_humidity_command_topic": "humidity-command-topic",
"device_class": None, "device_class": None,
@ -985,11 +984,11 @@ async def test_attributes(
}, },
True, True,
), ),
( ( # test invalid device_class
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
humidifier.DOMAIN: { humidifier.DOMAIN: {
"name": "test_invalid_device_class", "name": "test",
"command_topic": "command-topic", "command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic", "target_humidity_command_topic": "humidity-command-topic",
"device_class": "notsupporedSpeci@l", "device_class": "notsupporedSpeci@l",
@ -998,11 +997,11 @@ async def test_attributes(
}, },
False, False,
), ),
( ( # test mode_command_topic without modes
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
humidifier.DOMAIN: { humidifier.DOMAIN: {
"name": "test_mode_command_without_modes", "name": "test",
"command_topic": "command-topic", "command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic", "target_humidity_command_topic": "humidity-command-topic",
"mode_command_topic": "mode-command-topic", "mode_command_topic": "mode-command-topic",
@ -1011,11 +1010,11 @@ async def test_attributes(
}, },
False, False,
), ),
( ( # test invalid humidity min max case 1
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
humidifier.DOMAIN: { humidifier.DOMAIN: {
"name": "test_invalid_humidity_min_max_1", "name": "test",
"command_topic": "command-topic", "command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic", "target_humidity_command_topic": "humidity-command-topic",
"min_humidity": 0, "min_humidity": 0,
@ -1025,11 +1024,11 @@ async def test_attributes(
}, },
False, False,
), ),
( ( # test invalid humidity min max case 2
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
humidifier.DOMAIN: { humidifier.DOMAIN: {
"name": "test_invalid_humidity_min_max_2", "name": "test",
"command_topic": "command-topic", "command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic", "target_humidity_command_topic": "humidity-command-topic",
"max_humidity": 20, "max_humidity": 20,
@ -1039,11 +1038,11 @@ async def test_attributes(
}, },
False, False,
), ),
( ( # test invalid mode, is reset payload
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
humidifier.DOMAIN: { humidifier.DOMAIN: {
"name": "test_invalid_mode_is_reset", "name": "test",
"command_topic": "command-topic", "command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic", "target_humidity_command_topic": "humidity-command-topic",
"mode_command_topic": "mode-command-topic", "mode_command_topic": "mode-command-topic",
@ -1061,11 +1060,9 @@ async def test_validity_configurations(
valid: bool, valid: bool,
) -> None: ) -> None:
"""Test validity of configurations.""" """Test validity of configurations."""
if valid: await mqtt_mock_entry()
await mqtt_mock_entry() state = hass.states.get("humidifier.test")
return assert (state is not None) == valid
with pytest.raises(AssertionError):
await mqtt_mock_entry()
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -1167,14 +1164,11 @@ async def test_supported_features(
features: humidifier.HumidifierEntityFeature | None, features: humidifier.HumidifierEntityFeature | None,
) -> None: ) -> None:
"""Test supported features.""" """Test supported features."""
await mqtt_mock_entry()
state = hass.states.get(f"humidifier.{name}")
assert (state is not None) == success
if success: if success:
await mqtt_mock_entry()
state = hass.states.get(f"humidifier.{name}")
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == features assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == features
return
with pytest.raises(AssertionError):
await mqtt_mock_entry()
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG]) @pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])