-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix and improve
auto-temperature.py
script (#23)
Now it should not suddenly jump to `TEMP_DAY` when current_time == DUSK_TIME. Also, I made some performance and portability improvments.
- Loading branch information
Showing
1 changed file
with
43 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,49 @@ | ||
import datetime as dt | ||
import time | ||
#!/usr/bin/python | ||
"""Change screen temperature depending on dawn/dusk time.""" | ||
|
||
import subprocess | ||
from datetime import datetime | ||
from time import sleep | ||
|
||
|
||
TEMP_DAY = 6500.0 | ||
TEMP_NIGHT = 3800.0 | ||
|
||
DAWN_TIME = 9 * 3600.0 # 9am (Should be > WINDOW) | ||
DUSK_TIME = 19 * 3600.0 # 19pm (Shoulbe be > WINDOW + DAWN_TIME) | ||
|
||
## Tested parameter space: | ||
# dusk_time < 3600 + dawn_time | ||
# 3600 < dusk_time | ||
WINDOW = 900 # 15m (The time during which the temperature changes gradually until it reaches the desired value) | ||
|
||
temp_day = 6500 | ||
temp_night = 3800 | ||
dawn_time = 9 * 3600 # 9am | ||
dusk_time = 19 * 3600 # 19pm | ||
|
||
temp = None | ||
|
||
while True: | ||
temp = temp_day | ||
now = dt.datetime.now() | ||
curr_time = int( | ||
(now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() | ||
) | ||
if dawn_time - 3600 < curr_time < dawn_time: | ||
temp = (dawn_time - curr_time) * (temp_night - temp_day) / 3600 + temp_day | ||
if dusk_time - 3600 < curr_time < dusk_time: | ||
temp = (dusk_time - curr_time) * (temp_day - temp_night) / 3600 + temp_night | ||
print("check") | ||
if dusk_time < curr_time or curr_time < dawn_time: | ||
temp = temp_night | ||
temp = int(temp) | ||
subprocess.run( | ||
f"busctl --user set-property rs.wl-gammarelay / rs.wl.gammarelay Temperature q {temp}", | ||
shell=True, | ||
) | ||
time.sleep(1) | ||
now = datetime.now().astimezone() | ||
# Seconds since the beginning of day | ||
current_time = now.hour * 3600 + now.minute * 60 + now.second | ||
|
||
if DAWN_TIME - WINDOW < current_time < DAWN_TIME: | ||
temp = (DAWN_TIME - current_time) * (TEMP_NIGHT - TEMP_DAY) / WINDOW + TEMP_DAY | ||
elif DUSK_TIME - WINDOW < current_time < DUSK_TIME: | ||
temp = (DUSK_TIME - current_time) * (TEMP_DAY - TEMP_NIGHT) / WINDOW + TEMP_NIGHT | ||
elif current_time > DUSK_TIME or current_time < DAWN_TIME: | ||
temp = TEMP_NIGHT | ||
elif current_time > DAWN_TIME or current_time < DUSK_TIME: | ||
temp = TEMP_DAY | ||
|
||
if temp: | ||
subprocess.run( | ||
[ | ||
"busctl", | ||
"--user", | ||
"set-property", | ||
"rs.wl-gammarelay", | ||
"/", | ||
"rs.wl.gammarelay", | ||
"Temperature", | ||
"q", | ||
str(int(temp)), | ||
] | ||
) | ||
|
||
sleep(1) |