X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;ds=inline;f=myq_renderer.py;h=ebfac233927a8038cda3daca83a2fe23fb984d78;hb=05d7c68eff68d919e1868463d6721acb896eacd9;hp=333c356a6e96891a26ddf8c36f743636c785b155;hpb=3b4fef0ca20b98c670acbaa2899bdb1b35eb12d7;p=kiosk.git
diff --git a/myq_renderer.py b/myq_renderer.py
index 333c356..ebfac23 100644
--- a/myq_renderer.py
+++ b/myq_renderer.py
@@ -1,80 +1,93 @@
-#!/usr/local/bin/python3.7
+#!/usr/bin/env python3
-import pymyq
from aiohttp import ClientSession
import asyncio
-import constants
import datetime
from dateutil.parser import parse
+import pymyq # type: ignore
+from typing import Dict, Optional
+
+from pyutils.datetimes import datetime_utils
+
+import kiosk_constants
import file_writer
import renderer
-import secrets
+import kiosk_secrets as secrets
-class garage_door_renderer(renderer.debuggable_abstaining_renderer):
- def __init__(self, name_to_timeout_dict):
- super(garage_door_renderer, self).__init__(name_to_timeout_dict, False)
- self.doors = None
- def debug_prefix(self):
+class garage_door_renderer(renderer.abstaining_renderer):
+ def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
+ super().__init__(name_to_timeout_dict)
+ self.doors: Optional[Dict] = None
+ self.last_update: Optional[datetime.datetime] = None
+
+ def debug_prefix(self) -> str:
return "myq"
- def periodic_render(self, key):
+ def periodic_render(self, key: str) -> bool:
if key == "Poll MyQ":
- asyncio.get_event_loop().run_until_complete(self.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:
- 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
+ assert(self.doors is not None)
+ return len(self.doors) > 0
- def update_page(self):
- f = file_writer.file_writer(constants.myq_pagename)
- now = datetime.datetime.now()
- f.write("""
+ def update_page(self) -> bool:
+ with file_writer.file_writer(kiosk_constants.myq_pagename) as f:
+ f.write(
+ f"""
Garage Door Status
-
+
-
-
-""" % now)
- html = self.do_door("Near House")
- if html == None:
- return False
- f.write(html)
+
+
+"""
+ )
+ 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("""
-
-
""")
- f.close()
+ html = self.do_door("Middle Door")
+ if html is None:
+ return False
+ f.write(html)
+ f.write(
+ """
+
+
"""
+ )
return True
- def get_state_icon(self, state):
+ def get_state_icon(self, state: str) -> str:
if state == "open":
- return "/icons/garage_open.png"
+ return "/kiosk/images/garage_open.png"
elif state == "closed":
- return "/icons/garage_closed.png"
+ return "/kiosk/images/garage_closed.png"
elif state == "opening":
- return "/icons/garage_opening.png"
+ return "/kiosk/images/garage_opening.png"
elif state == "closing":
- return "/icons/garage_closing.png"
+ return "/kiosk/images/garage_closing.png"
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:
- j = self.doors[key].json
+ j = self.doors[key].device_json
state = j["state"]["door_state"]
# "last_update": "2020-07-04T18:11:34.2981419Z"
@@ -85,36 +98,30 @@ 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
- days = divmod(delta, constants.seconds_per_day)
- hours = divmod(days[1], constants.seconds_per_hour)
- minutes = divmod(hours[1], constants.seconds_per_minute)
+ duration = datetime_utils.describe_duration_briefly(int(delta))
width = 0
- if is_night and door.get_status() == "open":
+ if is_night and door.state == "open":
color = "border-color: #ff0000;"
width = 15
else:
color = ""
width = 0
- return """
-
+ return f"""
+ |
- %s
- {name}
+
+ STYLE="border-style: solid; border-width: {width}px; {color}">
- %s
- for %d day(s), %02d:%02d.
+ {state}
+ for {duration}
- | """ % (name,
- self.get_state_icon(state),
- width,
- color,
- state,
- days[0], hours[0], minutes[0])
+"""
return None
+
# Test
-#x = garage_door_renderer({"Test" : 1})
+#x = garage_door_renderer({"Test": 1})
#x.periodic_render("Poll MyQ")
#x.periodic_render("Update Page")