3 from aiohttp import ClientSession
6 from dateutil.parser import parse
7 import pymyq # type: ignore
8 from typing import Dict, List, Optional
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)
20 self.doors: Optional[Dict] = None
21 self.last_update: Optional[datetime.datetime] = 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 Exception("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 assert(self.doors is not None)
42 return len(self.doors) > 0
44 def update_page(self) -> bool:
45 with file_writer.file_writer(constants.myq_pagename) as f:
48 <H1>Garage Door Status</H1>
49 <!-- Last updated at {self.last_update} -->
51 <TABLE BORDER=0 WIDTH=99%>
55 html = self.do_door("Near House")
60 html = self.do_door("Middle Door")
71 def get_state_icon(self, state: str) -> str:
73 return "/kiosk/pages/images/garage_open.png"
74 elif state == "closed":
75 return "/kiosk/pages/images/garage_closed.png"
76 elif state == "opening":
77 return "/kiosk/pages/images/garage_opening.png"
78 elif state == "closing":
79 return "/kiosk/pages/images/garage_closing.png"
81 return str(state) + ", an unknown state for the door."
83 def do_door(self, name: str) -> Optional[str]:
84 if self.doors is None:
86 for key in self.doors:
87 door = self.doors[key]
89 j = self.doors[key].device_json
90 state = j["state"]["door_state"]
92 # "last_update": "2020-07-04T18:11:34.2981419Z"
93 raw = j["state"]["last_update"]
96 now = datetime.datetime.now(tz_info)
97 delta = (now - ts).total_seconds()
98 now = datetime.datetime.now()
99 is_night = now.hour <= 7 or now.hour >= 21
100 duration = utils.describe_duration_briefly(int(delta))
102 if is_night and door.state == "open":
103 color = "border-color: #ff0000;"
111 <FONT STYLE="font-size:26pt">{name}<BR>
112 <IMG SRC="{self.get_state_icon(state)}"
114 STYLE="border-style: solid; border-width: {width}px; {color}">
116 <B>{state}</B></FONT><BR>
124 #x = garage_door_renderer({"Test": 1})
125 #x.periodic_render("Poll MyQ")
126 #x.periodic_render("Update Page")