/
evohome_
executable file
·185 lines (145 loc) · 5.21 KB
/
evohome_
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
"""
Plugin to show setpoint and measured temperature of zone.
Also show the outside temperature based on location
To make this plugin work make sure you install the following:
evohomeclient http://evohome-client.readthedocs.org/en/latest/
#pip install ./evohome-client
python-weather-api https://code.google.com/p/python-weather-api/
#pip install --allow-all-external pywapi --allow-unverified pywapi
ex: /etc/munin/plugin-conf.d/plugins
[evohome*]
user root
To activate the plugin make a symlink
#ln -s /usr/local/share/munin/plugins/evohome_ /usr/local/etc/munin/plugins/evohome_Zonename
For zone names containing spaces, replace the spaces with underscores in the symlink name
"""
import os
import logging
import time
import sys
import json
import io
from pprint import pprint
import pywapi
from munin import MuninPlugin
from evohomeclient import EvohomeClient
class EvohomeMuninPlugin(MuninPlugin):
"""
Set the below values to your own account
Look up your location id at weather.com
"""
username = 'user@mail.com'
password = 'yoursecretpassword'
location = 'NLXX0010'
args = "--base 1000" # -l 0 --upper-limit 40"
vlabel = "temperature"
scale = True
fields = (
('temp', dict(
label="Temperature",
info="Temperature in Room",
type="GAUGE",
max="50"
)),
('setpoint', dict(
label="Temperature SP",
info="Temperature Setpoint in Room",
type="GAUGE",
max="35",
min="5"
)),
('outside_temperature', dict(
label="Outside Temperature",
info="Temperature outside at location",
type="GAUGE",
)),
)
zonename_in_args = True
category = "domotic"
# This is the file which has the zone data
PERSISTENT_STORAGE = "/tmp/munin-zone-data.json"
WEATHER_DATA = "/tmp/munin-outside-temperature.json"
def __init__(self):
super(EvohomeMuninPlugin, self).__init__()
self.zonename = (sys.argv[0].split('_', 1)[-1].replace('_',' ')
if self.zonename_in_args else None)
# write zone info to JSON file
# if the file is not older than 5 minutes we don't need to fetch the data
def write_zone_info(self):
# Read or construct data persitent data
try:
data = json.load(io.open(self.PERSISTENT_STORAGE, "rb"))
except:
data = []
try:
stat = os.stat(self.PERSISTENT_STORAGE)
except OSError:
age_minutes = 10
else:
age_minutes = (time.time() - stat.st_mtime) / 60
if age_minutes >= 5:
client = EvohomeClient(self.username, self.password)
for device in client.temperatures():
data.append(device)
with io.open(self.PERSISTENT_STORAGE, "wb") as f:
json.dump(data, f)
f.close()
# write weather data to JSON file
# if the file is not older than 15 minutes we don't need to fetch the data
def write_weather_info(self):
try:
data = json.load(io.open(self.WEATHER_DATA, "rb"))
except:
data = []
try:
stat = os.stat(self.WEATHER_DATA)
except OSError:
age_minutes = 20
else:
age_minutes = (time.time() - stat.st_mtime) / 60
if age_minutes >= 15:
weather_com_result = pywapi.get_weather_from_weather_com(self.location, units = 'metric' )
weather = dict()
temp = weather_com_result['current_conditions']['temperature']
station = weather_com_result['current_conditions']['station']
weather['temperature'] = temp
weather['station'] = station
with io.open(self.WEATHER_DATA, "wb") as f:
json.dump(weather, f)
f.close()
# Get room temperature
def getzoneinfo(self):
self.write_zone_info()
self.write_weather_info()
# Read zone data
try:
data = json.load(io.open(self.PERSISTENT_STORAGE, "rb"))
except:
data = []
# Read weather data
try:
data_weather = json.load(io.open(self.WEATHER_DATA, "rb"))
except:
data_weather = []
outside_temperature = data_weather['temperature']
# get the info from the requested zone
for i in data:
if i['name'] == self.zonename:
temperature = i['temp']
setpoint = i['setpoint']
return temperature, setpoint,outside_temperature
@property
def title(self):
return "Room temperature %s" % self.zonename
def execute(self):
temperature, setpoint, outside_temperature = self.getzoneinfo()
values = dict()
values['temp'] = temperature
values['setpoint'] = setpoint
values['outside_temperature'] = outside_temperature
#print "temp.value %s" % temperature
#print "setpoint.value %s" % setpoint
return values
if __name__ == "__main__":
EvohomeMuninPlugin().run()