From 7ffdb7471407d6cc41a8863fe92adf10f6f10d7c Mon Sep 17 00:00:00 2001 From: Martin Pauli Date: Wed, 22 Oct 2025 01:37:15 +0200 Subject: [PATCH] Use temperature measurement from car if available, otherwise fetch from openweather API Adjust tests to match code changes Test Adjust tests to match code changes Fix code formatting --- .../psacc/application/psa_client.py | 9 ++++--- psa_car_controller/psacc/repository/db.py | 6 +++-- tests/test_unit.py | 26 ++++++++++--------- tests/utils.py | 8 +++--- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/psa_car_controller/psacc/application/psa_client.py b/psa_car_controller/psacc/application/psa_client.py index fee3ff9..88b9fbb 100644 --- a/psa_car_controller/psacc/application/psa_client.py +++ b/psa_car_controller/psacc/application/psa_client.py @@ -195,11 +195,14 @@ class PSAClient: date = car.status.last_position.properties.updated_at if date is None or date < datetime.now(timezone.utc) - timedelta(days=1): # if position isn't updated date = charge_date + + temp = getattr(getattr(getattr(car.status, "environment", None), "air", None), "temp", None) + logger.debug("vin:%s longitude:%s latitude:%s date:%s mileage:%s level:%s charge_date:%s level_fuel:" - "%s moving:%s", car.vin, longitude, latitude, date, mileage, level, charge_date, level_fuel, - moving) + "%s moving:%s temp:%s", car.vin, longitude, latitude, date, mileage, level, charge_date, + level_fuel, moving, temp) Database.record_position(self.weather_api, car.vin, mileage, latitude, longitude, altitude, date, level, - level_fuel, moving) + level_fuel, moving, temp) self.abrp.call(car, Database.get_last_temp(car.vin)) if car.has_battery(): electric_energy_status = car.status.get_energy('Electric') diff --git a/psa_car_controller/psacc/repository/db.py b/psa_car_controller/psacc/repository/db.py index 5474fc1..97b5d54 100644 --- a/psa_car_controller/psacc/repository/db.py +++ b/psa_car_controller/psacc/repository/db.py @@ -269,14 +269,16 @@ class Database: # pylint: disable=too-many-arguments,too-many-positional-arguments @staticmethod - def record_position(weather_api, vin, mileage, latitude, longitude, altitude, date, level, level_fuel, moving): + def record_position(weather_api, vin, mileage, latitude, longitude, altitude, date, level, level_fuel, + moving, temp): if mileage == 0: # fix a bug of the api logger.error("The api return a wrong mileage for %s : %f", vin, mileage) else: try: conn = Database.get_db() if conn.execute("SELECT Timestamp from position where Timestamp=?", (date,)).fetchone() is None: - temp = get_temp(latitude, longitude, weather_api) + if temp is None: + temp = get_temp(latitude, longitude, weather_api) if level_fuel and level_fuel == 0: # fix fuel level not provided when car is off try: level_fuel = conn.execute( diff --git a/tests/test_unit.py b/tests/test_unit.py index 4d29f90..e535456 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -156,7 +156,8 @@ class TestUnit(unittest.TestCase): datetime(2022, 3, 26, 11, 2, 54, tzinfo=tzutc()), 59.0, None, - True) + True, + None) self.assertEqual(db_record_position_arg, expected_result) @patch("psa_car_controller.psacc.repository.db.Database.record_battery_soh") @@ -175,7 +176,8 @@ class TestUnit(unittest.TestCase): datetime(2022, 3, 26, 11, 2, 54, tzinfo=tzutc()), 59.0, None, - True) + True, + None) self.assertEqual(db_record_position_arg, expected_result) self.assertEqual( ('VR3UHZKX', @@ -257,9 +259,9 @@ class TestUnit(unittest.TestCase): get_new_test_db() config_repository.CONFIG_FILENAME = DATA_DIR + "config.ini" car = self.vehicule_list[1] - Database.record_position(None, car.vin, 11, latitude, longitude, 22, date0, 40, 30, False) - Database.record_position(None, car.vin, 20, latitude, longitude, 22, date1, 35, 29, False) - Database.record_position(None, car.vin, 30, latitude, longitude, 22, date2, 30, 28, False) + Database.record_position(None, car.vin, 11, latitude, longitude, 22, date0, 40, 30, False, None) + Database.record_position(None, car.vin, 20, latitude, longitude, 22, date1, 35, 29, False, None) + Database.record_position(None, car.vin, 30, latitude, longitude, 22, date2, 30, 28, False, None) trips = Trips.get_trips(self.vehicule_list) res = trips[car.vin].get_trips_as_dict() assert compare_dict(res, [{'consumption_km': 6.947368421052632, @@ -280,14 +282,14 @@ class TestUnit(unittest.TestCase): get_new_test_db() config_repository.CONFIG_FILENAME = DATA_DIR + "config.ini" car = self.vehicule_list[1] - Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(1), 40, 30, False) - Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(2), 35, 29, False) - Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(3), 30, 28, False) + Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(1), 40, 30, False, None) + Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(2), 35, 29, False, None) + Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(3), 30, 28, False, None) start = get_date(4) end = get_date(6) - Database.record_position(None, car.vin, 11, latitude, longitude, 22, start, 40, 30, False) - Database.record_position(None, car.vin, 20, latitude, longitude, 22, get_date(5), 35, 29, False) - Database.record_position(None, car.vin, 30, latitude, longitude, 22, end, 30, 28, False) + Database.record_position(None, car.vin, 11, latitude, longitude, 22, start, 40, 30, False, None) + Database.record_position(None, car.vin, 20, latitude, longitude, 22, get_date(5), 35, 29, False, None) + Database.record_position(None, car.vin, 30, latitude, longitude, 22, end, 30, 28, False, None) trips = Trips.get_trips(self.vehicule_list) res = trips[car.vin].get_trips_as_dict() assert compare_dict(res, [{'consumption_km': 6.947368421052632, @@ -309,7 +311,7 @@ class TestUnit(unittest.TestCase): get_new_test_db() Database.set_db_callback(callback_test) assert old_dummy_value == dummy_value - Database.record_position(None, "xx", 11, latitude, longitude - 0.05, None, date0, 40, None, False) + Database.record_position(None, "xx", 11, latitude, longitude - 0.05, None, date0, 40, None, False, None) assert old_dummy_value != dummy_value def test_parse_hour(self): diff --git a/tests/utils.py b/tests/utils.py index 7bfb482..fd927fb 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -37,10 +37,10 @@ def get_new_test_db(): def record_position(): - Database.record_position(None, car.vin, 11, latitude, longitude - 0.05, None, date0, 40, None, False) - Database.record_position(None, car.vin, 20, latitude, longitude, 32, date1, 35, None, False) - Database.record_position(None, car.vin, 30, latitude, longitude, 42, date2, 30, None, False) - Database.record_position(None, car.vin, None, latitude, longitude, 42, date2, 30, None, False) + Database.record_position(None, car.vin, 11, latitude, longitude - 0.05, None, date0, 40, None, False, None) + Database.record_position(None, car.vin, 20, latitude, longitude, 32, date1, 35, None, False, None) + Database.record_position(None, car.vin, 30, latitude, longitude, 42, date2, 30, None, False, None) + Database.record_position(None, car.vin, None, latitude, longitude, 42, date2, 30, None, False, None) def record_charging():