forked from jinglemansweep/MHub
/
byebyestandby.py
64 lines (43 loc) · 1.89 KB
/
byebyestandby.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
import datetime
import time
from socket import socket, AF_INET, SOCK_DGRAM
class Plugin(object):
""" ByeByeStandby online controller plugin """
def __init__(self, cfg, producer, logger):
""" Constructor """
self.name = "byebyestandby"
self.description = "ByeByeStandby home automation integration"
self.author = "MHub"
self.cfg = cfg
self.producer = producer
self.logger = logger
self.tasks = list()
def on_init(self):
""" Main plugin initialisation """
self.socket = socket(AF_INET, SOCK_DGRAM)
def on_message(self, data, message):
""" On AMQP message handler """
action, params = data.get("action"), data.get("params")
if action == "%s.switch" % (self.name):
device, state = params.get("device", None), params.get("state", None)
device = device.upper()
state_desc = "ON" if state else "OFF"
if device is not None and state is not None:
self.logger.info("ByeByeStandby Trigger: %s %s" % (device, state_desc))
self.switch(device, state)
if action == "%s.scene" % (self.name):
scenes = self.cfg.get("scenes", dict())
name = params.get("name")
if name in scenes:
self.logger.debug("Scene '%s' running" % (name))
scene = scenes.get(name)
for action in scene:
device, state = action.get("device"), action.get("state")
self.switch(device, state)
def switch(self, device, state):
""" BBS device switcher helper """
state = 1 if state else 0
h, u = device[0], device[1:]
cmd = "D:%i%s%02d:E" % (int(state), h.upper(), int(u))
self.socket.sendto(cmd, (self.cfg.get("host"), self.cfg.get("port")))
time.sleep(1)