forked from jalmeroth/homie-python
-
Notifications
You must be signed in to change notification settings - Fork 2
/
relay_switch_with_config.py
executable file
·46 lines (38 loc) · 1.01 KB
/
relay_switch_with_config.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
#!/usr/bin/env python
import time
import homie
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
config = {
"HOST": "iot.eclipse.org",
"PORT": 1883,
"KEEPALIVE": 10,
"USERNAME": "",
"PASSWORD": "",
"CA_CERTS": "",
"DEVICE_ID": "xxxxxxxx",
"DEVICE_NAME": "xxxxxxxx",
"TOPIC": "homie"
}
Homie = homie.Homie(config)
switchNode = Homie.Node("switch", "switch")
def switchOnHandler(mqttc, obj, msg):
payload = msg.payload.decode("UTF-8").lower()
if payload == 'true':
logger.info("Switch: ON")
switchNode.setProperty("on").send("true")
else:
logger.info("Switch: OFF")
switchNode.setProperty("on").send("false")
def main():
Homie.setFirmware("relay-switch", "1.0.0")
switchNode.advertise("on").settable(switchOnHandler)
Homie.setup()
while True:
time.sleep(1)
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, SystemExit):
logger.info("Quitting.")