-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolightd.py
94 lines (77 loc) · 2.82 KB
/
solightd.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import homie
import json
import pigpio
import time
import logging
from dy01 import DY01
from dy05 import DY05
from dy08 import DY08
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
with open("solight_config.json") as config_file:
config = json.load(config_file)
pi = pigpio.pi()
dy01 = DY01(pi, config["transmitter_pin"])
dy05 = DY05(pi, config["transmitter_pin"], 0.5)
dy08 = DY08(pi, config["transmitter_pin"])
communicator = homie.Device(config["homie"])
communicator.setFirmware("SolightSockets", "1.0.0")
id_counter = 0
def newNodeId():
global id_counter
nodeId = "socket%d" % id_counter
id_counter += 1
return nodeId
def create_dy01_handler(name, sock):
def handle_command(property, payload):
logger.debug("Received message: '%s' for %s" % (payload, name))
if payload == "true":
dy01.send(sock, 1)
property.update("true")
else:
dy01.send(sock, 0)
property.update("false")
return handle_command
def create_dy05_handler(name, sock):
def handle_command(property, payload):
logger.debug("Received message: '%s' for %s" % (payload, name))
if payload == "true":
logger.debug("'%s' == 'true'")
dy05.send(sock, 1, 1)
property.update("true")
else:
logger.debug("'%s' != 'true'")
dy05.send(sock, 1, 0)
property.update("false")
return handle_command
if "DY01_sockets" in config:
for name, sock in config["DY01_sockets"].iteritems():
switch = communicator.addNode(newNodeId(), name, "switch")
on = switch.addProperty("on", name, None, "boolean", None, True)
on.settable(create_dy01_handler(name, sock))
on.update("false")
if "DY05_sockets" in config:
for name, sock in config["DY05_sockets"].iteritems():
switch = communicator.addNode(newNodeId(), name, "switch")
on = switch.addProperty("on", name, None, "boolean", None, True)
on.settable(create_dy05_handler(name, sock))
on.update("false")
if "DY08_sockets" in config:
for name, sock in config["DY08_sockets"].iteritems():
switch = communicator.addNode(newNodeId(), name, "switch")
def handle_command(property, payload):
logger.debug("Received message: '%s' for %s" % (payload, name))
if payload == "true":
dy08.send(sock, 1)
property.update("true")
else:
dy08.send(sock, 0)
property.update("false")
on = switch.addProperty("on", name, None, "boolean", None, True)
on.settable(create_dy08_handler(name, sock))
on.update("false")
communicator.setup()
# Yes, this is horrible. Unfortunately, the API doesn't provide
# a better way of blocking.
while True:
time.sleep(1)