Skip to content

Instantly share code, notes, and snippets.

@PeWu
Created December 27, 2018 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeWu/8c146df8fbe94306f1ebcd362117960d to your computer and use it in GitHub Desktop.
Save PeWu/8c146df8fbe94306f1ebcd362117960d to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import appdaemon.plugins.hass.hassapi as hass
from enum import Enum
from machine import Machine, ANY, StateOn, StateOff, Timeout
class States(Enum):
DISABLED = 1
IDLE = 2
ACTIVE = 3
globals().update(States.__members__)
DEFAULT_DELAY = 180 # seconds
class LightingSM(hass.Hass):
def initialize(self):
machine = Machine(self, States)
for e in self.args.get("overrides", []):
machine.add_transitions(ANY, StateOn(e), DISABLED)
machine.add_transition(DISABLED, StateOff(e), IDLE)
for e in self.args.get('sensors', []):
machine.add_transition(
IDLE, StateOn(e), ACTIVE, on_transition=self.on_enter_active)
# Self-transition resets the timer.
machine.add_transition(ACTIVE, StateOn(e), ACTIVE)
machine.add_transition(
ACTIVE, Timeout(DEFAULT_DELAY), IDLE, on_transition=self.on_exit_active)
def on_enter_active(self):
# TODO: Turn on lights
def on_exit_active(self):
# TODO: Turn off lights
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment