Skip to content

Commit

Permalink
Added state condition; some refactorin
Browse files Browse the repository at this point in the history
  • Loading branch information
muxa committed Sep 18, 2021
1 parent c343cbc commit 5f34e19
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ Configuration options:
* **id** (*Optional*, [ID](https://esphome.io/guides/configuration-types.html#config-id)): The ID of the state machine.
* **state** (**Required**, string): The state to set state machine to bypassing transitions.

## `state_machine.state` Condition

This condition lets you check what state the machine is currently in.

```yaml
# in some trigger
on_...:
# Basic
if:
condition:
state_machine.state: "ON"
then:
- logger.log: Is ON
# Advanced
if:
condition:
state_machine.state:
id: sm1
value: "ON"
then:
- logger.log: Is ON
```

## `state_machine.transition` Condition

This condition lets you check what transition last occurred.
Expand Down
29 changes: 24 additions & 5 deletions components/state_machine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
CONF_TRIGGER_ID,
CONF_FROM,
CONF_TO,
CONF_STATE,
CONF_VALUE
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,6 +51,7 @@

StateMachineTransitionAction = state_machine_ns.class_("StateMachineTransitionAction", automation.Action)

StateMachineStateCondition = state_machine_ns.class_("StateMachineStateCondition", automation.Condition)
StateMachineTransitionCondition = state_machine_ns.class_("StateMachineTransitionCondition", automation.Condition)

CONF_DIAGRAM = 'diagram'
Expand All @@ -68,8 +71,6 @@
CONF_TRANSITION_INPUT_KEY = 'input'
CONF_TRANSITION_TO_KEY = 'to'

CONF_STATE_KEY = 'state'

CONF_STATE_MACHINE_ID = 'state_machine_id'

def validate_transition(value):
Expand Down Expand Up @@ -300,13 +301,13 @@ async def to_code(config):
cv.maybe_simple_value(
{
cv.GenerateID(): cv.use_id(StateMachineComponent),
cv.Required(CONF_STATE_KEY): cv.string_strict,
cv.Required(CONF_STATE): cv.string_strict,
},
key=CONF_STATE_KEY
key=CONF_STATE
),
)
def state_machine_set_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg, config[CONF_STATE_KEY])
var = cg.new_Pvariable(action_id, template_arg, config[CONF_STATE])
yield cg.register_parented(var, config[CONF_ID])
yield var

Expand All @@ -326,6 +327,24 @@ def state_machine_transition_to_code(config, action_id, template_arg, args):
yield cg.register_parented(var, config[CONF_ID])
yield var

@automation.register_condition(
"state_machine.state",
StateMachineStateCondition,
cv.maybe_simple_value(
{
cv.GenerateID(): cv.use_id(StateMachineComponent),
cv.Required(CONF_VALUE): cv.templatable(cv.string_strict)
},
key=CONF_VALUE
),
)
async def state_machine_state_condition_to_code(config, condition_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(condition_id, template_arg, paren)
cg.add(var.set_value(await cg.templatable(config[CONF_VALUE], args, cg.std_string)))
return var


@automation.register_condition(
"state_machine.transition",
StateMachineTransitionCondition,
Expand Down
17 changes: 17 additions & 0 deletions components/state_machine/automation.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ namespace esphome
std::string input_;
};

template <typename... Ts>
class StateMachineStateCondition : public Condition<Ts...>
{
public:
explicit StateMachineStateCondition(StateMachineComponent *parent) : parent_(parent) {}

TEMPLATABLE_VALUE(std::string, value)

bool check(Ts... x) override
{
return this->value_.value(x...) == this->parent_->current_state();
}

protected:
StateMachineComponent *parent_;
};

template <typename... Ts>
class StateMachineTransitionCondition : public Condition<Ts...>
{
Expand Down

0 comments on commit 5f34e19

Please sign in to comment.