This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathhapush.py
executable file
·352 lines (302 loc) · 10.2 KB
/
hapush.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/python3
import json
import traceback
import re
import requests
from configobj import ConfigObj
import datetime
import argparse
import time
from daemonize import Daemonize
import logging
import glob
import os.path
import math
import pprint
from sseclient import SSEClient
from logging.handlers import RotatingFileHandler
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
logger = logging.getLogger(__name__)
ha_url = ""
ha_key = ""
dash_host = ""
dash_dir = ""
widgets = {}
monitored_files = {}
def roundup(x):
return int(math.ceil(x / 10.0)) * 10
def call_ha(widget_id, values):
global logger
url = "http://" + dash_host + "/widgets/" + widget_id
logger.debug(url)
logger.debug(str(values))
try:
response = requests.post(url, verify = False, json = values)
except requests.exceptions.RequestException as e:
logger.warn("Unexpected error calling Dashing: %s", e)
def process_message(msg):
global logger
global widgets
if msg.data == "ping":
return
# Check to see if dashboards have changed
readDashboards()
try:
data = json.loads(msg.data)
logger.debug("Event type:{}:".format(data['event_type']))
if data['event_type'] == "state_changed":
try:
entity_id = data['data']['new_state']['entity_id']
logger.debug("Entity ID:{}:".format(entity_id))
parts = entity_id.split(".")
type = parts[0]
widget_id = parts[1]
# Check to see if we have the widget registered and also where to send the notification
if type in widgets and widget_id in widgets[type]:
for widget in widgets[type][widget_id]:
send_type = widgets[type][widget_id][widget]
state = data['data']['new_state']
# Send it
dashboard_update(widget, send_type, state)
except: TypeError
except ValueError as e:
logger.warn("Malformed JSON: %s", e)
logger.warn("%s", msg)
except:
logger.warn("Unexpected error:")
logger.warn('-'*60)
logger.warn(traceback.format_exc())
logger.warn('-'*60)
def dashboard_update(widget_id, type, state):
try:
if type == "switch":
values = {"state": state['state']}
logger.info("switch." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "group":
values = {"state": state['state']}
logger.info("group." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "device_tracker":
if state['state'] == "not_home":
nstate = "away"
else:
nstate = state['state']
values = {"state": nstate.upper()}
logger.info("devicetracker." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "input_select":
values = {"value": state['state']}
logger.info("input_select." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "input_boolean":
values = {"state": state['state']}
logger.info("input_boolean." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "binary_sensor":
values = {"state": state['state']}
logger.info("binary_sensor." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "sensor":
values = {"value": state['state']}
logger.info("sensor." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "light":
astate = state['state']
try:
brightness = roundup(int(state['attributes']['brightness'])/2.55)
except KeyError:
brightness = 30
astate = 'off'
values = {"state": astate, "level": brightness}
logger.info("switch." + widget_id + " -> state = " + astate + ", brightness = " + str(brightness))
call_ha(widget_id, values)
elif type == "garage_door":
values = {"state": state['state']}
logger.info("garage_door." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "cover":
values = {"state": state['state']}
logger.info("cover." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "lock":
values = {"state": state['state']}
logger.info("lock." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "alarm_control_panel":
values = {"value": state['state']}
logger.info("alarm_control_panel." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
elif type == "media_player":
logger.info("media_player." + widget_id + " -> " + str(state))
call_ha(widget_id, state)
elif type == "script":
values = {"mode": state['state']}
logger.info("script." + widget_id + " -> " + state['state'])
call_ha(widget_id, values)
except:
logger.warn("Unexpected error:")
logger.warn('-'*60)
logger.warn(traceback.format_exc())
logger.warn('-'*60)
def translate_view(view):
views = {
"Hadevicetracker": "device_tracker",
"Hagarage": "garage_door",
"Hacover": "cover",
"Halock": "lock",
"Haalarmstatus": "alarm_control_panel",
"Hainputboolean": "input_boolean",
"Halux": "sensor",
"Hascene": "scene",
"Haswitch": "switch",
"Hagroup": "group",
"Hadimmer": "light",
"Hahumidity": "sensor",
"Hainputselect": "input_select",
"Hamotion": "binary_sensor",
"Habinary": "binary_sensor",
"Hamode": "script",
"Hatemp": "sensor",
"Hasensor": "sensor",
"Hameter": "sensor",
"Hamediaplayer": "media_player"
}
if view in views:
return views[view]
else:
return "none"
def readDash(file):
global widgets
logger.info("Reading dashboard: %s", file)
div = re.compile('<div.+>')
id = re.compile('data-id\s*?=\s*?"(.+?)"')
input = re.compile('data-input\s*?=\s*?"(.+?)"')
view = re.compile('data-view\s*?=\s*?"(.+?)"')
with open(file) as f:
for line in f:
data_id = ""
data_input = ""
data_view = ""
# Find a <div>
m1 = div.search(line)
if m1:
# Check for data-id
m2 = id.search(m1.group())
if m2:
# grab data-id
data_id = m2.group(1)
# grab data-view
m3 = view.search(m1.group())
if m3:
data_view = (translate_view(m3.group(1)))
# grab data-input
m4 = input.search(m1.group())
if m4:
data_input = m4.group(1)
if data_id:
if not data_view in widgets:
widgets[data_view] = {}
if (data_input):
# If we have a data-input this is a special case of a script that shows status based on an input_select
# We need to register it as interested in events from that input select
if not "input_select" in widgets:
widgets["input_select"] = {}
if not data_input in widgets["input_select"]:
widgets["input_select"][data_input] = {}
widgets["input_select"][data_input][data_id] = data_view
else:
if not data_id in widgets[data_view]:
widgets[data_view][data_id] = {}
widgets[data_view][data_id][data_id] = data_view
def readDashboards():
global monitored_files
global dash_dir
found_files = glob.glob(os.path.join(dash_dir, '*.erb'))
for file in found_files:
if file == "{0}/layout.erb".format(dash_dir):
continue
modified = os.path.getmtime(file)
if file in monitored_files:
if monitored_files[file] < modified:
readDash(file)
monitored_files[file] = modified
else:
readDash(file)
monitored_files[file] = modified
def run():
global ha_url
global ha_key
global dash_host
global logger
while True:
try:
headers = {'Content-Type' : 'application/json'}
if ha_key != "":
headers['x-ha-access'] = ha_key
messages = SSEClient(ha_url + "/api/stream", verify = False, headers = headers, retry = 3000)
for msg in messages:
process_message(msg)
except requests.exceptions.ConnectionError:
logger.warning("Unable to connect to Home Assistant, retrying in 5 seconds")
except:
logger.fatal("Unexpected error:")
logger.fatal('-'*60)
logger.fatal(traceback.format_exc())
logger.fatal('-'*60)
time.sleep(5)
def main():
global ha_url
global ha_key
global dash_host
global dash_dir
global logger
# Get command line args
parser = argparse.ArgumentParser()
parser.add_argument("config", help="full path to config file", type=str)
parser.add_argument("-d", "--daemon", help="run as a background process", action="store_true")
parser.add_argument("-p", "--pidfile", help="full path to PID File", default = "/tmp/hapush.pid")
parser.add_argument("-D", "--debug", help="debug level", default = "INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"])
args = parser.parse_args()
config_file = args.config
isdaemon = args.daemon
# Read Config File
config = ConfigObj(config_file, file_error=True)
ha_url = config['ha_url']
if 'ha_key' in config:
ha_key = config['ha_key']
dash_host = config['dash_host']
dash_dir = config['dash_dir']
logfile = config['logfile']
# Setup Logging
logger = logging.getLogger(__name__)
numeric_level = getattr(logging, args.debug, None)
logger.setLevel(numeric_level)
logger.propagate = False
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
# Send to file if we are daemonizing, else send to console
if isdaemon:
fh = RotatingFileHandler(logfile, maxBytes=1000000, backupCount=3)
fh.setLevel(numeric_level)
fh.setFormatter(formatter)
logger.addHandler(fh)
else:
ch = logging.StreamHandler()
ch.setLevel(numeric_level)
ch.setFormatter(formatter)
logger.addHandler(ch)
# Read dashboards
readDashboards()
# Start main loop
if isdaemon:
keep_fds = [fh.stream.fileno()]
pid = args.pidfile
daemon = Daemonize(app="hapush", pid=pid, action=run, keep_fds=keep_fds)
daemon.start()
while True:
time.sleep(1)
else:
run()
if __name__ == "__main__":
main()