3 from aiohttp import ClientSession
6 from dateutil.parser import parse
8 from typing import Dict, List
17 class garage_door_renderer(renderer.debuggable_abstaining_renderer):
18 def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
19 super(garage_door_renderer, self).__init__(name_to_timeout_dict, False)
21 self.last_update = None
23 def debug_prefix(self) -> str:
26 def periodic_render(self, key: str) -> bool:
28 self.last_update = datetime.datetime.now()
29 return asyncio.run(self.poll_myq())
30 elif key == "Update Page":
31 return self.update_page()
33 raise error("Unknown operaiton")
35 async def poll_myq(self) -> bool:
36 async with ClientSession() as websession:
37 myq = await pymyq.login(
38 secrets.myq_username, secrets.myq_password, websession
40 self.doors = myq.devices
41 return len(self.doors) > 0
43 def update_page(self) -> bool:
44 with file_writer.file_writer(constants.myq_pagename) as f:
47 <H1>Garage Door Status</H1>
48 <!-- Last updated at {self.last_update} -->
50 <TABLE BORDER=0 WIDTH=99%>
54 html = self.do_door("Near House")
59 html = self.do_door("Middle Door")
70 def get_state_icon(self, state: str) -> str:
72 return "/icons/garage_open.png"
73 elif state == "closed":
74 return "/icons/garage_closed.png"
75 elif state == "opening":
76 return "/icons/garage_opening.png"
77 elif state == "closing":
78 return "/icons/garage_closing.png"
80 return str(state) + ", an unknown state for the door."
82 def do_door(self, name: str) -> str:
83 for key in self.doors:
84 door = self.doors[key]
86 j = self.doors[key].device_json
87 state = j["state"]["door_state"]
89 # "last_update": "2020-07-04T18:11:34.2981419Z"
90 raw = j["state"]["last_update"]
93 now = datetime.datetime.now(tz_info)
94 delta = (now - ts).total_seconds()
95 now = datetime.datetime.now()
96 is_night = now.hour <= 7 or now.hour >= 21
97 duration = utils.describe_duration_briefly(delta)
99 if is_night and door.state == "open":
100 color = "border-color: #ff0000;"
108 <FONT STYLE="font-size:26pt">{name}<BR>
109 <IMG SRC="{self.get_state_icon(state)}"
111 STYLE="border-style: solid; border-width: {width}px; {color}">
113 <B>{state}</B></FONT><BR>
121 #x = garage_door_renderer({"Test": 1})
122 #x.periodic_render("Poll MyQ")
123 #x.periodic_render("Update Page")