Do not fail MQTT setup if fans configured via yaml can't be validated (#102310)
Add fan
This commit is contained in:
parent
c408b60e4e
commit
3853214496
3 changed files with 39 additions and 33 deletions
|
@ -19,7 +19,6 @@ from . import (
|
||||||
climate as climate_platform,
|
climate as climate_platform,
|
||||||
cover as cover_platform,
|
cover as cover_platform,
|
||||||
event as event_platform,
|
event as event_platform,
|
||||||
fan as fan_platform,
|
|
||||||
humidifier as humidifier_platform,
|
humidifier as humidifier_platform,
|
||||||
image as image_platform,
|
image as image_platform,
|
||||||
lawn_mower as lawn_mower_platform,
|
lawn_mower as lawn_mower_platform,
|
||||||
|
@ -70,10 +69,7 @@ CONFIG_SCHEMA_BASE = vol.Schema(
|
||||||
cv.ensure_list,
|
cv.ensure_list,
|
||||||
[event_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
[event_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
||||||
),
|
),
|
||||||
Platform.FAN.value: vol.All(
|
Platform.FAN.value: vol.All(cv.ensure_list, [dict]),
|
||||||
cv.ensure_list,
|
|
||||||
[fan_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
|
||||||
),
|
|
||||||
Platform.HUMIDIFIER.value: vol.All(
|
Platform.HUMIDIFIER.value: vol.All(
|
||||||
cv.ensure_list,
|
cv.ensure_list,
|
||||||
[humidifier_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
[humidifier_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
||||||
|
|
|
@ -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
|
||||||
import math
|
import math
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
@ -30,7 +29,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 homeassistant.util.percentage import (
|
from homeassistant.util.percentage import (
|
||||||
int_states_in_range,
|
int_states_in_range,
|
||||||
percentage_to_ranged_value,
|
percentage_to_ranged_value,
|
||||||
|
@ -53,7 +52,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 (
|
||||||
|
@ -200,21 +199,15 @@ async def async_setup_entry(
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up MQTT fan through YAML and through MQTT discovery."""
|
"""Set up MQTT fan 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,
|
||||||
|
MqttFan,
|
||||||
|
fan.DOMAIN,
|
||||||
|
async_add_entities,
|
||||||
|
DISCOVERY_SCHEMA,
|
||||||
|
PLATFORM_SCHEMA_MODERN,
|
||||||
)
|
)
|
||||||
await async_setup_entry_helper(hass, fan.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 fan."""
|
|
||||||
async_add_entities([MqttFan(hass, config, config_entry, discovery_data)])
|
|
||||||
|
|
||||||
|
|
||||||
class MqttFan(MqttEntity, FanEntity):
|
class MqttFan(MqttEntity, FanEntity):
|
||||||
|
|
|
@ -96,9 +96,8 @@ async def test_fail_setup_if_no_command_topic(
|
||||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||||
) -> 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(
|
||||||
|
@ -1584,7 +1583,7 @@ async def test_attributes(
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("name", "hass_config", "success", "features"),
|
("name", "hass_config", "success", "features", "error_message"),
|
||||||
[
|
[
|
||||||
(
|
(
|
||||||
"test1",
|
"test1",
|
||||||
|
@ -1598,6 +1597,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature(0),
|
fan.FanEntityFeature(0),
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test2",
|
"test2",
|
||||||
|
@ -1612,6 +1612,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.OSCILLATE,
|
fan.FanEntityFeature.OSCILLATE,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test3",
|
"test3",
|
||||||
|
@ -1626,6 +1627,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.SET_SPEED,
|
fan.FanEntityFeature.SET_SPEED,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test4",
|
"test4",
|
||||||
|
@ -1640,6 +1642,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
False,
|
False,
|
||||||
None,
|
None,
|
||||||
|
"some but not all values in the same group of inclusion 'preset_modes'",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test5",
|
"test5",
|
||||||
|
@ -1655,6 +1658,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.PRESET_MODE,
|
fan.FanEntityFeature.PRESET_MODE,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test6",
|
"test6",
|
||||||
|
@ -1670,6 +1674,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.PRESET_MODE,
|
fan.FanEntityFeature.PRESET_MODE,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test7",
|
"test7",
|
||||||
|
@ -1684,6 +1689,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.SET_SPEED,
|
fan.FanEntityFeature.SET_SPEED,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test8",
|
"test8",
|
||||||
|
@ -1699,6 +1705,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.OSCILLATE | fan.FanEntityFeature.SET_SPEED,
|
fan.FanEntityFeature.OSCILLATE | fan.FanEntityFeature.SET_SPEED,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test9",
|
"test9",
|
||||||
|
@ -1714,6 +1721,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.PRESET_MODE,
|
fan.FanEntityFeature.PRESET_MODE,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test10",
|
"test10",
|
||||||
|
@ -1729,6 +1737,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.PRESET_MODE,
|
fan.FanEntityFeature.PRESET_MODE,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test11",
|
"test11",
|
||||||
|
@ -1745,6 +1754,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.PRESET_MODE | fan.FanEntityFeature.OSCILLATE,
|
fan.FanEntityFeature.PRESET_MODE | fan.FanEntityFeature.OSCILLATE,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test12",
|
"test12",
|
||||||
|
@ -1761,6 +1771,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.SET_SPEED,
|
fan.FanEntityFeature.SET_SPEED,
|
||||||
|
None,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test13",
|
"test13",
|
||||||
|
@ -1777,6 +1788,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
False,
|
False,
|
||||||
None,
|
None,
|
||||||
|
"not a valid value",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test14",
|
"test14",
|
||||||
|
@ -1793,13 +1805,14 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
False,
|
False,
|
||||||
None,
|
None,
|
||||||
|
"not a valid value",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test15",
|
"test15",
|
||||||
{
|
{
|
||||||
mqtt.DOMAIN: {
|
mqtt.DOMAIN: {
|
||||||
fan.DOMAIN: {
|
fan.DOMAIN: {
|
||||||
"name": "test7reset_payload_in_preset_modes_a",
|
"name": "test15",
|
||||||
"command_topic": "command-topic",
|
"command_topic": "command-topic",
|
||||||
"preset_mode_command_topic": "preset-mode-command-topic",
|
"preset_mode_command_topic": "preset-mode-command-topic",
|
||||||
"preset_modes": ["auto", "smart", "normal", "None"],
|
"preset_modes": ["auto", "smart", "normal", "None"],
|
||||||
|
@ -1808,6 +1821,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
False,
|
False,
|
||||||
None,
|
None,
|
||||||
|
"preset_modes must not contain payload_reset_preset_mode",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test16",
|
"test16",
|
||||||
|
@ -1824,6 +1838,7 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.PRESET_MODE,
|
fan.FanEntityFeature.PRESET_MODE,
|
||||||
|
"some error",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"test17",
|
"test17",
|
||||||
|
@ -1838,25 +1853,27 @@ async def test_attributes(
|
||||||
},
|
},
|
||||||
True,
|
True,
|
||||||
fan.FanEntityFeature.DIRECTION,
|
fan.FanEntityFeature.DIRECTION,
|
||||||
|
"some error",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_supported_features(
|
async def test_supported_features(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
name: str,
|
name: str,
|
||||||
success: bool,
|
success: bool,
|
||||||
features,
|
features: fan.FanEntityFeature | None,
|
||||||
|
error_message: str | None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test optimistic mode without state topic."""
|
"""Test optimistic mode without state topic."""
|
||||||
|
await mqtt_mock_entry()
|
||||||
|
state = hass.states.get(f"fan.{name}")
|
||||||
|
assert (state is not None) == success
|
||||||
if success:
|
if success:
|
||||||
await mqtt_mock_entry()
|
|
||||||
|
|
||||||
state = hass.states.get(f"fan.{name}")
|
|
||||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == features
|
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == features
|
||||||
return
|
return
|
||||||
with pytest.raises(AssertionError):
|
assert error_message in caplog.text
|
||||||
await mqtt_mock_entry()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])
|
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue