mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 16:36:08 +01:00
610 lines
19 KiB
Python
610 lines
19 KiB
Python
"""Test humidity trigger."""
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from homeassistant.components.climate import (
|
|
ATTR_CURRENT_HUMIDITY as CLIMATE_ATTR_CURRENT_HUMIDITY,
|
|
HVACMode,
|
|
)
|
|
from homeassistant.components.humidifier import (
|
|
ATTR_CURRENT_HUMIDITY as HUMIDIFIER_ATTR_CURRENT_HUMIDITY,
|
|
)
|
|
from homeassistant.components.sensor import SensorDeviceClass
|
|
from homeassistant.components.weather import ATTR_WEATHER_HUMIDITY
|
|
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, STATE_ON
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from tests.components.common import (
|
|
TriggerStateDescription,
|
|
assert_trigger_behavior_any,
|
|
assert_trigger_behavior_first,
|
|
assert_trigger_behavior_last,
|
|
assert_trigger_gated_by_labs_flag,
|
|
assert_trigger_ignores_limit_entities_with_wrong_unit,
|
|
parametrize_numerical_attribute_changed_trigger_states,
|
|
parametrize_numerical_attribute_crossed_threshold_trigger_states,
|
|
parametrize_numerical_state_value_changed_trigger_states,
|
|
parametrize_numerical_state_value_crossed_threshold_trigger_states,
|
|
parametrize_target_entities,
|
|
target_entities,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
async def target_sensors(hass: HomeAssistant) -> dict[str, list[str]]:
|
|
"""Create multiple sensor entities associated with different targets."""
|
|
return await target_entities(hass, "sensor")
|
|
|
|
|
|
@pytest.fixture
|
|
async def target_climates(hass: HomeAssistant) -> dict[str, list[str]]:
|
|
"""Create multiple climate entities associated with different targets."""
|
|
return await target_entities(hass, "climate")
|
|
|
|
|
|
@pytest.fixture
|
|
async def target_humidifiers(hass: HomeAssistant) -> dict[str, list[str]]:
|
|
"""Create multiple humidifier entities associated with different targets."""
|
|
return await target_entities(hass, "humidifier")
|
|
|
|
|
|
@pytest.fixture
|
|
async def target_weathers(hass: HomeAssistant) -> dict[str, list[str]]:
|
|
"""Create multiple weather entities associated with different targets."""
|
|
return await target_entities(hass, "weather")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"trigger_key",
|
|
[
|
|
"humidity.changed",
|
|
"humidity.crossed_threshold",
|
|
],
|
|
)
|
|
async def test_humidity_triggers_gated_by_labs_flag(
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, trigger_key: str
|
|
) -> None:
|
|
"""Test the humidity triggers are gated by the labs flag."""
|
|
await assert_trigger_gated_by_labs_flag(hass, caplog, trigger_key)
|
|
|
|
|
|
# --- Sensor domain tests (value in state.state) ---
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("sensor"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_state_value_changed_trigger_states(
|
|
"humidity.changed",
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: "%"},
|
|
),
|
|
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: "%"},
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_sensor_behavior_any(
|
|
hass: HomeAssistant,
|
|
target_sensors: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity trigger fires for sensor entities with device_class humidity."""
|
|
await assert_trigger_behavior_any(
|
|
hass,
|
|
target_entities=target_sensors,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("sensor"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: "%"},
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_sensor_crossed_threshold_behavior_first(
|
|
hass: HomeAssistant,
|
|
target_sensors: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity crossed_threshold trigger fires on the first sensor state change."""
|
|
await assert_trigger_behavior_first(
|
|
hass,
|
|
target_entities=target_sensors,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("sensor"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: "%"},
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_sensor_crossed_threshold_behavior_last(
|
|
hass: HomeAssistant,
|
|
target_sensors: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity crossed_threshold trigger fires when the last sensor changes state."""
|
|
await assert_trigger_behavior_last(
|
|
hass,
|
|
target_entities=target_sensors,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
# --- Climate domain tests (value in current_humidity attribute) ---
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("climate"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_attribute_changed_trigger_states(
|
|
"humidity.changed", HVACMode.AUTO, CLIMATE_ATTR_CURRENT_HUMIDITY
|
|
),
|
|
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
HVACMode.AUTO,
|
|
CLIMATE_ATTR_CURRENT_HUMIDITY,
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_climate_behavior_any(
|
|
hass: HomeAssistant,
|
|
target_climates: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity trigger fires for climate entities."""
|
|
await assert_trigger_behavior_any(
|
|
hass,
|
|
target_entities=target_climates,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("climate"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
HVACMode.AUTO,
|
|
CLIMATE_ATTR_CURRENT_HUMIDITY,
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_climate_crossed_threshold_behavior_first(
|
|
hass: HomeAssistant,
|
|
target_climates: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity crossed_threshold trigger fires on the first climate state change."""
|
|
await assert_trigger_behavior_first(
|
|
hass,
|
|
target_entities=target_climates,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("climate"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
HVACMode.AUTO,
|
|
CLIMATE_ATTR_CURRENT_HUMIDITY,
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_climate_crossed_threshold_behavior_last(
|
|
hass: HomeAssistant,
|
|
target_climates: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity crossed_threshold trigger fires when the last climate changes state."""
|
|
await assert_trigger_behavior_last(
|
|
hass,
|
|
target_entities=target_climates,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
# --- Humidifier domain tests (value in current_humidity attribute) ---
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("humidifier"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_attribute_changed_trigger_states(
|
|
"humidity.changed", STATE_ON, HUMIDIFIER_ATTR_CURRENT_HUMIDITY
|
|
),
|
|
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
STATE_ON,
|
|
HUMIDIFIER_ATTR_CURRENT_HUMIDITY,
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_humidifier_behavior_any(
|
|
hass: HomeAssistant,
|
|
target_humidifiers: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity trigger fires for humidifier entities."""
|
|
await assert_trigger_behavior_any(
|
|
hass,
|
|
target_entities=target_humidifiers,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("humidifier"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
STATE_ON,
|
|
HUMIDIFIER_ATTR_CURRENT_HUMIDITY,
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_humidifier_crossed_threshold_behavior_first(
|
|
hass: HomeAssistant,
|
|
target_humidifiers: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity crossed_threshold trigger fires on the first humidifier state change."""
|
|
await assert_trigger_behavior_first(
|
|
hass,
|
|
target_entities=target_humidifiers,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("humidifier"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
STATE_ON,
|
|
HUMIDIFIER_ATTR_CURRENT_HUMIDITY,
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_humidifier_crossed_threshold_behavior_last(
|
|
hass: HomeAssistant,
|
|
target_humidifiers: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity crossed_threshold trigger fires when the last humidifier changes state."""
|
|
await assert_trigger_behavior_last(
|
|
hass,
|
|
target_entities=target_humidifiers,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
# --- Weather domain tests (value in humidity attribute) ---
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("weather"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_attribute_changed_trigger_states(
|
|
"humidity.changed", "sunny", ATTR_WEATHER_HUMIDITY
|
|
),
|
|
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
"sunny",
|
|
ATTR_WEATHER_HUMIDITY,
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_weather_behavior_any(
|
|
hass: HomeAssistant,
|
|
target_weathers: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity trigger fires for weather entities."""
|
|
await assert_trigger_behavior_any(
|
|
hass,
|
|
target_entities=target_weathers,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("weather"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
"sunny",
|
|
ATTR_WEATHER_HUMIDITY,
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_weather_crossed_threshold_behavior_first(
|
|
hass: HomeAssistant,
|
|
target_weathers: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity crossed_threshold trigger fires on the first weather state change."""
|
|
await assert_trigger_behavior_first(
|
|
hass,
|
|
target_entities=target_weathers,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("weather"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
|
"humidity.crossed_threshold",
|
|
"sunny",
|
|
ATTR_WEATHER_HUMIDITY,
|
|
),
|
|
],
|
|
)
|
|
async def test_humidity_trigger_weather_crossed_threshold_behavior_last(
|
|
hass: HomeAssistant,
|
|
target_weathers: dict[str, list[str]],
|
|
trigger_target_config: dict,
|
|
entity_id: str,
|
|
entities_in_target: int,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
states: list[TriggerStateDescription],
|
|
) -> None:
|
|
"""Test humidity crossed_threshold trigger fires when the last weather changes state."""
|
|
await assert_trigger_behavior_last(
|
|
hass,
|
|
target_entities=target_weathers,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "limit_entities"),
|
|
[
|
|
(
|
|
"humidity.changed",
|
|
{
|
|
"threshold": {
|
|
"type": "between",
|
|
"value_min": {"entity": "sensor.humidity_above"},
|
|
"value_max": {"entity": "sensor.humidity_below"},
|
|
},
|
|
},
|
|
["sensor.humidity_above", "sensor.humidity_below"],
|
|
),
|
|
(
|
|
"humidity.crossed_threshold",
|
|
{
|
|
"threshold": {
|
|
"type": "between",
|
|
"value_min": {"entity": "sensor.humidity_lower"},
|
|
"value_max": {"entity": "sensor.humidity_upper"},
|
|
},
|
|
},
|
|
["sensor.humidity_lower", "sensor.humidity_upper"],
|
|
),
|
|
],
|
|
)
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
async def test_humidity_trigger_ignores_limit_entity_with_wrong_unit(
|
|
hass: HomeAssistant,
|
|
trigger: str,
|
|
trigger_options: dict[str, Any],
|
|
limit_entities: list[str],
|
|
) -> None:
|
|
"""Test humidity triggers do not fire if limit entity unit is not %."""
|
|
await assert_trigger_ignores_limit_entities_with_wrong_unit(
|
|
hass,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
entity_id="climate.test_climate",
|
|
reset_state={
|
|
"state": HVACMode.AUTO,
|
|
"attributes": {CLIMATE_ATTR_CURRENT_HUMIDITY: 0},
|
|
},
|
|
trigger_state={
|
|
"state": HVACMode.AUTO,
|
|
"attributes": {CLIMATE_ATTR_CURRENT_HUMIDITY: 50},
|
|
},
|
|
limit_entities=[
|
|
(limit_entities[0], "10"),
|
|
(limit_entities[1], "90"),
|
|
],
|
|
correct_unit="%",
|
|
wrong_unit="g/m³",
|
|
)
|