2017-12-24 13:15:37 +01:00
|
|
|
from platypush.message.event import Event, EventMatchResult
|
|
|
|
|
|
|
|
|
|
|
|
class FlicButtonEvent(Event):
|
2018-07-09 22:37:54 +02:00
|
|
|
"""
|
|
|
|
Event triggered when a sequence of user short/long presses is detected on a
|
|
|
|
Flic button (https://flic.io).
|
|
|
|
"""
|
2017-12-24 13:15:37 +01:00
|
|
|
|
|
|
|
def __init__(self, btn_addr, sequence, *args, **kwargs):
|
2018-07-09 22:37:54 +02:00
|
|
|
"""
|
|
|
|
:param btn_addr: Physical address of the button that originated the event
|
|
|
|
:type btn_addr: str
|
|
|
|
|
|
|
|
:param sequence: Detected sequence, as a list of Flic button event types (either "ShortPressEvent" or "LongPressEvent")
|
|
|
|
:type sequence: list[str]
|
|
|
|
"""
|
|
|
|
|
2017-12-24 13:15:37 +01:00
|
|
|
super().__init__(btn_addr=btn_addr, sequence=sequence, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def matches_condition(self, condition):
|
2018-07-24 17:01:01 +02:00
|
|
|
"""
|
|
|
|
:param condition: Condition to be checked against, as a sequence of button presses ("ShortPressEvent" and "LongPressEvent")
|
|
|
|
:type condition: list
|
|
|
|
"""
|
|
|
|
|
2017-12-24 13:15:37 +01:00
|
|
|
result = EventMatchResult(is_match=False)
|
|
|
|
|
|
|
|
if not isinstance(self, condition.type) \
|
|
|
|
or self.args['btn_addr'] != condition.args['btn_addr']:
|
|
|
|
return result
|
|
|
|
|
|
|
|
cond_sequence = list(condition.args['sequence'])
|
|
|
|
event_sequence = list(self.args['sequence'])
|
|
|
|
|
|
|
|
while cond_sequence and event_sequence:
|
|
|
|
cond_press = cond_sequence[0]
|
|
|
|
event_press = event_sequence[0]
|
|
|
|
|
|
|
|
if cond_press == event_press:
|
|
|
|
cond_sequence.pop(0)
|
|
|
|
result.score += 1
|
|
|
|
|
|
|
|
event_sequence.pop(0)
|
|
|
|
|
|
|
|
result.is_match = len(cond_sequence) == 0
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|