muxa / esphome-state-machine

ESPHome State Machine component
MIT License
74 stars 7 forks source link

Add posibility of creating the conditional transitions #38

Open majkrzak opened 1 year ago

majkrzak commented 1 year ago

I would like to suggest an enhancement, which I fill may be extremely useful.

Adding possibility to specify the condition parameter for transitions, like:

transitions:
- from: IDLE
  to: SCANNING
  condition:
     lambda: ...
majkrzak commented 1 year ago

I've created initial solution. A bit purkish and messy, but may work. https://github.com/muxa/esphome-state-machine/compare/main...majkrzak:esphome-state-machine:master

muxa commented 1 year ago

Thanks, @majkrzak, for providing a solution. From the first glance it looks quite elegant. Do you have any ideas how conditions has be aded to the diagram output? I don't have any free time at the moment to integrate this, but when I do I'll look into integrating your change into the codebase.

EternityForest commented 1 year ago

@muxa I have basic Mermaid chart support for conditions: https://github.com/EternityForest/esphome-state-machine/tree/integrate-conditions

Edit: GraphViz DOT also supported now

It's fairly complex and adds a lot of new code, but it tries to produce nice uncluttered diagrams.

Algorithm

Basically what I'm doing is adding (? CONDITION) to the label.

If it's a lambda, I remove the "return" part and the semicolon to save space.

If it is a sensor or number in_range, I take the ID and output something like (? 60.0<component_id<100.0)

If it's something else, then I remove the part before the dot(Unless it is very short), so "binary_sensor.is_on" becomes "is_on", and then add the argument, if it's an ID, So we get something like "is_on fooSensor".

Finally, if it is still too long, you get a (? truncated_text_here[1]), and then the full condition goes in a footnote.