This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmip.py
138 lines (126 loc) · 7.08 KB
/
hmip.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from calendar import timegm
from datetime import datetime
from os import (
makedirs,
path,
walk,
)
import homematicip
import rrdtool
from flask import current_app as app
from homematicip.device import (
ShutterContact,
TemperatureHumiditySensorOutdoor,
WallMountedThermostatPro,
)
from homematicip.home import Home
class HmIP(object):
def __init__(self):
self.config = homematicip.load_config_file(config_file=path.join(app.instance_path, 'config.ini'))
self.home = Home()
self.home.set_auth_token(self.config.auth_token)
self.home.init(self.config.access_point)
def fetch_metrics(self):
if not self.home.get_current_state():
self.home.init(self.config.access_point)
self.home.get_current_state()
for group in self.home.groups:
if group.groupType == 'META':
for device in group.devices:
if isinstance(device, WallMountedThermostatPro):
self.__collect_thermostat_metrics(group.label, device)
elif isinstance(device, ShutterContact):
self.__collect_shutter_metrics(group.label, device)
elif isinstance(device, TemperatureHumiditySensorOutdoor):
self.__collect_thermostat_outdoor_metrics(group.label, device)
def __collect_shutter_metrics(self, room, device):
rrd = path.join(app.instance_path, 'rrds', room, f'{device.label}.rrd')
if not path.exists(rrd):
makedirs(path.join(app.instance_path, 'rrds', room), mode=0o0750, exist_ok=True)
rrdtool.create(rrd, '--start', 'now', '--step', '180', 'DS:state:GAUGE:540:0:1', 'RRA:AVERAGE:0.5:1:360000')
app.logger.debug('room: {}, label: {}, windowState: {}'.format(room, device.label, device.windowState))
try:
rrdtool.update(path.join(app.instance_path, 'rrds', room, f'{device.label}.rrd'),
f'N: {self.__window_state(device.windowState)}')
except rrdtool.OperationalError:
app.logger.error('room: {}, label: {}, windowState: {}'.format(room, device.label, device.windowState))
@staticmethod
def __collect_thermostat_metrics(room, device):
rrd = path.join(app.instance_path, 'rrds', room, f'{device.label}.rrd')
if not path.exists(rrd):
makedirs(path.join(app.instance_path, 'rrds', room), mode=0o0750, exist_ok=True)
rrdtool.create(rrd, '--start', 'now', '--step', '180', 'DS:actualtemperature:GAUGE:540:0:50',
'DS:settemperature:GAUGE:540:0:40', 'DS:humidity:GAUGE:540:0:100', 'DS:vapor:GAUGE:540:0:100',
'RRA:AVERAGE:0.5:1:360000')
app.logger.debug(f'room: {room}, label: {device.label}, temperature_actual: {device.actualTemperature}, temperature_setpoint: '
f'{device.setPointTemperature}, humidity_actual: {device.humidity} vaporAmount: {device.vaporAmount}')
try:
rrdtool.update(path.join(app.instance_path, 'rrds', room, f'{device.label}.rrd'), f'N: {device.actualTemperature}:'
f'{device.setPointTemperature}:'
f'{device.humidity}:{device.vaporAmount}')
except rrdtool.OperationalError:
app.logger.error(f'room: {room}, label: {device.label}, temperature_actual: {device.actualTemperature}, temperature_setpoint: '
f'{device.setPointTemperature}, humidity_actual: {device.humidity} vaporAmount: {device.vaporAmount}')
@staticmethod
def __collect_thermostat_outdoor_metrics(room, device):
rrd = path.join(app.instance_path, 'rrds', room, f'{device.label}.rrd')
if not path.exists(rrd):
makedirs(path.join(app.instance_path, 'rrds', room), mode=0o0750, exist_ok=True)
rrdtool.create(rrd, '--start', 'now', '--step', '180', 'DS:actualtemperature:GAUGE:540:-20:55', 'DS:humidity:GAUGE:540:0:100',
'DS:vapor:GAUGE:540:0:100', 'RRA:AVERAGE:0.5:1:360000')
app.logger.debug(
f'room: {room}, label: {device.label}, temperature_actual: {device.actualTemperature}, humidity_actual: {device.humidity} '
f'vaporAmount: {device.vaporAmount}')
try:
rrdtool.update(path.join(app.instance_path, 'rrds', room, f'{device.label}.rrd'), f'N: {device.actualTemperature}:'
f'{device.humidity}:{device.vaporAmount}')
except rrdtool.OperationalError:
app.logger.error(
f'room: {room}, label: {device.label}, temperature_actual: {device.actualTemperature}, humidity_actual: {device.humidity} '
f'vaporAmount: {device.vaporAmount}')
@staticmethod
def __window_state(state):
return 1 if state == 'OPEN' else 0
def convert_to_time_ms(self, timestamp):
return 1000 * self.convert_to_time_s(timestamp)
@staticmethod
def convert_to_time_s(timestamp):
return timegm(datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ').timetuple())
def get_metric_names(self):
ret = set()
for group in self.home.groups:
if group.groupType == 'META':
for device in group.devices:
if isinstance(device, WallMountedThermostatPro):
ret.add('actualtemperature')
ret.add('settemperature')
ret.add('humidity')
ret.add('vapor')
elif isinstance(device, ShutterContact):
ret.add('state')
elif isinstance(device, TemperatureHumiditySensorOutdoor):
ret.add('actualtemperature')
ret.add('humidity')
ret.add('vapor')
return ret
def get_metrics(self, start, end, resolution, metrics):
ret = []
for root, dirs, files in walk(path.join(app.instance_path, 'rrds')):
for file in files:
if file.endswith('.rrd'):
rrd = path.join(root, file)
app.logger.debug(f'open rrd: {rrd}')
fetch = rrdtool.fetch(rrd, 'AVERAGE', '--resolution', resolution, '--start', str(self.convert_to_time_s(start)),
'--end', str(self.convert_to_time_s(end)))
for num, name in enumerate(fetch[1]):
if name in metrics:
datapoints = []
ts = fetch[0][0]
for dp in fetch[2]:
datapoints.append([dp[num], ts * 1000])
ts += fetch[0][2]
ret.append({
'target': file.split('.')[0],
'datapoints': datapoints
})
return ret