fix get_temp()

This commit is contained in:
Florian Bezannier
2021-03-22 12:05:30 +01:00
parent 58851f40d6
commit 8bb528d92f
2 changed files with 12 additions and 14 deletions
+2 -6
View File
@@ -508,10 +508,7 @@ class MyPSACC:
logger.error("The api return a wrong mileage for %s : %f", vin, mileage)
else:
if conn.execute("SELECT Timestamp from position where Timestamp=?", (date,)).fetchone() is None:
temp = None
if self.weather_api is not None:
temp = get_temp(latitude,longitude,self.weather_api)
temp = get_temp(latitude,longitude,self.weather_api)
if level_fuel == 0: # fix fuel level not provided when car is off
try:
level_fuel = conn.execute(
@@ -540,8 +537,7 @@ class MyPSACC:
except TypeError:
in_progress = False
if not in_progress:
conn.execute("INSERT INTO battery(start_at,start_level,VIN) VALUES(?,?,?)",
(charge_date, level, vin))
conn.execute("INSERT INTO battery(start_at,start_level,VIN) VALUES(?,?,?)", (charge_date, level, vin))
conn.commit()
else:
try:
+10 -8
View File
@@ -7,15 +7,17 @@ import requests
from MyLogger import logger
def get_temp(latitude, longitude, api_key):
def get_temp(latitude:str, longitude:str, api_key:str) -> float:
try:
weather_rep = requests.get("https://api.openweathermap.org/data/2.5/onecall",
params={"lat": latitude, "lon": longitude,
"exclude": "minutely,hourly,daily,alerts",
"appid": api_key,
"units": "metric"})
temp = weather_rep.json()["current"]["temp"]
logger.debug("Temperature :%fc", temp)
if not (latitude is None or longitude is None or api_key is None):
weather_rep = requests.get("https://api.openweathermap.org/data/2.5/onecall",
params={"lat": latitude, "lon": longitude,
"exclude": "minutely,hourly,daily,alerts",
"appid": api_key,
"units": "metric"})
temp = weather_rep.json()["current"]["temp"]
logger.debug("Temperature :%fc", temp)
return temp
except ConnectionError:
logger.error("Can't connect to openweathermap :%s", traceback.format_exc())
except KeyError: