-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradcontrold.py
More file actions
executable file
·70 lines (56 loc) · 2.05 KB
/
radcontrold.py
File metadata and controls
executable file
·70 lines (56 loc) · 2.05 KB
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
#!/usr/bin/env python
import sys
import logging
from configparser import ConfigParser
from os.path import expanduser
from socket import gethostname
from time import sleep
from eq3bt import Thermostat, Mode
from bluepy.btle import BTLEException
from mqttwrapper import run_script
log = logging.getLogger("radcontrold")
def callback(topic, payload, config):
log.debug("%s %s", topic, payload)
room = topic.split("/")[2]
mode = {
b'0': Mode.Closed,
b'1': Mode.Open,
}.get(payload)
if mode is None:
log.warning("Ignoring invalid payload on %s", topic)
return
addresses = config['radiators'].get(room, "")
if not addresses:
# Control message is for a radiator we're not responsible for.
log.debug("No EQ3 addresses in config for %s", room)
return
success = True
for address in addresses.split(","):
for attempt in range(10):
try:
Thermostat(address).mode = mode
log.info("Set %s in %s to %s", address, room, mode)
break
except BTLEException:
log.warning("Couldn't set mode %s for %s in %s", mode, address, room)
sleep(1)
else:
success = False
# Only post acknowledgment to MQTT topic if all thermostats were controlled.
if success:
return [
("{}/ack".format(topic), payload)
]
def main():
formatter = "[%(asctime)s] %(name)s %(levelname)s - %(message)s"
logging.basicConfig(level=logging.DEBUG, format=formatter)
logging.getLogger('eq3bt').setLevel(logging.ERROR)
hostname = gethostname().split(".")[0]
config = ConfigParser()
config.read(expanduser("~/.config/radcontrold/{}.ini".format(hostname)))
if not config.has_section('radiators') or len(config['radiators']) == 0:
log.warning("No config for {}, exiting.".format(hostname))
sys.exit(0)
run_script(callback, broker=config['mqtt']['broker'], topics=['control/radiator/+/active'], config=config)
if __name__ == '__main__':
main()