dotfiles/scripts/lib/sunset.py

37 lines
1.5 KiB
Python

"""Sets the monitor temperature based on the current time using Hyprsunset"""
from os import environ
from socket import AF_UNIX, SOCK_STREAM, socket
from .evening import on_steam, Range, smoothstep, sunup_amount
def setup_socket() -> socket:
"""Connects to the Hyprsunset socket"""
hyprsunset = socket(AF_UNIX, SOCK_STREAM)
hyprsunset.connect(f"{environ["XDG_RUNTIME_DIR"]}/hypr/{environ["HYPRLAND_INSTANCE_SIGNATURE"]}/.hyprsunset.sock")
return hyprsunset
def get_temperature(hyprsunset: socket) -> int:
"""Retrieves the current screen temperature"""
# 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(b"temperature")
# 4 bytes should be enough but why not 8 for comfort
# Just raise an error if it's not a number, nothing special to do here
return int(hyprsunset.recv(8))
def set_temperature(hyprsunset: socket, temperature: float) -> None:
"""Sends a message to hyprsunset to set the temperature"""
# 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())
def main(temperature_range: Range, /, check_steam: bool = False) -> None:
"""Adjusts the monitor temperature based on the current time"""
hyprsunset = setup_socket()
temperature = int(smoothstep(sunup_amount(), temperature_range))
if temperature != get_temperature(hyprsunset):
if check_steam and on_steam():
return
set_temperature(hyprsunset, temperature)