forked from Phantom-Cluster/Alexa-Raspberry-Single-Relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debounce_handler.py
37 lines (28 loc) · 1.06 KB
/
debounce_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import time
class debounce_handler(object):
"""Use this handler to keep multiple Amazon Echo devices from reacting to
the same voice command.
"""
DEBOUNCE_SECONDS = 0.3
def __init__(self):
self.lastEcho = time.time()
def on(self, client_address, name):
if self.debounce():
return True
return self.act(client_address, True, name)
def off(self, client_address, name):
if self.debounce():
return True
return self.act(client_address, False, name)
def act(self, client_address, state):
pass
def debounce(self):
"""If multiple Echos are present, the one most likely to respond first
is the one that can best hear the speaker... which is the closest one.
Adding a refractory period to handlers keeps us from worrying about
one Echo overhearing a command meant for another one.
"""
if (time.time() - self.lastEcho) < self.DEBOUNCE_SECONDS:
return True
self.lastEcho = time.time()
return False