Skip to content

Commit

Permalink
Do not fail MQTT setup if humidifiers configured via yaml can't be va…
Browse files Browse the repository at this point in the history
…lidated (home-assistant#102312)

Add humidifier
  • Loading branch information
jbouwh authored Oct 19, 2023
1 parent 6baa808 commit f4e7c5a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 54 deletions.
6 changes: 1 addition & 5 deletions homeassistant/components/mqtt/config_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
button as button_platform,
cover as cover_platform,
event as event_platform,
humidifier as humidifier_platform,
lawn_mower as lawn_mower_platform,
lock as lock_platform,
number as number_platform,
Expand Down Expand Up @@ -60,10 +59,7 @@
[event_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
),
Platform.FAN.value: vol.All(cv.ensure_list, [dict]),
Platform.HUMIDIFIER.value: vol.All(
cv.ensure_list,
[humidifier_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
),
Platform.HUMIDIFIER.value: vol.All(cv.ensure_list, [dict]),
Platform.IMAGE.value: vol.All(cv.ensure_list, [dict]),
Platform.LAWN_MOWER.value: vol.All(
cv.ensure_list,
Expand Down
27 changes: 10 additions & 17 deletions homeassistant/components/mqtt/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

from collections.abc import Callable
import functools
import logging
from typing import Any

Expand Down Expand Up @@ -33,7 +32,7 @@
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.typing import ConfigType

from . import subscription
from .config import MQTT_RW_SCHEMA
Expand All @@ -55,7 +54,7 @@
from .mixins import (
MQTT_ENTITY_COMMON_SCHEMA,
MqttEntity,
async_setup_entry_helper,
async_mqtt_entry_helper,
write_state_on_attr_change,
)
from .models import (
Expand Down Expand Up @@ -192,21 +191,15 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up MQTT humidifier 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,
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):
Expand Down
58 changes: 26 additions & 32 deletions tests/components/mqtt/test_humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ async def test_fail_setup_if_no_command_topic(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test if command fails with command topic."""
with pytest.raises(AssertionError):
await mqtt_mock_entry()
assert "Invalid config for [mqtt]: required key not provided" in caplog.text
assert await mqtt_mock_entry()
assert "required key not provided" in caplog.text


@pytest.mark.parametrize(
Expand Down Expand Up @@ -934,23 +933,23 @@ async def test_attributes(
@pytest.mark.parametrize(
("hass_config", "valid"),
[
(
( # test valid case 1
{
mqtt.DOMAIN: {
humidifier.DOMAIN: {
"name": "test_valid_1",
"name": "test",
"command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic",
}
}
},
True,
),
(
( # test valid case 2
{
mqtt.DOMAIN: {
humidifier.DOMAIN: {
"name": "test_valid_2",
"name": "test",
"command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic",
"device_class": "humidifier",
Expand All @@ -959,11 +958,11 @@ async def test_attributes(
},
True,
),
(
( # test valid case 3
{
mqtt.DOMAIN: {
humidifier.DOMAIN: {
"name": "test_valid_3",
"name": "test",
"command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic",
"device_class": "dehumidifier",
Expand All @@ -972,11 +971,11 @@ async def test_attributes(
},
True,
),
(
( # test valid case 4
{
mqtt.DOMAIN: {
humidifier.DOMAIN: {
"name": "test_valid_4",
"name": "test",
"command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic",
"device_class": None,
Expand All @@ -985,11 +984,11 @@ async def test_attributes(
},
True,
),
(
( # test invalid device_class
{
mqtt.DOMAIN: {
humidifier.DOMAIN: {
"name": "test_invalid_device_class",
"name": "test",
"command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic",
"device_class": "notsupporedSpeci@l",
Expand All @@ -998,11 +997,11 @@ async def test_attributes(
},
False,
),
(
( # test mode_command_topic without modes
{
mqtt.DOMAIN: {
humidifier.DOMAIN: {
"name": "test_mode_command_without_modes",
"name": "test",
"command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic",
"mode_command_topic": "mode-command-topic",
Expand All @@ -1011,11 +1010,11 @@ async def test_attributes(
},
False,
),
(
( # test invalid humidity min max case 1
{
mqtt.DOMAIN: {
humidifier.DOMAIN: {
"name": "test_invalid_humidity_min_max_1",
"name": "test",
"command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic",
"min_humidity": 0,
Expand All @@ -1025,11 +1024,11 @@ async def test_attributes(
},
False,
),
(
( # test invalid humidity min max case 2
{
mqtt.DOMAIN: {
humidifier.DOMAIN: {
"name": "test_invalid_humidity_min_max_2",
"name": "test",
"command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic",
"max_humidity": 20,
Expand All @@ -1039,11 +1038,11 @@ async def test_attributes(
},
False,
),
(
( # test invalid mode, is reset payload
{
mqtt.DOMAIN: {
humidifier.DOMAIN: {
"name": "test_invalid_mode_is_reset",
"name": "test",
"command_topic": "command-topic",
"target_humidity_command_topic": "humidity-command-topic",
"mode_command_topic": "mode-command-topic",
Expand All @@ -1061,11 +1060,9 @@ async def test_validity_configurations(
valid: bool,
) -> None:
"""Test validity of configurations."""
if valid:
await mqtt_mock_entry()
return
with pytest.raises(AssertionError):
await mqtt_mock_entry()
await mqtt_mock_entry()
state = hass.states.get("humidifier.test")
assert (state is not None) == valid


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1167,14 +1164,11 @@ async def test_supported_features(
features: humidifier.HumidifierEntityFeature | None,
) -> None:
"""Test supported features."""
await mqtt_mock_entry()
state = hass.states.get(f"humidifier.{name}")
assert (state is not None) == success
if success:
await mqtt_mock_entry()

state = hass.states.get(f"humidifier.{name}")
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == features
return
with pytest.raises(AssertionError):
await mqtt_mock_entry()


@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])
Expand Down

0 comments on commit f4e7c5a

Please sign in to comment.