mirror of
https://github.com/home-assistant/core.git
synced 2025-08-07 12:38:21 +00:00
Adjust condition and trigger method names (#150060)
This commit is contained in:
parent
12dca4b1bf
commit
2b0cda0ad1
@ -61,7 +61,7 @@ class DeviceCondition(Condition):
|
|||||||
self._hass = hass
|
self._hass = hass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def async_validate_condition_config(
|
async def async_validate_config(
|
||||||
cls, hass: HomeAssistant, config: ConfigType
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
) -> ConfigType:
|
) -> ConfigType:
|
||||||
"""Validate device condition config."""
|
"""Validate device condition config."""
|
||||||
@ -69,7 +69,7 @@ class DeviceCondition(Condition):
|
|||||||
hass, config, cv.DEVICE_CONDITION_SCHEMA, DeviceAutomationType.CONDITION
|
hass, config, cv.DEVICE_CONDITION_SCHEMA, DeviceAutomationType.CONDITION
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_condition_from_config(self) -> condition.ConditionCheckerType:
|
async def async_get_checker(self) -> condition.ConditionCheckerType:
|
||||||
"""Test a device condition."""
|
"""Test a device condition."""
|
||||||
platform = await async_get_device_automation_platform(
|
platform = await async_get_device_automation_platform(
|
||||||
self._hass, self._config[CONF_DOMAIN], DeviceAutomationType.CONDITION
|
self._hass, self._config[CONF_DOMAIN], DeviceAutomationType.CONDITION
|
||||||
|
@ -131,13 +131,13 @@ class SunCondition(Condition):
|
|||||||
self._hass = hass
|
self._hass = hass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def async_validate_condition_config(
|
async def async_validate_config(
|
||||||
cls, hass: HomeAssistant, config: ConfigType
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
) -> ConfigType:
|
) -> ConfigType:
|
||||||
"""Validate config."""
|
"""Validate config."""
|
||||||
return _CONDITION_SCHEMA(config) # type: ignore[no-any-return]
|
return _CONDITION_SCHEMA(config) # type: ignore[no-any-return]
|
||||||
|
|
||||||
async def async_condition_from_config(self) -> ConditionCheckerType:
|
async def async_get_checker(self) -> ConditionCheckerType:
|
||||||
"""Wrap action method with sun based condition."""
|
"""Wrap action method with sun based condition."""
|
||||||
before = self._config.get("before")
|
before = self._config.get("before")
|
||||||
after = self._config.get("after")
|
after = self._config.get("after")
|
||||||
|
@ -100,13 +100,13 @@ class ZoneCondition(Condition):
|
|||||||
self._config = config
|
self._config = config
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def async_validate_condition_config(
|
async def async_validate_config(
|
||||||
cls, hass: HomeAssistant, config: ConfigType
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
) -> ConfigType:
|
) -> ConfigType:
|
||||||
"""Validate config."""
|
"""Validate config."""
|
||||||
return _CONDITION_SCHEMA(config) # type: ignore[no-any-return]
|
return _CONDITION_SCHEMA(config) # type: ignore[no-any-return]
|
||||||
|
|
||||||
async def async_condition_from_config(self) -> ConditionCheckerType:
|
async def async_get_checker(self) -> ConditionCheckerType:
|
||||||
"""Wrap action method with zone based condition."""
|
"""Wrap action method with zone based condition."""
|
||||||
entity_ids = self._config.get(CONF_ENTITY_ID, [])
|
entity_ids = self._config.get(CONF_ENTITY_ID, [])
|
||||||
zone_entity_ids = self._config.get(CONF_ZONE, [])
|
zone_entity_ids = self._config.get(CONF_ZONE, [])
|
||||||
|
@ -263,13 +263,13 @@ class EventTrigger(Trigger):
|
|||||||
self._hass = hass
|
self._hass = hass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def async_validate_trigger_config(
|
async def async_validate_config(
|
||||||
cls, hass: HomeAssistant, config: ConfigType
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
) -> ConfigType:
|
) -> ConfigType:
|
||||||
"""Validate config."""
|
"""Validate config."""
|
||||||
return await async_validate_trigger_config(hass, config)
|
return await async_validate_trigger_config(hass, config)
|
||||||
|
|
||||||
async def async_attach_trigger(
|
async def async_attach(
|
||||||
self,
|
self,
|
||||||
action: TriggerActionType,
|
action: TriggerActionType,
|
||||||
trigger_info: TriggerInfo,
|
trigger_info: TriggerInfo,
|
||||||
|
@ -216,13 +216,13 @@ class ValueUpdatedTrigger(Trigger):
|
|||||||
self._hass = hass
|
self._hass = hass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def async_validate_trigger_config(
|
async def async_validate_config(
|
||||||
cls, hass: HomeAssistant, config: ConfigType
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
) -> ConfigType:
|
) -> ConfigType:
|
||||||
"""Validate config."""
|
"""Validate config."""
|
||||||
return await async_validate_trigger_config(hass, config)
|
return await async_validate_trigger_config(hass, config)
|
||||||
|
|
||||||
async def async_attach_trigger(
|
async def async_attach(
|
||||||
self,
|
self,
|
||||||
action: TriggerActionType,
|
action: TriggerActionType,
|
||||||
trigger_info: TriggerInfo,
|
trigger_info: TriggerInfo,
|
||||||
|
@ -199,14 +199,14 @@ class Condition(abc.ABC):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def async_validate_condition_config(
|
async def async_validate_config(
|
||||||
cls, hass: HomeAssistant, config: ConfigType
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
) -> ConfigType:
|
) -> ConfigType:
|
||||||
"""Validate config."""
|
"""Validate config."""
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def async_condition_from_config(self) -> ConditionCheckerType:
|
async def async_get_checker(self) -> ConditionCheckerType:
|
||||||
"""Evaluate state based on configuration."""
|
"""Get the condition checker."""
|
||||||
|
|
||||||
|
|
||||||
class ConditionProtocol(Protocol):
|
class ConditionProtocol(Protocol):
|
||||||
@ -346,7 +346,7 @@ async def async_from_config(
|
|||||||
if platform is not None:
|
if platform is not None:
|
||||||
condition_descriptors = await platform.async_get_conditions(hass)
|
condition_descriptors = await platform.async_get_conditions(hass)
|
||||||
condition_instance = condition_descriptors[condition](hass, config)
|
condition_instance = condition_descriptors[condition](hass, config)
|
||||||
return await condition_instance.async_condition_from_config()
|
return await condition_instance.async_get_checker()
|
||||||
|
|
||||||
for fmt in (ASYNC_FROM_CONFIG_FORMAT, FROM_CONFIG_FORMAT):
|
for fmt in (ASYNC_FROM_CONFIG_FORMAT, FROM_CONFIG_FORMAT):
|
||||||
factory = getattr(sys.modules[__name__], fmt.format(condition), None)
|
factory = getattr(sys.modules[__name__], fmt.format(condition), None)
|
||||||
@ -974,7 +974,7 @@ async def async_validate_condition_config(
|
|||||||
condition_descriptors = await platform.async_get_conditions(hass)
|
condition_descriptors = await platform.async_get_conditions(hass)
|
||||||
if not (condition_class := condition_descriptors.get(condition)):
|
if not (condition_class := condition_descriptors.get(condition)):
|
||||||
raise vol.Invalid(f"Invalid condition '{condition}' specified")
|
raise vol.Invalid(f"Invalid condition '{condition}' specified")
|
||||||
return await condition_class.async_validate_condition_config(hass, config)
|
return await condition_class.async_validate_config(hass, config)
|
||||||
if platform is None and condition in ("numeric_state", "state"):
|
if platform is None and condition in ("numeric_state", "state"):
|
||||||
validator = cast(
|
validator = cast(
|
||||||
Callable[[HomeAssistant, ConfigType], ConfigType],
|
Callable[[HomeAssistant, ConfigType], ConfigType],
|
||||||
|
@ -173,18 +173,18 @@ class Trigger(abc.ABC):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def async_validate_trigger_config(
|
async def async_validate_config(
|
||||||
cls, hass: HomeAssistant, config: ConfigType
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
) -> ConfigType:
|
) -> ConfigType:
|
||||||
"""Validate config."""
|
"""Validate config."""
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def async_attach_trigger(
|
async def async_attach(
|
||||||
self,
|
self,
|
||||||
action: TriggerActionType,
|
action: TriggerActionType,
|
||||||
trigger_info: TriggerInfo,
|
trigger_info: TriggerInfo,
|
||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
"""Attach a trigger."""
|
"""Attach the trigger."""
|
||||||
|
|
||||||
|
|
||||||
class TriggerProtocol(Protocol):
|
class TriggerProtocol(Protocol):
|
||||||
@ -390,7 +390,7 @@ async def async_validate_trigger_config(
|
|||||||
)
|
)
|
||||||
if not (trigger := trigger_descriptors.get(relative_trigger_key)):
|
if not (trigger := trigger_descriptors.get(relative_trigger_key)):
|
||||||
raise vol.Invalid(f"Invalid trigger '{trigger_key}' specified")
|
raise vol.Invalid(f"Invalid trigger '{trigger_key}' specified")
|
||||||
conf = await trigger.async_validate_trigger_config(hass, conf)
|
conf = await trigger.async_validate_config(hass, conf)
|
||||||
elif hasattr(platform, "async_validate_trigger_config"):
|
elif hasattr(platform, "async_validate_trigger_config"):
|
||||||
conf = await platform.async_validate_trigger_config(hass, conf)
|
conf = await platform.async_validate_trigger_config(hass, conf)
|
||||||
else:
|
else:
|
||||||
@ -495,7 +495,7 @@ async def async_initialize_triggers(
|
|||||||
platform_domain, trigger_key
|
platform_domain, trigger_key
|
||||||
)
|
)
|
||||||
trigger = trigger_descriptors[relative_trigger_key](hass, conf)
|
trigger = trigger_descriptors[relative_trigger_key](hass, conf)
|
||||||
coro = trigger.async_attach_trigger(action_wrapper, info)
|
coro = trigger.async_attach(action_wrapper, info)
|
||||||
else:
|
else:
|
||||||
coro = platform.async_attach_trigger(hass, conf, action_wrapper, info)
|
coro = platform.async_attach_trigger(hass, conf, action_wrapper, info)
|
||||||
|
|
||||||
|
@ -977,7 +977,7 @@ async def test_zwave_js_event_invalid_config_entry_id(
|
|||||||
async def test_invalid_trigger_configs(hass: HomeAssistant) -> None:
|
async def test_invalid_trigger_configs(hass: HomeAssistant) -> None:
|
||||||
"""Test invalid trigger configs."""
|
"""Test invalid trigger configs."""
|
||||||
with pytest.raises(vol.Invalid):
|
with pytest.raises(vol.Invalid):
|
||||||
await TRIGGERS["event"].async_validate_trigger_config(
|
await TRIGGERS["event"].async_validate_config(
|
||||||
hass,
|
hass,
|
||||||
{
|
{
|
||||||
"platform": f"{DOMAIN}.event",
|
"platform": f"{DOMAIN}.event",
|
||||||
@ -988,7 +988,7 @@ async def test_invalid_trigger_configs(hass: HomeAssistant) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
with pytest.raises(vol.Invalid):
|
with pytest.raises(vol.Invalid):
|
||||||
await TRIGGERS["value_updated"].async_validate_trigger_config(
|
await TRIGGERS["value_updated"].async_validate_config(
|
||||||
hass,
|
hass,
|
||||||
{
|
{
|
||||||
"platform": f"{DOMAIN}.value_updated",
|
"platform": f"{DOMAIN}.value_updated",
|
||||||
@ -1026,7 +1026,7 @@ async def test_zwave_js_trigger_config_entry_unloaded(
|
|||||||
await hass.config_entries.async_unload(integration.entry_id)
|
await hass.config_entries.async_unload(integration.entry_id)
|
||||||
|
|
||||||
# Test full validation for both events
|
# Test full validation for both events
|
||||||
assert await TRIGGERS["value_updated"].async_validate_trigger_config(
|
assert await TRIGGERS["value_updated"].async_validate_config(
|
||||||
hass,
|
hass,
|
||||||
{
|
{
|
||||||
"platform": f"{DOMAIN}.value_updated",
|
"platform": f"{DOMAIN}.value_updated",
|
||||||
@ -1036,7 +1036,7 @@ async def test_zwave_js_trigger_config_entry_unloaded(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert await TRIGGERS["event"].async_validate_trigger_config(
|
assert await TRIGGERS["event"].async_validate_config(
|
||||||
hass,
|
hass,
|
||||||
{
|
{
|
||||||
"platform": f"{DOMAIN}.event",
|
"platform": f"{DOMAIN}.event",
|
||||||
|
@ -2089,7 +2089,7 @@ async def test_platform_multiple_conditions(hass: HomeAssistant) -> None:
|
|||||||
"""Initialize condition."""
|
"""Initialize condition."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def async_validate_condition_config(
|
async def async_validate_config(
|
||||||
cls, hass: HomeAssistant, config: ConfigType
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
) -> ConfigType:
|
) -> ConfigType:
|
||||||
"""Validate config."""
|
"""Validate config."""
|
||||||
@ -2098,14 +2098,14 @@ async def test_platform_multiple_conditions(hass: HomeAssistant) -> None:
|
|||||||
class MockCondition1(MockCondition):
|
class MockCondition1(MockCondition):
|
||||||
"""Mock condition 1."""
|
"""Mock condition 1."""
|
||||||
|
|
||||||
async def async_condition_from_config(self) -> condition.ConditionCheckerType:
|
async def async_get_checker(self) -> condition.ConditionCheckerType:
|
||||||
"""Evaluate state based on configuration."""
|
"""Evaluate state based on configuration."""
|
||||||
return lambda hass, vars: True
|
return lambda hass, vars: True
|
||||||
|
|
||||||
class MockCondition2(MockCondition):
|
class MockCondition2(MockCondition):
|
||||||
"""Mock condition 2."""
|
"""Mock condition 2."""
|
||||||
|
|
||||||
async def async_condition_from_config(self) -> condition.ConditionCheckerType:
|
async def async_get_checker(self) -> condition.ConditionCheckerType:
|
||||||
"""Evaluate state based on configuration."""
|
"""Evaluate state based on configuration."""
|
||||||
return lambda hass, vars: False
|
return lambda hass, vars: False
|
||||||
|
|
||||||
|
@ -461,7 +461,7 @@ async def test_platform_multiple_triggers(hass: HomeAssistant) -> None:
|
|||||||
"""Initialize trigger."""
|
"""Initialize trigger."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def async_validate_trigger_config(
|
async def async_validate_config(
|
||||||
cls, hass: HomeAssistant, config: ConfigType
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
) -> ConfigType:
|
) -> ConfigType:
|
||||||
"""Validate config."""
|
"""Validate config."""
|
||||||
@ -470,7 +470,7 @@ async def test_platform_multiple_triggers(hass: HomeAssistant) -> None:
|
|||||||
class MockTrigger1(MockTrigger):
|
class MockTrigger1(MockTrigger):
|
||||||
"""Mock trigger 1."""
|
"""Mock trigger 1."""
|
||||||
|
|
||||||
async def async_attach_trigger(
|
async def async_attach(
|
||||||
self,
|
self,
|
||||||
action: TriggerActionType,
|
action: TriggerActionType,
|
||||||
trigger_info: TriggerInfo,
|
trigger_info: TriggerInfo,
|
||||||
@ -481,7 +481,7 @@ async def test_platform_multiple_triggers(hass: HomeAssistant) -> None:
|
|||||||
class MockTrigger2(MockTrigger):
|
class MockTrigger2(MockTrigger):
|
||||||
"""Mock trigger 2."""
|
"""Mock trigger 2."""
|
||||||
|
|
||||||
async def async_attach_trigger(
|
async def async_attach(
|
||||||
self,
|
self,
|
||||||
action: TriggerActionType,
|
action: TriggerActionType,
|
||||||
trigger_info: TriggerInfo,
|
trigger_info: TriggerInfo,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user