Catch errors during server communication
This commit is contained in:
parent
2e908372c4
commit
aa1bea0db8
1 changed files with 23 additions and 14 deletions
37
main.py
37
main.py
|
@ -1,4 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import pendulum
|
import pendulum
|
||||||
|
@ -13,21 +14,29 @@ def main():
|
||||||
assert BASE_URL, "Must supply HASS_BASE_URL env variable"
|
assert BASE_URL, "Must supply HASS_BASE_URL env variable"
|
||||||
assert TOKEN, "Must supply HASS_TOKEN env variable"
|
assert TOKEN, "Must supply HASS_TOKEN env variable"
|
||||||
|
|
||||||
client = requests.Session()
|
try:
|
||||||
client.headers.update({
|
client.headers.update({
|
||||||
"Authorization": f"Bearer {TOKEN}",
|
"Authorization": f"Bearer {TOKEN}",
|
||||||
"Content-Type": "application/json",
|
"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"
|
url = f"{BASE_URL}/states/sensor.chambre_temperature"
|
||||||
outside = client.get(url)
|
bedroom = client.get(url, timeout=5)
|
||||||
outside.raise_for_status()
|
bedroom.raise_for_status()
|
||||||
outside = outside.json()
|
bedroom = bedroom.json()
|
||||||
|
|
||||||
|
url = f"{BASE_URL}/states/sensor.exterieur_temperature"
|
||||||
|
outside = client.get(url, timeout=5)
|
||||||
|
outside.raise_for_status()
|
||||||
|
outside = outside.json()
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
client.close()
|
||||||
|
print("Erreur lors de la connexion au serveur.")
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
client.close()
|
||||||
|
print(f"Erreur inconnue: {e}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
print(format_temp(bedroom))
|
print(format_temp(bedroom))
|
||||||
print(format_temp(outside))
|
print(format_temp(outside))
|
||||||
|
|
Loading…
Reference in a new issue