103 lines
2.8 KiB
Python
103 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import yaml
|
|
import json
|
|
import requests
|
|
import time
|
|
|
|
from datetime import datetime, timedelta
|
|
from pprint import pprint
|
|
|
|
|
|
config_path = "./config.yaml"
|
|
|
|
headers = {"Content-Type":"application/json"}
|
|
def init():
|
|
global conf # Configuration as dict, comfortable and easy.
|
|
conf = load_config(config_path)
|
|
if conf["https"] == True:
|
|
conf["post_url"] = f"https://{conf['base_url']}/json"
|
|
else:
|
|
conf["post_url"] = f"http://{conf['base_url']}/json"
|
|
|
|
def load_config(config_name) -> dict:
|
|
# Loads config file as yaml and returns a dict.
|
|
with open(config_name) as f:
|
|
data = yaml.load(f, Loader=yaml.BaseLoader)
|
|
return data
|
|
|
|
|
|
def parse_bedtime():
|
|
bedtime = conf["bedtime"]
|
|
hh,mm,ss = bedtime.split(":")
|
|
print(hh,mm,ss)
|
|
bedtime = datetime.now()
|
|
|
|
|
|
def get_status():
|
|
response = requests.get(conf["post_url"])
|
|
response_json = json.loads(response.text)
|
|
return response_json
|
|
|
|
def set_powerstate(target_state):
|
|
if target_state:
|
|
send_state = "false"
|
|
else:
|
|
send_state = "true"
|
|
response = requests.post(conf["post_url"], json={"on": send_state}, headers=headers)
|
|
|
|
def set_preset_id(preset_id=0):
|
|
send_data = {"ps": preset_id}
|
|
response = requests.post(conf["post_url"], json=send_data, headers=headers)
|
|
response_json = json.loads(response.text)
|
|
return response_json
|
|
|
|
|
|
class night_light:
|
|
def __init__(self):
|
|
get_status()
|
|
|
|
def get_status(self):
|
|
response = requests.get(f"{conf['post_url']}/state", headers=headers)
|
|
response_json = json.loads(response.text)
|
|
self.duration = response_json["nl"]["dur"]
|
|
self.mode = response_json["nl"]["mode"]
|
|
self.enabled = response_json["nl"]["on"]
|
|
self.target_brightness = response_json["nl"]["tbri"]
|
|
self.remaining_runtime = response_json["nl"]["rem"]
|
|
pprint(f"Remaining duration: {int(response_json['nl']['rem'])/60} min")
|
|
return response_json["nl"]
|
|
|
|
|
|
def set_status(self, force=False):
|
|
get_status()
|
|
send_data = {"nl":{
|
|
"on": self.enabled,
|
|
"dur": self.duration,
|
|
"mode": self.mode,
|
|
"tbri": self.target_brightness,
|
|
"rem": self.remaining_runtime
|
|
}
|
|
}
|
|
if self.remaining_runtime == -1 or force:
|
|
response = requests.post(conf["post_url"], json=send_data, headers=headers)
|
|
response_json = json.loads(response.text)
|
|
return response_json
|
|
else:
|
|
return {'success': False}
|
|
|
|
|
|
init()
|
|
|
|
parse_bedtime()
|
|
|
|
# set_powerstate(1)
|
|
# set_preset_id(1)
|
|
# nl = night_light()
|
|
# pprint(nl.get_status())
|
|
# nl.enabled = True
|
|
# nl.mode = 1
|
|
# nl.duration = 90
|
|
# nl.target_brightness = 0
|
|
# pprint(nl.set_status())
|
|
# pprint(nl.get_status()) |