Add script

This commit is contained in:
Gabriel Augendre 2020-06-29 16:25:07 +02:00
commit 5ee6c61ff0
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
3 changed files with 58 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.env
.python-version

53
main.py Normal file
View file

@ -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()

3
requirements.txt Normal file
View file

@ -0,0 +1,3 @@
pendulum==2.1.0
python-dotenv==0.13.0
requests==2.24.0