1 #!/usr/local/bin/python3.7
4 from aiohttp import ClientSession
8 from dateutil.parser import parse
13 class garage_door_renderer(renderer.debuggable_abstaining_renderer):
14 def __init__(self, name_to_timeout_dict):
15 super(garage_door_renderer, self).__init__(name_to_timeout_dict, False)
18 def debug_prefix(self):
21 def periodic_render(self, key):
23 asyncio.get_event_loop().run_until_complete(self.poll_myq())
24 elif key == "Update Page":
25 return self.update_page()
27 raise error("Unknown operaiton")
29 async def poll_myq(self):
30 async with ClientSession() as websession:
31 myq = await pymyq.login(secrets.myq_username,
34 self.doors = myq.devices
36 def update_page(self):
37 f = file_writer.file_writer(constants.myq_pagename)
38 now = datetime.datetime.now()
40 <H1>Garage Door Status</H1>
41 <!-- Last updated at %s -->
43 <TABLE BORDER=0 WIDTH=99%%>
46 html = self.do_door("Near House")
51 html = self.do_door("Middle Door")
61 def get_state_icon(self, state):
63 return "/icons/garage_open.png"
64 elif state == "closed":
65 return "/icons/garage_closed.png"
66 elif state == "opening":
67 return "/icons/garage_opening.png"
68 elif state == "closing":
69 return "/icons/garage_closing.png"
71 return str(state) + ", an unknown state for the door."
73 def do_door(self, name):
74 for key in self.doors:
75 door = self.doors[key]
77 j = self.doors[key].json
78 state = j["state"]["door_state"]
80 # "last_update": "2020-07-04T18:11:34.2981419Z"
81 raw = j["state"]["last_update"]
84 now = datetime.datetime.now(tz_info)
85 delta = (now - ts).total_seconds()
86 now = datetime.datetime.now()
87 is_night = now.hour <= 7 or now.hour >= 21
88 days = divmod(delta, constants.seconds_per_day)
89 hours = divmod(days[1], constants.seconds_per_hour)
90 minutes = divmod(hours[1], constants.seconds_per_minute)
92 if is_night and door.get_status() == "open":
93 color = "border-color: #ff0000;"
101 <FONT STYLE="font-size:26pt">%s<BR>
104 STYLE="border-style: solid; border-width: %dpx; %s">
107 for %d day(s), %02d:%02d.
110 self.get_state_icon(state),
114 days[0], hours[0], minutes[0])
118 #x = garage_door_renderer({"Test" : 1})
119 #x.periodic_render("Poll MyQ")
120 #x.periodic_render("Update Page")