-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_check.py
49 lines (40 loc) · 1.47 KB
/
health_check.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
#!/usr/bin/env python
import sys
import logging
from configparser import ConfigParser
from os.path import expanduser
from socket import gethostname
from eq3bt import Thermostat
from bluepy.btle import BTLEException
log = logging.getLogger("health_check")
def check_battery_statuses(config):
healthy = True
for room, addresses in config['radiators'].items():
addresses = addresses.split(",")
for addr in addresses:
try:
thermostat = Thermostat(addr)
thermostat.update()
except BTLEException:
log.error("Couldn't connect to %s %s!", room, addr)
healthy = False
continue
if thermostat.low_battery:
log.warning("Low battery reported by %s %s", room, addr)
healthy = False
return healthy
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)
healthy = check_battery_statuses(config)
if not healthy:
sys.exit(1)
if __name__ == '__main__':
main()