test pisugar support

This commit is contained in:
Ace 2024-06-23 20:13:58 +02:00
parent 610f246c02
commit 309c6ca5bc
5 changed files with 88 additions and 3 deletions

View File

@ -1,7 +1,12 @@
"""Basic Inkycal run script.
Assumes that the settings.json file is in the /boot directory.
set render=True to render the display, set render=False to only run the modules.
"""
import asyncio
from inkycal import Inkycal
inky = Inkycal(render=True) # Initialise Inkycal
# If your settings.json file is not in /boot, use the full path: inky = Inkycal('path/to/settings.json', render=True)
inky.test() # test if Inkycal can be run correctly, running this will show a bit of info for each module
inky.run(run_once=True) # test if Inkycal can be run correctly, running this will show a bit of info for each module
asyncio.run(inky.run()) # If there were no issues, you can run Inkycal nonstop

View File

@ -13,7 +13,7 @@ from inkycal import loggers # noqa
from inkycal.custom import *
from inkycal.display import Display
from inkycal.modules.inky_image import Inkyimage as Images
from inkycal.utils.json_cache import JSONCache
from inkycal.utils import JSONCache
logger = logging.getLogger(__name__)

View File

@ -8,7 +8,7 @@ from inkycal.custom import *
# PIL has a class named Image, use alias for Inkyimage -> Images
from inkycal.modules.inky_image import Inkyimage as Images, image_to_palette
from inkycal.modules.template import inkycal_module
from inkycal.utils.json_cache import JSONCache
from inkycal.utils import JSONCache
logger = logging.getLogger(__name__)

View File

@ -0,0 +1,2 @@
from .pisugar import PiSugar
from .json_cache import JSONCache

78
inkycal/utils/pisugar.py Normal file
View File

@ -0,0 +1,78 @@
"""PiSugar helper class for Inkycal."""
import logging
import subprocess
from inkycal.settings import Settings
settings = Settings()
logger = logging.getLogger(__name__)
class PiSugar:
def __init__(self):
# replace "command" with actual command
self.command_template = 'echo "command" | nc -q 0 127.0.0.1 8423'
self.allowed_commands = ["get battery", "get model", "get rtc_time", "get rtc_alarm_enabled",
"get rtc_alarm_time", "get alarm_repeat", "rtc_pi2rtc"]
def _get_output(self, command):
if command not in self.allowed_commands:
logger.error(f"Command {command} not allowed")
return None
cmd = self.command_template.replace("command", command)
try:
result = subprocess.run(cmd, text=True, capture_output=True)
if result.returncode != 0:
print(f"Command failed with {result.stderr}")
return None
return result
except Exception as e:
logger.error(f"Error executing command: {e}")
return None
def get_battery(self) -> int or None:
"""Get the battery level in percentage.
Returns:
int or None: The battery level in percentage or None if the command fails.
"""
battery_output = self._get_output("get battery")
if battery_output:
for line in battery_output.splitlines():
if 'battery:' in line:
return int(line.split(':')[1].strip())
return None
def get_model(self):
"""Get the PiSugar model."""
model_output = self._get_output("get model")
if model_output:
for line in model_output.splitlines():
if 'model:' in line:
return line.split(':')[1].strip()
return None
def get_rtc_time(self):
"""Get the RTC time."""
return self._get_output("get rtc_time")
def get_rtc_alarm_enabled(self):
"""Get the RTC alarm enabled status."""
return self._get_output("get rtc_alarm_enabled")
def get_rtc_alarm_time(self):
"""Get the RTC alarm time."""
return self._get_output("get rtc_alarm_time")
def get_alarm_repeat(self):
"""Get the alarm repeat status."""
return self._get_output("get alarm_repeat")
def rtc_pi2rtc(self):
"""Sync the Pi time to RTC."""
return self._get_output("rtc_pi2rtc")