Skip to content

Commit

Permalink
Do not fail MQTT setup if climate's configured via yaml can't be vali…
Browse files Browse the repository at this point in the history
…dated (home-assistant#102303)

Add climate
  • Loading branch information
jbouwh authored Oct 19, 2023
1 parent b57af4e commit e26a259
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
29 changes: 11 additions & 18 deletions homeassistant/components/mqtt/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from abc import ABC, abstractmethod
from collections.abc import Callable
import functools
import logging
from typing import Any

Expand Down Expand Up @@ -47,7 +46,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.unit_conversion import TemperatureConverter

from . import subscription
Expand Down Expand Up @@ -85,7 +84,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 @@ -399,22 +398,16 @@ async def async_setup_entry(
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up MQTT climate device through YAML and through MQTT discovery."""
setup = functools.partial(
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
"""Set up MQTT climate through YAML and through MQTT discovery."""
await async_mqtt_entry_helper(
hass,
config_entry,
MqttClimate,
climate.DOMAIN,
async_add_entities,
DISCOVERY_SCHEMA,
PLATFORM_SCHEMA_MODERN,
)
await async_setup_entry_helper(hass, climate.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 climate devices."""
async_add_entities([MqttClimate(hass, config, config_entry, discovery_data)])


class MqttTemperatureControlEntity(MqttEntity, ABC):
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 @@ -16,7 +16,6 @@

from . import (
button as button_platform,
climate as climate_platform,
cover as cover_platform,
event as event_platform,
humidifier as humidifier_platform,
Expand Down Expand Up @@ -52,10 +51,7 @@
[button_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
),
Platform.CAMERA.value: vol.All(cv.ensure_list, [dict]),
Platform.CLIMATE.value: vol.All(
cv.ensure_list,
[climate_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
),
Platform.CLIMATE.value: vol.All(cv.ensure_list, [dict]),
Platform.COVER.value: vol.All(
cv.ensure_list,
[cover_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
Expand Down
33 changes: 15 additions & 18 deletions tests/components/mqtt/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ async def test_preset_none_in_preset_modes(
mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None:
"""Test the preset mode payload reset configuration."""
with pytest.raises(AssertionError):
await mqtt_mock_entry()
assert "Invalid config for [mqtt]: not a valid value" in caplog.text
assert await mqtt_mock_entry()
assert "not a valid value" in caplog.text


@pytest.mark.parametrize(
Expand Down Expand Up @@ -2448,59 +2447,59 @@ async def test_publishing_with_custom_encoding(
@pytest.mark.parametrize(
("hass_config", "valid"),
[
(
( # test_valid_humidity_min_max
{
mqtt.DOMAIN: {
climate.DOMAIN: {
"name": "test_valid_humidity_min_max",
"name": "test",
"min_humidity": 20,
"max_humidity": 80,
},
}
},
True,
),
(
( # test_invalid_humidity_min_max_1
{
mqtt.DOMAIN: {
climate.DOMAIN: {
"name": "test_invalid_humidity_min_max_1",
"name": "test",
"min_humidity": 0,
"max_humidity": 101,
},
}
},
False,
),
(
( # test_invalid_humidity_min_max_2
{
mqtt.DOMAIN: {
climate.DOMAIN: {
"name": "test_invalid_humidity_min_max_2",
"name": "test",
"max_humidity": 20,
"min_humidity": 40,
},
}
},
False,
),
(
( # test_valid_humidity_state
{
mqtt.DOMAIN: {
climate.DOMAIN: {
"name": "test_valid_humidity_state",
"name": "test",
"target_humidity_state_topic": "humidity-state",
"target_humidity_command_topic": "humidity-command",
},
}
},
True,
),
(
( # test_invalid_humidity_state
{
mqtt.DOMAIN: {
climate.DOMAIN: {
"name": "test_invalid_humidity_state",
"name": "test",
"target_humidity_state_topic": "humidity-state",
},
}
Expand All @@ -2515,11 +2514,9 @@ async def test_humidity_configuration_validity(
valid: bool,
) -> None:
"""Test the validity of humidity configurations."""
if valid:
await mqtt_mock_entry()
return
with pytest.raises(AssertionError):
await mqtt_mock_entry()
assert await mqtt_mock_entry()
state = hass.states.get("climate.test")
assert (state is not None) == valid


async def test_reloadable(
Expand Down

0 comments on commit e26a259

Please sign in to comment.