mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-22 17:36:15 +00:00
fix ecomix using country code when last_position is not available
This commit is contained in:
@@ -48,10 +48,10 @@ class Ecomix:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_data_from_co2_signal(latitude, longitude):
|
||||
def get_data_from_co2_signal(latitude, longitude, country_code_default):
|
||||
if Ecomix.co2_signal_key is not None:
|
||||
try:
|
||||
country_code = Ecomix.get_country(latitude, longitude)
|
||||
country_code = Ecomix.get_country(latitude, longitude, country_code_default)
|
||||
assert country_code is not None
|
||||
if country_code not in Ecomix._cache:
|
||||
Ecomix._cache[country_code] = []
|
||||
@@ -90,19 +90,22 @@ class Ecomix:
|
||||
return mean(co2_per_kw)
|
||||
|
||||
@staticmethod
|
||||
def get_country(latitude, longitude):
|
||||
def get_country(latitude, longitude, country_code_default):
|
||||
try:
|
||||
location = reverse_geocode.search([(latitude, longitude)])[0]
|
||||
country_code = location["country_code"]
|
||||
return country_code
|
||||
except (UnicodeDecodeError, IndexError):
|
||||
logger.error("Can't find country for %s %s", latitude, longitude)
|
||||
return None
|
||||
# return None
|
||||
country_code = country_code_default
|
||||
logger.warning("Using country of origin : %s (wrong co2 when traveling abroad)", country_code)
|
||||
return country_code
|
||||
|
||||
@staticmethod
|
||||
def get_co2_per_kw(start: datetime, end: datetime, latitude, longitude):
|
||||
def get_co2_per_kw(start: datetime, end: datetime, latitude, longitude, country_code_default):
|
||||
co2_per_kw = None
|
||||
country_code = Ecomix.get_country(latitude, longitude)
|
||||
country_code = Ecomix.get_country(latitude, longitude, country_code_default)
|
||||
if country_code is None:
|
||||
return None
|
||||
if Ecomix.co2_signal_key is not None:
|
||||
|
||||
+3
-3
@@ -47,7 +47,7 @@ class Charging:
|
||||
Database.clean_battery(conn)
|
||||
|
||||
@staticmethod
|
||||
def record_charging(car, charging_status, charge_date: datetime, level, latitude, longitude, charging_mode):
|
||||
def record_charging(car, charging_status, charge_date: datetime, level, latitude, longitude, country_code, charging_mode):
|
||||
conn = Database.get_db()
|
||||
charge_date = charge_date.replace(microsecond=0)
|
||||
if charging_status == "InProgress":
|
||||
@@ -64,7 +64,7 @@ class Charging:
|
||||
else:
|
||||
conn.execute("INSERT INTO battery(start_at,start_level,charging_mode,VIN) VALUES(?,?,?,?)",
|
||||
(charge_date, level, charging_mode, car.vin))
|
||||
Ecomix.get_data_from_co2_signal(latitude, longitude)
|
||||
Ecomix.get_data_from_co2_signal(latitude, longitude, country_code)
|
||||
else:
|
||||
try:
|
||||
start_at, stop_at, start_level = conn.execute(
|
||||
@@ -72,7 +72,7 @@ class Charging:
|
||||
"DESC limit 1", (car.vin,)).fetchone()
|
||||
in_progress = stop_at is None
|
||||
if in_progress:
|
||||
co2_per_kw = Ecomix.get_co2_per_kw(start_at, charge_date, latitude, longitude)
|
||||
co2_per_kw = Ecomix.get_co2_per_kw(start_at, charge_date, latitude, longitude, country_code)
|
||||
consumption_kw = (level - start_level) / 100 * car.battery_power
|
||||
|
||||
Charging.update_chargings(conn, start_at, charge_date, level, co2_per_kw, consumption_kw, car.vin)
|
||||
|
||||
+1
-1
@@ -476,7 +476,7 @@ class MyPSACC:
|
||||
try:
|
||||
charging_status = car.status.get_energy('Electric').charging.status
|
||||
charging_mode = car.status.get_energy('Electric').charging.charging_mode
|
||||
Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, charging_mode)
|
||||
Charging.record_charging(car, charging_status, charge_date, level, latitude, longitude, self.country_code, charging_mode)
|
||||
logger.debug("charging_status:%s ", charging_status)
|
||||
except AttributeError:
|
||||
logger.error("charging status not available from api")
|
||||
|
||||
Reference in New Issue
Block a user