-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
executable file
·276 lines (216 loc) · 9.52 KB
/
server.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
#!/bin/python3
# Author: Greg Hare
# https://github.com/greghare
# Created: 10/26/2022
import asyncio
import tornado.web
from tornado.options import define, options
from datetime import datetime
import datetime
from datetime import timedelta
from pytz import timezone
import time
import sqlite3
import requests
import json
import re
import pprint as pprint
import os
import configparser
config = configparser.ConfigParser()
config.read("/opt/nook-dashboard/app.conf");
HA_URL = config["home_assistant"]["url"]
HA_ACCESS_TOKEN = config["home_assistant"]["access_token"]
HA_API = HA_URL + "/api"
# Configure headers for Home Assistant API
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + HA_ACCESS_TOKEN
}
CALENDAR = config["home_assistant"]["calendar"]
define("port", default=8888, help="run on the given port", type=int)
con = sqlite3.connect("/opt/nook-dashboard/todolist.db")
cur = con.cursor()
requests.packages.urllib3.disable_warnings()
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write('> <a href="/dashboard" style="font-size: 25px">Dashboard</a><br>\
> <a href="/todo" style="font-size: 25px">To Do Manager</a>')
class DeleteToDo(tornado.web.RequestHandler):
def post(self):
# Delete todo item
id = self.get_argument("id")
cur.execute("DELETE FROM todo WHERE ID = ?", (id,))
con.commit()
print("Item " + id + " was deleted")
class ToDo(tornado.web.RequestHandler):
def get(self):
# Get to do list
res = cur.execute("SELECT ID, title, state FROM todo")
items = res.fetchall()
self.render("todo.html", title="To Do", items=items)
def post(self):
# Insert todo item
title = self.get_argument("title")
cur.execute("INSERT INTO todo VALUES (NULL, ?, false)", (title,))
con.commit()
print("New todo item posted")
self.redirect('/todo')
class Dashboard(tornado.web.RequestHandler):
# Return the high, and low temperatures from a list of temps
def get_high_low(self, temps):
return [max(temps), min(temps)]
def get_weather_icon(self, condition):
# The path is relative to the static_url
icon_path = {
"clear-night": "img/icons/weather-night.png",
"cloudy": "img/icons/weather-cloudy.png",
"fog": "img/icons/weather-fog.png",
"hail": "img/icons/weather-hail.png",
"lightning": "img/icons/weather-lightning.png",
"lightning-rainy": "img/icons/weather-lightning-rainy.png",
"partlycloudy": "img/icons/weather-partly-cloudy.png",
"pouring": "img/icons/weather-pouring.png",
"rainy": "img/icons/weather-rainy.png",
"snowy": "img/icons/weather-snowy.pngclear",
"snowy-rainy": "img/icons/weather-snowy-rainy.png",
"sunny": "img/icons/weather-sunny.png",
"windy": "img/icons/weather-windy.png",
"windy-variant": "img/icons/weather-windy-variant.png",
"exceptional": "img/icons/weather-sunny-alert.png"
}
return icon_path[condition]
def get(self):
##############################
# TO DO LIST
##############################
# Get to do list items
res = cur.execute("SELECT ID, title, state FROM todo")
items = res.fetchall()
##############################
# TODAYS EVENTS (FROM CALENDAR)
##############################
# Get calendar
today = datetime.datetime.now().strftime("%Y-%m-%d")
yesterday = (datetime.date.today() - timedelta(days = 1)).strftime("%Y-%m-%d")
start = today + "T00:00:00.000Z"
end = today + "T23:59:59.999Z"
response = requests.get(HA_API + "/calendars/" + CALENDAR + "?start="+start+"&end="+end, headers=headers, verify=False)
calendar = response.json()
##############################
# WEATHER
##############################
# Get weather
response = requests.get(HA_API + "/states/weather.openweathermap", headers=headers, verify=False)
weather = response.json()["attributes"]
weather_state = response.json()["state"]
weather_icon = self.get_weather_icon(weather_state)
# Build 5 day forecast of high/lows
forecast = weather["forecast"]
five_day = {}
prev_date = datetime.datetime.now().strftime("%Y-%m-%d")
prev_day = datetime.datetime.now().strftime("%a")
temps = []
index = 0
for f in forecast:
# Get current date
today = datetime.datetime.now().strftime("%Y-%m-%d")
# Get forecast datetime
dt = datetime.datetime.strptime(f["datetime"], '%Y-%m-%dT%H:%M:%S%z').astimezone(timezone("America/New_York"))
forecast_date = dt.strftime("%Y-%m-%d")
forecast_day = dt.strftime("%a")
# If new day, reset temps
if forecast_date != prev_date:
if len(temps) > 0:
high_low = self.get_high_low(temps)
five_day.update({index: {"dow": prev_day, "high": high_low[0], "low": high_low[1]}})
index += 1
temps = []
# If not today, add temperature to temps list
# if forecast_date != today:
temps.append(f["temperature"])
prev_date = forecast_date
prev_day = forecast_day
# Handle last date
high_low = self.get_high_low(temps)
five_day.update({index: {"dow": prev_day, "high": high_low[0], "low": high_low[1]}})
index += 1
##############################
# TIDE CONDITIONS
##############################
# Get tides
response = requests.get(HA_API + "/states/sensor.tide", headers=headers, verify=False)
tide = response.json()["attributes"]
format = "%Y-%m-%dT%H:%M"
low_tide_time = datetime.datetime.strptime(tide["low_tide_time"], format)
high_tide_time = datetime.datetime.strptime(tide["high_tide_time"], format)
low_tide_height = round(tide["low_tide_height"], 1)
high_tide_height = round(tide["high_tide_height"], 1)
ltt = low_tide_time.strftime("%A @ %-I:%M %p")
htt = high_tide_time.strftime("%A @ %-I:%M %p")
##############################
# SENSORS/DEVICE STATES
##############################
# Sensor state dictionary
sensors = []
# Get motion status
response = requests.get(HA_API + "/states/input_boolean.kitchen_motion", headers=headers, verify=False)
kitchen_motion = response.json()["state"]
sensors.append({ "name": "Kitchen Motion Sensor", "state": kitchen_motion, "icon": "motion-sensor"})
# Get patio light status
response = requests.get(HA_API + "/states/switch.patio_lights", headers=headers, verify=False)
patio_light = response.json()["state"]
sensors.append({ "name": "Patio Light", "state": patio_light, "icon": "string-lights"})
# Get door lock status
response = requests.get(HA_API + "/states/binary_sensor.front_door_lock", headers=headers, verify=False)
front_door = response.json()["state"]
sensors.append({ "name": "Front Door Lock", "state": front_door, "icon": "lock" })
response = requests.get(HA_API + "/states/binary_sensor.back_door_lock", headers=headers, verify=False)
back_door = response.json()["state"]
sensors.append({ "name": "Back Door Lock", "state": back_door, "icon": "lock" })
response = requests.get(HA_API + "/states/climate.living_room", headers=headers, verify=False)
thermostat = str(response.json()["attributes"]["current_temperature"])
sensors.append({ "name": "Living Room Thermostat", "state": thermostat, "icon": "thermostat" })
##############################
# RENDER DASHBOARD
##############################
self.render(
"dashboard.html",
title="Nook Dashboard",
items=items,
calendar=calendar,
weather_state=weather_state,
weather=weather,
weather_icon=weather_icon,
five_day=five_day,
htt=htt,
ltt=ltt,
hth=high_tide_height,
lth=low_tide_height,
sensors=sensors
)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/dashboard", Dashboard),
(r"/todo", ToDo),
(r"/deletetodo", DeleteToDo)
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
)
tornado.web.Application.__init__(self, handlers, **settings)
async def main():
# Create the todo list database
try:
cur.execute("CREATE TABLE IF NOT EXISTS todo(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title, state)")
except Exception as ex:
print("Exception occured: " + ex)
# Start the web server
app = tornado.httpserver.HTTPServer(Application())
app.listen(options.port)
await asyncio.Event().wait()
if __name__ == "__main__":
asyncio.run(main())