-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsilent_events.py
56 lines (45 loc) · 1.88 KB
/
silent_events.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
from mqtt.core.smqtt import SilentMQTT
class IOTLinkTopics(object):
BASE_COMMAND = 'iotlink/{domain}/{device_name}/commands'
SHUTDOWN = f'{BASE_COMMAND}/shutdown'
REBOOT = f'{BASE_COMMAND}/reboot'
SUSPEND = f'{BASE_COMMAND}/suspend'
MUTE = f'{BASE_COMMAND}/volume/mute'
class SilentEvents(object):
""" A wrapper class to manage devices through MQTT messages """
def __init__(self, broker, port=1883, username=None, password=None):
self.smqtt = SilentMQTT(broker, port=port, username=username, password=password)
def send_sol_packet(self, *devs, **kwargs):
"""
Sends MQTT shutdown message to the broker
:param devs: the list o devices to shutdown
:param kwargs['action']: if provided will change the command to send to the device. Allowed values are "suspend", "reboot", "shutdown"
"""
topic = IOTLinkTopics.SHUTDOWN
if 'action' in kwargs:
if kwargs['action'] == 'suspend':
topic = IOTLinkTopics.SUSPEND
elif kwargs['action'] == 'reboot':
topic = IOTLinkTopics.REBOOT
with self.smqtt as client:
for dev in devs:
client.publish(topic.format(
domain=dev['domain'],
device_name=dev['name']
))
def send_mute(self, dev, action=None):
"""
Seds MQTT mute message to the broker
:param dev: the device to mute/unmute
:param action: could be 'true', 'false', None. If None will toggle the status.
"""
if action not in ['true', 'false', None]:
action = None
with self.smqtt as client:
client.publish(
IOTLinkTopics.MUTE.format(
domain=dev['domain'],
device_name=dev['name']
),
payload=action
)