mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 16:36:08 +01:00
483 lines
16 KiB
Python
483 lines
16 KiB
Python
"""Test illuminance trigger."""
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
|
|
from homeassistant.components.number import NumberDeviceClass
|
|
from homeassistant.components.sensor import SensorDeviceClass
|
|
from homeassistant.const import (
|
|
ATTR_DEVICE_CLASS,
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
|
LIGHT_LUX,
|
|
STATE_OFF,
|
|
STATE_ON,
|
|
)
|
|
from homeassistant.core import HomeAssistant, ServiceCall
|
|
|
|
from tests.components.common import (
|
|
TriggerStateDescription,
|
|
assert_trigger_behavior_any,
|
|
assert_trigger_behavior_first,
|
|
assert_trigger_behavior_last,
|
|
assert_trigger_gated_by_labs_flag,
|
|
parametrize_numerical_state_value_changed_trigger_states,
|
|
parametrize_numerical_state_value_crossed_threshold_trigger_states,
|
|
parametrize_target_entities,
|
|
parametrize_trigger_states,
|
|
target_entities,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
async def target_binary_sensors(hass: HomeAssistant) -> dict[str, list[str]]:
|
|
"""Create multiple binary sensor entities associated with different targets."""
|
|
return await target_entities(hass, "binary_sensor")
|
|
|
|
|
|
@pytest.fixture
|
|
async def target_numbers(hass: HomeAssistant) -> dict[str, list[str]]:
|
|
"""Create multiple number entities associated with different targets."""
|
|
return await target_entities(hass, "number")
|
|
|
|
|
|
@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.mark.parametrize(
|
|
"trigger_key",
|
|
[
|
|
"illuminance.detected",
|
|
"illuminance.cleared",
|
|
"illuminance.changed",
|
|
"illuminance.crossed_threshold",
|
|
],
|
|
)
|
|
async def test_illuminance_triggers_gated_by_labs_flag(
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, trigger_key: str
|
|
) -> None:
|
|
"""Test the illuminance triggers are gated by the labs flag."""
|
|
await assert_trigger_gated_by_labs_flag(hass, caplog, trigger_key)
|
|
|
|
|
|
# --- Binary sensor detected/cleared tests ---
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("binary_sensor"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_trigger_states(
|
|
trigger="illuminance.detected",
|
|
target_states=[STATE_ON],
|
|
other_states=[STATE_OFF],
|
|
required_filter_attributes={
|
|
ATTR_DEVICE_CLASS: BinarySensorDeviceClass.LIGHT
|
|
},
|
|
trigger_from_none=False,
|
|
),
|
|
*parametrize_trigger_states(
|
|
trigger="illuminance.cleared",
|
|
target_states=[STATE_OFF],
|
|
other_states=[STATE_ON],
|
|
required_filter_attributes={
|
|
ATTR_DEVICE_CLASS: BinarySensorDeviceClass.LIGHT
|
|
},
|
|
trigger_from_none=False,
|
|
),
|
|
],
|
|
)
|
|
async def test_illuminance_trigger_binary_sensor_behavior_any(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
target_binary_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 illuminance trigger fires for binary_sensor entities with device_class light."""
|
|
await assert_trigger_behavior_any(
|
|
hass,
|
|
service_calls=service_calls,
|
|
target_entities=target_binary_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("binary_sensor"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_trigger_states(
|
|
trigger="illuminance.detected",
|
|
target_states=[STATE_ON],
|
|
other_states=[STATE_OFF],
|
|
required_filter_attributes={
|
|
ATTR_DEVICE_CLASS: BinarySensorDeviceClass.LIGHT
|
|
},
|
|
trigger_from_none=False,
|
|
),
|
|
*parametrize_trigger_states(
|
|
trigger="illuminance.cleared",
|
|
target_states=[STATE_OFF],
|
|
other_states=[STATE_ON],
|
|
required_filter_attributes={
|
|
ATTR_DEVICE_CLASS: BinarySensorDeviceClass.LIGHT
|
|
},
|
|
trigger_from_none=False,
|
|
),
|
|
],
|
|
)
|
|
async def test_illuminance_trigger_binary_sensor_behavior_first(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
target_binary_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 illuminance trigger fires on the first binary_sensor state change."""
|
|
await assert_trigger_behavior_first(
|
|
hass,
|
|
service_calls=service_calls,
|
|
target_entities=target_binary_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("binary_sensor"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_trigger_states(
|
|
trigger="illuminance.detected",
|
|
target_states=[STATE_ON],
|
|
other_states=[STATE_OFF],
|
|
required_filter_attributes={
|
|
ATTR_DEVICE_CLASS: BinarySensorDeviceClass.LIGHT
|
|
},
|
|
trigger_from_none=False,
|
|
),
|
|
*parametrize_trigger_states(
|
|
trigger="illuminance.cleared",
|
|
target_states=[STATE_OFF],
|
|
other_states=[STATE_ON],
|
|
required_filter_attributes={
|
|
ATTR_DEVICE_CLASS: BinarySensorDeviceClass.LIGHT
|
|
},
|
|
trigger_from_none=False,
|
|
),
|
|
],
|
|
)
|
|
async def test_illuminance_trigger_binary_sensor_behavior_last(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
target_binary_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 illuminance trigger fires when the last binary_sensor changes state."""
|
|
await assert_trigger_behavior_last(
|
|
hass,
|
|
service_calls=service_calls,
|
|
target_entities=target_binary_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,
|
|
)
|
|
|
|
|
|
# --- Sensor changed/crossed_threshold tests ---
|
|
|
|
|
|
@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(
|
|
"illuminance.changed",
|
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: LIGHT_LUX},
|
|
),
|
|
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
|
"illuminance.crossed_threshold",
|
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: LIGHT_LUX},
|
|
),
|
|
],
|
|
)
|
|
async def test_illuminance_trigger_sensor_behavior_any(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
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 illuminance trigger fires for sensor entities with device_class illuminance."""
|
|
await assert_trigger_behavior_any(
|
|
hass,
|
|
service_calls=service_calls,
|
|
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(
|
|
"illuminance.crossed_threshold",
|
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: LIGHT_LUX},
|
|
),
|
|
],
|
|
)
|
|
async def test_illuminance_trigger_sensor_crossed_threshold_behavior_first(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
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 illuminance crossed_threshold trigger fires on the first sensor state change."""
|
|
await assert_trigger_behavior_first(
|
|
hass,
|
|
service_calls=service_calls,
|
|
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(
|
|
"illuminance.crossed_threshold",
|
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: LIGHT_LUX},
|
|
),
|
|
],
|
|
)
|
|
async def test_illuminance_trigger_sensor_crossed_threshold_behavior_last(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
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 illuminance crossed_threshold trigger fires when the last sensor changes state."""
|
|
await assert_trigger_behavior_last(
|
|
hass,
|
|
service_calls=service_calls,
|
|
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,
|
|
)
|
|
|
|
|
|
# --- Number changed/crossed_threshold tests ---
|
|
|
|
|
|
@pytest.mark.usefixtures("enable_labs_preview_features")
|
|
@pytest.mark.parametrize(
|
|
("trigger_target_config", "entity_id", "entities_in_target"),
|
|
parametrize_target_entities("number"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_state_value_changed_trigger_states(
|
|
"illuminance.changed",
|
|
device_class=NumberDeviceClass.ILLUMINANCE,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: LIGHT_LUX},
|
|
),
|
|
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
|
"illuminance.crossed_threshold",
|
|
device_class=NumberDeviceClass.ILLUMINANCE,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: LIGHT_LUX},
|
|
),
|
|
],
|
|
)
|
|
async def test_illuminance_trigger_number_behavior_any(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
target_numbers: 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 illuminance trigger fires for number entities with device_class illuminance."""
|
|
await assert_trigger_behavior_any(
|
|
hass,
|
|
service_calls=service_calls,
|
|
target_entities=target_numbers,
|
|
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("number"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
|
"illuminance.crossed_threshold",
|
|
device_class=NumberDeviceClass.ILLUMINANCE,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: LIGHT_LUX},
|
|
),
|
|
],
|
|
)
|
|
async def test_illuminance_trigger_number_crossed_threshold_behavior_first(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
target_numbers: 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 illuminance crossed_threshold trigger fires on the first number state change."""
|
|
await assert_trigger_behavior_first(
|
|
hass,
|
|
service_calls=service_calls,
|
|
target_entities=target_numbers,
|
|
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("number"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("trigger", "trigger_options", "states"),
|
|
[
|
|
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
|
"illuminance.crossed_threshold",
|
|
device_class=NumberDeviceClass.ILLUMINANCE,
|
|
unit_attributes={ATTR_UNIT_OF_MEASUREMENT: LIGHT_LUX},
|
|
),
|
|
],
|
|
)
|
|
async def test_illuminance_trigger_number_crossed_threshold_behavior_last(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
target_numbers: 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 illuminance crossed_threshold trigger fires when the last number changes state."""
|
|
await assert_trigger_behavior_last(
|
|
hass,
|
|
service_calls=service_calls,
|
|
target_entities=target_numbers,
|
|
trigger_target_config=trigger_target_config,
|
|
entity_id=entity_id,
|
|
entities_in_target=entities_in_target,
|
|
trigger=trigger,
|
|
trigger_options=trigger_options,
|
|
states=states,
|
|
)
|