Skip to content

Commit 6e41ad9

Browse files
committed
Add documentation on conditions
1 parent 40fdc6a commit 6e41ad9

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

docs/automations.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class EventTrigger(Trigger):
8585
### Registering triggers
8686

8787
Implement `async_get_triggers` in the `trigger` platform to register all the integration's triggers.
88-
Each trigger is identified by a unique string (e.g., `"event"` in the example above).
88+
Each trigger is identified by a unique string (e.g., `"event"` in the following example).
8989

9090
```python
9191
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
@@ -96,3 +96,103 @@ async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
9696
```
9797

9898

99+
## Conditions
100+
101+
Conditions are checks that must be met in order for an automation to trigger. Implement them in the `condition` platform (`condition.py`) of your integration by creating and registering condition classes.
102+
103+
### Condition class
104+
105+
Conditions inherit from `homeassistant.helpers.condition.Condition` and must implement `async_validate_config` and `async_get_checker`.
106+
Just as with the [trigger class](#trigger-class), `async_validate_config` is used to validate the condition configuration.
107+
`async_get_checker` should return a function that will be called whenever the condition needs to be checked.
108+
109+
In the following snippet we create a condition that can be configured to only pass when `binary_sensor.front_door` has a desired configured state.
110+
111+
```python
112+
from typing import TYPE_CHECKING, override
113+
114+
import voluptuous as vol
115+
116+
from homeassistant.const import STATE_OFF, STATE_ON, CONF_STATE
117+
from homeassistant.core import HomeAssistant
118+
from homeassistant.helpers import config_validation as cv
119+
from homeassistant.helpers.condition import (
120+
Condition,
121+
ConditionCheckerType,
122+
ConditionConfig,
123+
trace_condition_function,
124+
)
125+
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
126+
127+
STATE_CONDITION_SCHEMA = vol.Schema(
128+
{
129+
vol.Required(CONF_STATE): vol.In(STATE_ON, STATE_OFF),
130+
}
131+
)
132+
133+
134+
class DoorStateConditionBase(Condition):
135+
"""State condition."""
136+
137+
@override
138+
@classmethod
139+
async def async_validate_config(
140+
cls, hass: HomeAssistant, config: ConfigType
141+
) -> ConfigType:
142+
"""Validate config."""
143+
return STATE_CONDITION_SCHEMA(config)
144+
145+
def __init__(
146+
self, hass: HomeAssistant, config: ConditionConfig
147+
) -> None:
148+
"""Initialize condition."""
149+
self._hass = hass
150+
if TYPE_CHECKING:
151+
assert config.options
152+
self._state = config.options[CONF_STATE]
153+
154+
@override
155+
async def async_get_checker(self) -> ConditionCheckerType:
156+
"""Get the condition checker."""
157+
158+
@trace_condition_function
159+
def test_state(hass: HomeAssistant, _: TemplateVarsType = None) -> bool:
160+
"""Test state condition."""
161+
# In reality this would be more configurable
162+
# but for the sake of example it's simplified.
163+
return hass.states.get("binary_sensor.front_door") == self._state
164+
165+
return test_state
166+
```
167+
168+
### Registering conditions
169+
170+
To register the conditions `async_get_conditions` should be implemented in the `condition` platform for that integration.
171+
Each condition is identified by a unique string (e.g., `"door_state"` in the example below).
172+
173+
```python
174+
async def async_get_conditions(hass: HomeAssistant) -> dict[str, type[Condition]]:
175+
"""Return the door state conditions."""
176+
return {
177+
"door_state": DoorStateConditionBase,
178+
}
179+
```
180+
181+
### Condition schema
182+
183+
The frontend uses the `conditions.yaml` file to know the structure of the conditions.
184+
This file is similar to `triggers.yaml` and `services.yaml`.
185+
186+
For example, the following snippet shows the `door_state` condition described in the previous example.
187+
188+
```yaml
189+
door_state:
190+
fields:
191+
state:
192+
required: true
193+
selector:
194+
select:
195+
options:
196+
- on
197+
- off
198+
```

0 commit comments

Comments
 (0)