mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-21 17:06:18 +00:00
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
This commit is contained in:
committed by
Florian BEZANNIER
parent
d41eae6fd8
commit
7ffdb74714
@@ -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')
|
||||
|
||||
@@ -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(
|
||||
|
||||
+14
-12
@@ -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):
|
||||
|
||||
+4
-4
@@ -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():
|
||||
|
||||
Reference in New Issue
Block a user