X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;ds=inline;f=myq_renderer.py;h=ebfac233927a8038cda3daca83a2fe23fb984d78;hb=05d7c68eff68d919e1868463d6721acb896eacd9;hp=1e666489e66cf0366532e3658a6f9a6bc2763a01;hpb=5e241dc47e497c547463cecc07946ea6882835a7;p=kiosk.git
diff --git a/myq_renderer.py b/myq_renderer.py
index 1e66648..ebfac23 100644
--- a/myq_renderer.py
+++ b/myq_renderer.py
@@ -1,86 +1,93 @@
-import pymyq
+#!/usr/bin/env python3
+
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
- self.last_update = None
+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):
+ def debug_prefix(self) -> str:
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:
- 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
)
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)
- f.write(
- """
+ def update_page(self) -> bool:
+ with file_writer.file_writer(kiosk_constants.myq_pagename) as f:
+ f.write(
+ f"""
Garage Door Status
-
+
-
-
+
+
"""
- % self.last_update
- )
- 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(
- """
-
+ html = self.do_door("Middle Door")
+ if html is None:
+ return False
+ f.write(html)
+ f.write(
+ """
+
"""
- )
- f.close()
+ )
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"
@@ -91,9 +98,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
- 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.state == "open":
color = "border-color: #ff0000;"
@@ -101,31 +106,22 @@ class garage_door_renderer(renderer.debuggable_abstaining_renderer):
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.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")