commit 5ee6c61ff01e05136e54e0e3a2c5015fea4458e1 Author: Gabriel Augendre Date: Mon Jun 29 16:25:07 2020 +0200 Add script diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e2beb72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +.python-version diff --git a/main.py b/main.py new file mode 100644 index 0000000..4cd6db9 --- /dev/null +++ b/main.py @@ -0,0 +1,53 @@ +import os + +from dotenv import load_dotenv +import pendulum +import requests + +load_dotenv() + +BASE_URL = os.getenv("HASS_BASE_URL") +TOKEN = os.getenv("HASS_TOKEN") + +def main(): + assert BASE_URL, "Must supply HASS_BASE_URL env variable" + assert TOKEN, "Must supply HASS_TOKEN env variable" + + client = requests.Session() + client.headers.update({ + "Authorization": f"Bearer {TOKEN}", + "Content-Type": "application/json", + }) + + url = f"{BASE_URL}/states/sensor.chambre_temperature" + bedroom = client.get(url) + bedroom.raise_for_status() + bedroom = bedroom.json() + + url = f"{BASE_URL}/states/sensor.exterieur_temperature" + outside = client.get(url) + outside.raise_for_status() + outside = outside.json() + + print(format_temp(bedroom)) + print(format_temp(outside)) + + temp_bedroom = float(bedroom["state"]) + temp_outside = float(outside["state"]) + print() + if temp_outside < temp_bedroom - 1: + print("Il fait plus FRAIS dehors, tu peux ouvrir !") + else: + print("FERME TOUT !") + + +def format_temp(data: dict) -> str: + name = data["attributes"]["friendly_name"].lower().replace("temperature", "").strip() + diff = pendulum.parse(data["last_updated"]).diff_for_humans(locale="fr") + return f"{name} : {data['state']}{data['attributes']['unit_of_measurement']} ({diff})" + + + +if __name__ == "__main__": + main() + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4ed46b2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pendulum==2.1.0 +python-dotenv==0.13.0 +requests==2.24.0