projects
/
kiosk.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Move the kiosk server to another machine; changes needed to make it work there.
[kiosk.git]
/
myq_renderer.py
diff --git
a/myq_renderer.py
b/myq_renderer.py
index ca405346dc10f88d98689886ae198ecd98195e8b..0ee104b55117a002b50476ed6f6d8d196c0b6411 100644
(file)
--- a/
myq_renderer.py
+++ b/
myq_renderer.py
@@
-1,80
+1,92
@@
-import pymyq
+#!/usr/bin/env python3
+
from aiohttp import ClientSession
import asyncio
from aiohttp import ClientSession
import asyncio
-import constants
import datetime
from dateutil.parser import parse
import datetime
from dateutil.parser import parse
+import pymyq # type: ignore
+from typing import Dict, List, Optional
+
+import constants
import file_writer
import renderer
import secrets
import file_writer
import renderer
import secrets
+import utils
+
class garage_door_renderer(renderer.debuggable_abstaining_renderer):
class garage_door_renderer(renderer.debuggable_abstaining_renderer):
- def __init__(self, name_to_timeout_dict
)
:
+ def __init__(self, name_to_timeout_dict
: Dict[str, int]) -> None
:
super(garage_door_renderer, self).__init__(name_to_timeout_dict, False)
super(garage_door_renderer, self).__init__(name_to_timeout_dict, False)
- self.doors = None
- self.last_update = None
+ self.doors
: Optional[Dict]
= None
+ self.last_update
: Optional[datetime.datetime]
= None
- def debug_prefix(self):
+ def debug_prefix(self)
-> str
:
return "myq"
return "myq"
- def periodic_render(self, key
)
:
+ def periodic_render(self, key
: str) -> bool
:
if key == "Poll MyQ":
self.last_update = datetime.datetime.now()
return asyncio.run(self.poll_myq())
elif key == "Update Page":
return self.update_page()
else:
if key == "Poll MyQ":
self.last_update = datetime.datetime.now()
return asyncio.run(self.poll_myq())
elif key == "Update Page":
return self.update_page()
else:
- raise
error
("Unknown operaiton")
+ raise
Exception
("Unknown operaiton")
- async def poll_myq(self):
+ async def poll_myq(self)
-> bool
:
async with ClientSession() as websession:
async with ClientSession() as websession:
- myq = await pymyq.login(
secrets.myq_username,
- secrets.myq_password,
-
websession
)
+ myq = await pymyq.login(
+ secrets.myq_username, secrets.myq_password, websession
+ )
self.doors = myq.devices
self.doors = myq.devices
+ assert(self.doors is not None)
return len(self.doors) > 0
return len(self.doors) > 0
- def update_page(self):
- f = file_writer.file_writer(constants.myq_pagename)
- f.write("""
+ def update_page(self) -> bool:
+ with file_writer.file_writer(constants.myq_pagename) as f:
+ f.write(
+ f"""
<H1>Garage Door Status</H1>
<H1>Garage Door Status</H1>
-<!-- Last updated at
%s
-->
+<!-- Last updated at
{self.last_update}
-->
<HR>
<HR>
-<TABLE BORDER=0 WIDTH=99%%>
- <TR>
-""" % self.last_update)
- html = self.do_door("Near House")
- if html == None:
- return False
- f.write(html)
+<TABLE BORDER=0 WIDTH=99%>
+ <TR>
+"""
+ )
+ html = self.do_door("Near House")
+ if html is None:
+ return False
+ f.write(html)
- html = self.do_door("Middle Door")
- if html == None:
- return False
- f.write(html)
- f.write("""
- </TR>
-</TABLE>""")
- f.close()
+ html = self.do_door("Middle Door")
+ if html is None:
+ return False
+ f.write(html)
+ f.write(
+ """
+ </TR>
+</TABLE>"""
+ )
return True
return True
- def get_state_icon(self, state
)
:
+ def get_state_icon(self, state
: str) -> str
:
if state == "open":
if state == "open":
- return "/
icon
s/garage_open.png"
+ return "/
kiosk/pages/image
s/garage_open.png"
elif state == "closed":
elif state == "closed":
- return "/
icon
s/garage_closed.png"
+ return "/
kiosk/pages/image
s/garage_closed.png"
elif state == "opening":
elif state == "opening":
- return "/
icon
s/garage_opening.png"
+ return "/
kiosk/pages/image
s/garage_opening.png"
elif state == "closing":
elif state == "closing":
- return "/
icon
s/garage_closing.png"
+ return "/
kiosk/pages/image
s/garage_closing.png"
else:
return str(state) + ", an unknown state for the door."
else:
return str(state) + ", an unknown state for the door."
- def do_door(self, name):
+ def do_door(self, name: str) -> Optional[str]:
+ if self.doors is None:
+ return None
for key in self.doors:
door = self.doors[key]
if door.name == name:
for key in self.doors:
door = self.doors[key]
if door.name == name:
- j = self.doors[key].json
+ j = self.doors[key].
device_
json
state = j["state"]["door_state"]
# "last_update": "2020-07-04T18:11:34.2981419Z"
state = j["state"]["door_state"]
# "last_update": "2020-07-04T18:11:34.2981419Z"
@@
-85,9
+97,7
@@
class garage_door_renderer(renderer.debuggable_abstaining_renderer):
delta = (now - ts).total_seconds()
now = datetime.datetime.now()
is_night = now.hour <= 7 or now.hour >= 21
delta = (now - ts).total_seconds()
now = datetime.datetime.now()
is_night = now.hour <= 7 or now.hour >= 21
- days = divmod(delta, constants.seconds_per_day)
- hours = divmod(days[1], constants.seconds_per_hour)
- minutes = divmod(hours[1], constants.seconds_per_minute)
+ duration = utils.describe_duration_briefly(int(delta))
width = 0
if is_night and door.state == "open":
color = "border-color: #ff0000;"
width = 0
if is_night and door.state == "open":
color = "border-color: #ff0000;"
@@
-95,26
+105,22
@@
class garage_door_renderer(renderer.debuggable_abstaining_renderer):
else:
color = ""
width = 0
else:
color = ""
width = 0
- return """
-<TD WIDTH=49%
%
>
+ return
f
"""
+<TD WIDTH=49%>
<CENTER>
<CENTER>
- <FONT STYLE="font-size:26pt">
%s
<BR>
- <IMG SRC="
%s
"
+ <FONT STYLE="font-size:26pt">
{name}
<BR>
+ <IMG SRC="
{self.get_state_icon(state)}
"
HEIGHT=250
HEIGHT=250
- STYLE="border-style: solid; border-width:
%dpx; %s
">
+ STYLE="border-style: solid; border-width:
{width}px; {color}
">
<BR>
<BR>
- <B>
%s
</B></FONT><BR>
- for
%d day(s), %02d:%02d.
+ <B>
{state}
</B></FONT><BR>
+ for
{duration}
</CENTER>
</CENTER>
-</TD>""" % (name,
- self.get_state_icon(state),
- width,
- color,
- state,
- days[0], hours[0], minutes[0])
+</TD>"""
return None
return None
+
# Test
# Test
-
x = garage_door_renderer({"Test"
: 1})
-x.periodic_render("Poll MyQ")
-x.periodic_render("Update Page")
+
#x = garage_door_renderer({"Test"
: 1})
+
#
x.periodic_render("Poll MyQ")
+
#
x.periodic_render("Update Page")