|
25 | 25 | "StateMachineComponent", cg.Component
|
26 | 26 | )
|
27 | 27 |
|
| 28 | +StateMachineOnSetTrigger = state_machine_ns.class_( |
| 29 | + "StateMachineOnSetTrigger", automation.Trigger.template() |
| 30 | +) |
| 31 | + |
28 | 32 | StateMachineOnEnterTrigger = state_machine_ns.class_(
|
29 | 33 | "StateMachineOnEnterTrigger", automation.Trigger.template()
|
30 | 34 | )
|
|
41 | 45 | "StateMachineTransitionActionTrigger", automation.Trigger.template()
|
42 | 46 | )
|
43 | 47 |
|
| 48 | +StateMachineSetAction = state_machine_ns.class_("StateMachineSetAction", automation.Action) |
| 49 | + |
44 | 50 | StateMachineTransitionAction = state_machine_ns.class_("StateMachineTransitionAction", automation.Action)
|
45 | 51 |
|
46 | 52 | StateMachineTransitionCondition = state_machine_ns.class_("StateMachineTransitionCondition", automation.Condition)
|
|
51 | 57 | CONF_INPUTS_KEY = 'inputs'
|
52 | 58 | CONF_TRANSITIONS_KEY = 'transitions'
|
53 | 59 |
|
| 60 | +CONF_STATE_ON_SET_KEY = 'on_set' |
54 | 61 | CONF_STATE_ON_ENTER_KEY = 'on_enter'
|
55 | 62 | CONF_STATE_ON_LEAVE_KEY = 'on_leave'
|
56 | 63 | CONF_INPUT_TRANSITIONS_KEY = 'transitions'
|
|
61 | 68 | CONF_TRANSITION_INPUT_KEY = 'input'
|
62 | 69 | CONF_TRANSITION_TO_KEY = 'to'
|
63 | 70 |
|
| 71 | +CONF_STATE_KEY = 'state' |
| 72 | + |
64 | 73 | CONF_STATE_MACHINE_ID = 'state_machine_id'
|
65 | 74 |
|
66 | 75 | def validate_transition(value):
|
@@ -137,6 +146,11 @@ def unique_names(items):
|
137 | 146 | cv.ensure_list(cv.maybe_simple_value(
|
138 | 147 | {
|
139 | 148 | cv.Required(CONF_NAME): cv.string_strict,
|
| 149 | + cv.Optional(CONF_STATE_ON_SET_KEY): automation.validate_automation( |
| 150 | + { |
| 151 | + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(StateMachineOnSetTrigger), |
| 152 | + } |
| 153 | + ), |
140 | 154 | cv.Optional(CONF_STATE_ON_ENTER_KEY): automation.validate_automation(
|
141 | 155 | {
|
142 | 156 | cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(StateMachineOnEnterTrigger),
|
@@ -221,7 +235,17 @@ async def to_code(config):
|
221 | 235 | if CONF_NAME in config:
|
222 | 236 | cg.add(var.set_name(config[CONF_NAME]))
|
223 | 237 |
|
224 |
| - # setup on_leave automations first (to ensure they are executed before on_enter) |
| 238 | + # setup on_set automations first |
| 239 | + for state in config[CONF_STATES_KEY]: |
| 240 | + |
| 241 | + if CONF_STATE_ON_LEAVE_KEY in state: |
| 242 | + for action in state.get(CONF_STATE_ON_SET_KEY, []): |
| 243 | + trigger = cg.new_Pvariable( |
| 244 | + action[CONF_TRIGGER_ID], var, state[CONF_NAME] |
| 245 | + ) |
| 246 | + await automation.build_automation(trigger, [], action) |
| 247 | + |
| 248 | + # then setup on_leave automations (to ensure they are executed before on_enter) |
225 | 249 | for state in config[CONF_STATES_KEY]:
|
226 | 250 |
|
227 | 251 | if CONF_STATE_ON_LEAVE_KEY in state:
|
@@ -270,6 +294,22 @@ async def to_code(config):
|
270 | 294 |
|
271 | 295 | cg.add(var.dump_config())
|
272 | 296 |
|
| 297 | +@automation.register_action( |
| 298 | + "state_machine.set", |
| 299 | + StateMachineSetAction, |
| 300 | + cv.maybe_simple_value( |
| 301 | + { |
| 302 | + cv.GenerateID(): cv.use_id(StateMachineComponent), |
| 303 | + cv.Required(CONF_STATE_KEY): cv.string_strict, |
| 304 | + }, |
| 305 | + key=CONF_STATE_KEY |
| 306 | + ), |
| 307 | +) |
| 308 | +def state_machine_set_to_code(config, action_id, template_arg, args): |
| 309 | + var = cg.new_Pvariable(action_id, template_arg, config[CONF_STATE_KEY]) |
| 310 | + yield cg.register_parented(var, config[CONF_ID]) |
| 311 | + yield var |
| 312 | + |
273 | 313 | @automation.register_action(
|
274 | 314 | "state_machine.transition",
|
275 | 315 | StateMachineTransitionAction,
|
|
0 commit comments