mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 01:21:51 +02:00
Add test
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
from freezegun import freeze_time
|
||||
import pytest
|
||||
@ -26,9 +26,12 @@ from homeassistant.helpers import (
|
||||
trace,
|
||||
)
|
||||
from homeassistant.helpers.template import Template
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import MockModule, mock_integration, mock_platform
|
||||
|
||||
|
||||
def assert_element(trace_element, expected_element, path):
|
||||
"""Assert a trace element is as expected.
|
||||
@ -2262,6 +2265,69 @@ async def test_platform_async_get_conditions(hass: HomeAssistant) -> None:
|
||||
device_automation_async_get_conditions_mock.assert_awaited()
|
||||
|
||||
|
||||
async def test_platform_multiple_conditions(hass: HomeAssistant) -> None:
|
||||
"""Test a condition platform with multiple conditions."""
|
||||
|
||||
class MockCondition(condition.Condition):
|
||||
"""Mock condition."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, config: ConfigType) -> None:
|
||||
"""Initialize condition."""
|
||||
|
||||
@classmethod
|
||||
async def async_validate_condition_config(
|
||||
cls, hass: HomeAssistant, config: ConfigType
|
||||
) -> ConfigType:
|
||||
"""Validate config."""
|
||||
return config
|
||||
|
||||
class MockCondition1(MockCondition):
|
||||
"""Mock condition 1."""
|
||||
|
||||
async def async_condition_from_config(self) -> condition.ConditionCheckerType:
|
||||
"""Evaluate state based on configuration."""
|
||||
return lambda hass, vars: True
|
||||
|
||||
class MockCondition2(MockCondition):
|
||||
"""Mock condition 2."""
|
||||
|
||||
async def async_condition_from_config(self) -> condition.ConditionCheckerType:
|
||||
"""Evaluate state based on configuration."""
|
||||
return lambda hass, vars: False
|
||||
|
||||
async def async_get_conditions(
|
||||
hass: HomeAssistant,
|
||||
) -> dict[str, type[condition.Condition]]:
|
||||
return {
|
||||
"test": MockCondition1,
|
||||
"test.cond_2": MockCondition2,
|
||||
}
|
||||
|
||||
mock_integration(hass, MockModule("test"))
|
||||
mock_platform(
|
||||
hass, "test.condition", Mock(async_get_conditions=async_get_conditions)
|
||||
)
|
||||
|
||||
config_1 = {CONF_CONDITION: "test"}
|
||||
config_2 = {CONF_CONDITION: "test.cond_2"}
|
||||
config_3 = {CONF_CONDITION: "test.unknown_cond"}
|
||||
assert await condition.async_validate_condition_config(hass, config_1) == config_1
|
||||
assert await condition.async_validate_condition_config(hass, config_2) == config_2
|
||||
with pytest.raises(
|
||||
vol.Invalid, match="Invalid condition 'test.unknown_cond' specified"
|
||||
):
|
||||
await condition.async_validate_condition_config(hass, config_3)
|
||||
|
||||
cond_func = await condition.async_from_config(hass, config_1)
|
||||
assert cond_func(hass, {}) is True
|
||||
|
||||
cond_func = await condition.async_from_config(hass, config_2)
|
||||
assert cond_func(hass, {}) is False
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
await condition.async_from_config(hass, config_3)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("enabled_value", [True, "{{ 1 == 1 }}"])
|
||||
async def test_enabled_condition(
|
||||
hass: HomeAssistant, enabled_value: bool | str
|
||||
|
Reference in New Issue
Block a user