Catch errors during server communication
This commit is contained in:
parent
2e908372c4
commit
aa1bea0db8
1 changed files with 23 additions and 14 deletions
35
main.py
35
main.py
|
@ -1,4 +1,5 @@
|
|||
import os
|
||||
import sys
|
||||
|
||||
from dotenv import load_dotenv
|
||||
import pendulum
|
||||
|
@ -13,21 +14,29 @@ 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",
|
||||
})
|
||||
try:
|
||||
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.chambre_temperature"
|
||||
bedroom = client.get(url, timeout=5)
|
||||
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()
|
||||
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(outside))
|
||||
|
|
Loading…
Reference in a new issue