From 916daecaf64294e24b91811f2222dadb55096a79 Mon Sep 17 00:00:00 2001 From: jlayec Date: Sat, 1 May 2021 19:21:31 +0000 Subject: [PATCH] fix ecomix using country code when last_position is not available --- ecomix.py | 15 +++++++++------ libs/charging.py | 6 +++--- my_psacc.py | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ecomix.py b/ecomix.py index ded9671..1ff4008 100644 --- a/ecomix.py +++ b/ecomix.py @@ -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: diff --git a/libs/charging.py b/libs/charging.py index 367c4e2..499ae92 100644 --- a/libs/charging.py +++ b/libs/charging.py @@ -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) diff --git a/my_psacc.py b/my_psacc.py index b03e81c..22d9fd9 100644 --- a/my_psacc.py +++ b/my_psacc.py @@ -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")