From 7d5c6353edd620d9e33222b6d6e9cf6f7ee43ffa Mon Sep 17 00:00:00 2001 From: Adam Rabjerg Date: Tue, 26 Sep 2023 11:40:25 +0200 Subject: [PATCH] init --- .gitignore | 2 + config.yaml | 4 ++ main.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 + 4 files changed, 111 insertions(+) create mode 100644 .gitignore create mode 100644 config.yaml create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c1d6aa5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv +.vscode diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..530a4b5 --- /dev/null +++ b/config.yaml @@ -0,0 +1,4 @@ +--- +base_url: 192.168.178.155 +https: false +bedtime: "00:30:00" \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..a2ff8c2 --- /dev/null +++ b/main.py @@ -0,0 +1,103 @@ +#!/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()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0f2eecc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pyyaml +requests \ No newline at end of file