1958c5ed2b8482cfe2498eb0583a0beba0479bee
[kiosk.git] / myq_renderer.py
1 #!/usr/bin/env python3
2
3 from aiohttp import ClientSession
4 import asyncio
5 import datetime
6 from dateutil.parser import parse
7 import pymyq
8 from typing import Dict, List
9
10 import constants
11 import file_writer
12 import renderer
13 import secrets
14 import utils
15
16
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 = None
21         self.last_update = None
22
23     def debug_prefix(self) -> str:
24         return "myq"
25
26     def periodic_render(self, key: str) -> bool:
27         if key == "Poll MyQ":
28             self.last_update = datetime.datetime.now()
29             return asyncio.run(self.poll_myq())
30         elif key == "Update Page":
31             return self.update_page()
32         else:
33             raise error("Unknown operaiton")
34
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
39             )
40             self.doors = myq.devices
41             return len(self.doors) > 0
42
43     def update_page(self) -> bool:
44         with file_writer.file_writer(constants.myq_pagename) as f:
45             f.write(
46                 f"""
47 <H1>Garage Door Status</H1>
48 <!-- Last updated at {self.last_update} -->
49 <HR>
50 <TABLE BORDER=0 WIDTH=99%>
51     <TR>
52 """
53             )
54             html = self.do_door("Near House")
55             if html is None:
56                 return False
57             f.write(html)
58
59             html = self.do_door("Middle Door")
60             if html is None:
61                 return False
62             f.write(html)
63             f.write(
64                 """
65     </TR>
66 </TABLE>"""
67             )
68         return True
69
70     def get_state_icon(self, state: str) -> str:
71         if state == "open":
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"
79         else:
80             return str(state) + ", an unknown state for the door."
81
82     def do_door(self, name: str) -> str:
83         for key in self.doors:
84             door = self.doors[key]
85             if door.name == name:
86                 j = self.doors[key].device_json
87                 state = j["state"]["door_state"]
88
89                 # "last_update": "2020-07-04T18:11:34.2981419Z"
90                 raw = j["state"]["last_update"]
91                 ts = parse(raw)
92                 tz_info = ts.tzinfo
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)
98                 width = 0
99                 if is_night and door.state == "open":
100                     color = "border-color: #ff0000;"
101                     width = 15
102                 else:
103                     color = ""
104                     width = 0
105                 return f"""
106 <TD WIDTH=49%>
107   <CENTER>
108   <FONT STYLE="font-size:26pt">{name}<BR>
109   <IMG SRC="{self.get_state_icon(state)}"
110        HEIGHT=250
111        STYLE="border-style: solid; border-width: {width}px; {color}">
112   <BR>
113   <B>{state}</B></FONT><BR>
114   for {duration}
115   </CENTER>
116 </TD>"""
117         return None
118
119
120 # Test
121 #x = garage_door_renderer({"Test": 1})
122 #x.periodic_render("Poll MyQ")
123 #x.periodic_render("Update Page")