From 9a03bb684b4ce51ef84b555b91c53024321efd1d Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Thu, 5 Jun 2025 00:34:34 -0400 Subject: [PATCH] Add CLI flags for Steam avoidance and instant transitions If called manually, ignore Steam by default. The systemd service checks for Steam as that is more likely to run at an annoying time. --- scripts/sunset.py | 27 ++++++++++++++++++++++----- systemd/sunset.service | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/scripts/sunset.py b/scripts/sunset.py index 577c939..c81152e 100755 --- a/scripts/sunset.py +++ b/scripts/sunset.py @@ -1,9 +1,11 @@ #!/bin/env python """Sets the monitor temperature based on the current time using Hyprsunset""" +from argparse import ArgumentParser, Namespace from datetime import datetime from os import environ from pathlib import Path from socket import AF_UNIX, SOCK_STREAM, socket +from typing import cast type Range = tuple[float, float] @@ -63,9 +65,12 @@ def get_temperature(hyprsunset: socket) -> int: return int(hyprsunset.recv(8)) -def set_temperature(hyprsunset: socket, temperature: float) -> None: +def set_temperature(hyprsunset: socket, temperature: float, /, instant: bool = False) -> None: # In theory the message might not send in one go, but in practice it does so I won't bother handling the error _ = hyprsunset.send(f"temperature {temperature:.1f}".encode()) + if instant: + # Setting the temperature twice in quick succession sometimes skips the transition period + set_temperature(hyprsunset, temperature, instant=False) def on_steam() -> bool: @@ -76,17 +81,29 @@ def on_steam() -> bool: return pid_dir.is_dir() -def main(sunrise: Range, sunset: Range, temperature_range: Range) -> None: +def main(sunrise: Range, sunset: Range, temperature_range: Range, /, check_steam: bool = False, instant: bool = False) -> None: """Adjusts the monitor temperature based on the current time""" hyprsunset = setup_socket() temperature = int(calculate_temperature(sunrise, sunset, temperature_range)) - if temperature != get_temperature(hyprsunset) and not on_steam(): - set_temperature(hyprsunset, temperature) + if temperature != get_temperature(hyprsunset): + if check_steam and on_steam(): + return + set_temperature(hyprsunset, temperature, instant=instant) +class Arguments(Namespace): + check_steam: bool = False + instant: bool = False + if __name__ == "__main__": + parser = ArgumentParser(description="Adjusts the temperature of your screen based on the time of day") + _ = parser.add_argument("-s", "--check-steam", action="store_true", help="Don't adjust temperature if Steam is active") + _ = parser.add_argument("-i", "--instant", action="store_true", help="Try to instantly change temperature") + args = cast(Arguments, parser.parse_args()) main( (day_elapsed(5), day_elapsed(7)), (day_elapsed(21), day_elapsed(23)), - (2500.0, 6000.0) + (2500.0, 6000.0), + check_steam=args.check_steam, + instant=args.instant ) diff --git a/systemd/sunset.service b/systemd/sunset.service index 30686f3..1dce2d9 100644 --- a/systemd/sunset.service +++ b/systemd/sunset.service @@ -3,4 +3,4 @@ Description=Sets the monitor temperature based on the time of day [Service] Type=oneshot -ExecStart=/bin/env python -O /home/mbradley/scripts/sunset.py +ExecStart=/bin/env python -O /home/mbradley/scripts/sunset.py --check-steam