platypush/platypush/message/event/flic/__init__.py

51 lines
1.6 KiB
Python
Raw Normal View History

from typing import Sequence
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>`_.
2018-07-09 22:37:54 +02:00
"""
2017-12-24 13:15:37 +01:00
def __init__(self, btn_addr: str, sequence: Sequence[str], *_, **kwargs):
2018-07-09 22:37:54 +02:00
"""
:param btn_addr: Physical address of the button that originated the
event.
:param sequence: Detected sequence, as a list of Flic button event
types (either ``ShortPressEvent`` or ``LongPressEvent``).
2018-07-09 22:37:54 +02:00
"""
super().__init__(btn_addr=btn_addr, sequence=sequence, **kwargs)
2017-12-24 13:15:37 +01:00
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``).
2018-07-24 17:01:01 +02:00
"""
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']
):
2017-12-24 13:15:37 +01:00
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
2017-12-24 13:15:37 +01:00
# vim:sw=4:ts=4:et: