hass-4button/wallmote.py

47 lines
1.8 KiB
Python

#!/usr/bin/python3
import hassapi as hass
from threading import Timer
from actions import check_action, run_action
# Short presses will be recorded into a sequence array. Long presses are reserved for
# actions directly in hass for now; use KeyReleased event to capture them. A sequence
# can be ended either immediately by recognizing a full sequence or by TIMEOUT seconds
# passing.
# - self.currentSequence contains an array of keys representing a sequence that is
# actively being entered.
# - self.tot contains a timer for the process_sequence function, ending sequence entry.
TIMEOUT=1
class WallmotePlumbing(hass.Hass):
def initialize(self):
self.currentSequence = []
self.listen_event(self.onevent, "zwave_js_value_notification")
def onevent(self, _, event, kwargs):
if (event.get('value') == 'KeyHeldDown'): return
if (event.get('device_id') != '3f7add36f94839b1446886b5dab35e6d'): return
key = int(event.get('property_key'))
# We don't get a KeyPressed event at all for long presses
evt = event.get('value')
if (evt == "KeyPressed"): self.shortpress(key)
def shortpress(self, key):
self.currentSequence.append(key)
self.reset_tot()
# Don't wait for tot to expire if we've already hit the end of a branch
if (check_action(self.currentSequence.copy())): self.process_sequence()
def reset_tot(self):
if (hasattr(self,"tot")): self.tot.cancel()
self.tot = Timer(TIMEOUT,self.process_sequence)
self.tot.start()
def process_sequence(self):
self.tot.cancel(); # So process_sequence can be called directly
sequence = self.currentSequence
self.currentSequence = []
self.log(sequence)
run_action(self,sequence)