-
Notifications
You must be signed in to change notification settings - Fork 5
/
base.py
79 lines (70 loc) · 2.22 KB
/
base.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import logging
import json
from device import Device
class Base(Device):
"""
Attributes
----------
name : str
internal name of the base (not necessarily identical to arlo)
status_interval: int
interval of status messages from generator (seconds)
"""
def __init__(self, arlo_base, status_interval):
super().__init__(arlo_base, status_interval)
logging.info(f"Base added: {self.name}")
# Distributes events to correct handler
async def on_event(self, attr, value):
match attr:
case 'activeMode':
self._state_event.set()
logging.info(f"{self.name} mode: {value}")
case _:
pass
def get_status(self):
return {
"mode": self._arlo.mode,
"siren": self._arlo.siren_state
}
async def mqtt_control(self, payload):
"""
Handles incoming MQTT commands
"""
handlers = {
'mode': self.set_mode,
'siren': self.set_siren
}
try:
payload = json.loads(payload)
for k, v in payload.items():
if k in handlers:
self.event_loop.run_in_executor(None, handlers[k], v)
except Exception:
logging.warning(f"{self.name}: Invalid data for MQTT control")
def set_mode(self, mode):
""""
Sets mode of Base Station
"""
try:
mode = mode.lower()
if mode not in self._arlo.available_modes:
raise ValueError
self._arlo.mode = mode
except (AttributeError, ValueError):
logging.warning(f"{self.name}: Invalid mode, ignored")
def set_siren(self, state):
"""
Sets siren (on/off/on with specified duration and volume)
"""
match state:
case 'on':
self._arlo.siren_on()
case 'off':
self._arlo.siren_off()
case dict():
try:
self._arlo.siren_on(**state)
except AttributeError:
logging.warning(f"{self.name}: Invalid siren arguments")
case _:
pass