Skip to content

Commit 7ffdb74

Browse files
GermanDarknesflobz
authored andcommitted
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
1 parent d41eae6 commit 7ffdb74

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

psa_car_controller/psacc/application/psa_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,14 @@ def record_info(self, car: Car): # pylint: disable=too-many-locals
195195
date = car.status.last_position.properties.updated_at
196196
if date is None or date < datetime.now(timezone.utc) - timedelta(days=1): # if position isn't updated
197197
date = charge_date
198+
199+
temp = getattr(getattr(getattr(car.status, "environment", None), "air", None), "temp", None)
200+
198201
logger.debug("vin:%s longitude:%s latitude:%s date:%s mileage:%s level:%s charge_date:%s level_fuel:"
199-
"%s moving:%s", car.vin, longitude, latitude, date, mileage, level, charge_date, level_fuel,
200-
moving)
202+
"%s moving:%s temp:%s", car.vin, longitude, latitude, date, mileage, level, charge_date,
203+
level_fuel, moving, temp)
201204
Database.record_position(self.weather_api, car.vin, mileage, latitude, longitude, altitude, date, level,
202-
level_fuel, moving)
205+
level_fuel, moving, temp)
203206
self.abrp.call(car, Database.get_last_temp(car.vin))
204207
if car.has_battery():
205208
electric_energy_status = car.status.get_energy('Electric')

psa_car_controller/psacc/repository/db.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,16 @@ def get_recorded_position():
269269

270270
# pylint: disable=too-many-arguments,too-many-positional-arguments
271271
@staticmethod
272-
def record_position(weather_api, vin, mileage, latitude, longitude, altitude, date, level, level_fuel, moving):
272+
def record_position(weather_api, vin, mileage, latitude, longitude, altitude, date, level, level_fuel,
273+
moving, temp):
273274
if mileage == 0: # fix a bug of the api
274275
logger.error("The api return a wrong mileage for %s : %f", vin, mileage)
275276
else:
276277
try:
277278
conn = Database.get_db()
278279
if conn.execute("SELECT Timestamp from position where Timestamp=?", (date,)).fetchone() is None:
279-
temp = get_temp(latitude, longitude, weather_api)
280+
if temp is None:
281+
temp = get_temp(latitude, longitude, weather_api)
280282
if level_fuel and level_fuel == 0: # fix fuel level not provided when car is off
281283
try:
282284
level_fuel = conn.execute(

tests/test_unit.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ def test_electric_record_info(self, mock_db):
156156
datetime(2022, 3, 26, 11, 2, 54, tzinfo=tzutc()),
157157
59.0,
158158
None,
159-
True)
159+
True,
160+
None)
160161
self.assertEqual(db_record_position_arg, expected_result)
161162

162163
@patch("psa_car_controller.psacc.repository.db.Database.record_battery_soh")
@@ -175,7 +176,8 @@ def test_electric_record_info_v2(self, mock_db, moock_soh):
175176
datetime(2022, 3, 26, 11, 2, 54, tzinfo=tzutc()),
176177
59.0,
177178
None,
178-
True)
179+
True,
180+
None)
179181
self.assertEqual(db_record_position_arg, expected_result)
180182
self.assertEqual(
181183
('VR3UHZKX',
@@ -257,9 +259,9 @@ def test_fuel_car(self):
257259
get_new_test_db()
258260
config_repository.CONFIG_FILENAME = DATA_DIR + "config.ini"
259261
car = self.vehicule_list[1]
260-
Database.record_position(None, car.vin, 11, latitude, longitude, 22, date0, 40, 30, False)
261-
Database.record_position(None, car.vin, 20, latitude, longitude, 22, date1, 35, 29, False)
262-
Database.record_position(None, car.vin, 30, latitude, longitude, 22, date2, 30, 28, False)
262+
Database.record_position(None, car.vin, 11, latitude, longitude, 22, date0, 40, 30, False, None)
263+
Database.record_position(None, car.vin, 20, latitude, longitude, 22, date1, 35, 29, False, None)
264+
Database.record_position(None, car.vin, 30, latitude, longitude, 22, date2, 30, 28, False, None)
263265
trips = Trips.get_trips(self.vehicule_list)
264266
res = trips[car.vin].get_trips_as_dict()
265267
assert compare_dict(res, [{'consumption_km': 6.947368421052632,
@@ -280,14 +282,14 @@ def test_none_mileage(self):
280282
get_new_test_db()
281283
config_repository.CONFIG_FILENAME = DATA_DIR + "config.ini"
282284
car = self.vehicule_list[1]
283-
Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(1), 40, 30, False)
284-
Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(2), 35, 29, False)
285-
Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(3), 30, 28, False)
285+
Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(1), 40, 30, False, None)
286+
Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(2), 35, 29, False, None)
287+
Database.record_position(None, car.vin, None, latitude, longitude, 22, get_date(3), 30, 28, False, None)
286288
start = get_date(4)
287289
end = get_date(6)
288-
Database.record_position(None, car.vin, 11, latitude, longitude, 22, start, 40, 30, False)
289-
Database.record_position(None, car.vin, 20, latitude, longitude, 22, get_date(5), 35, 29, False)
290-
Database.record_position(None, car.vin, 30, latitude, longitude, 22, end, 30, 28, False)
290+
Database.record_position(None, car.vin, 11, latitude, longitude, 22, start, 40, 30, False, None)
291+
Database.record_position(None, car.vin, 20, latitude, longitude, 22, get_date(5), 35, 29, False, None)
292+
Database.record_position(None, car.vin, 30, latitude, longitude, 22, end, 30, 28, False, None)
291293
trips = Trips.get_trips(self.vehicule_list)
292294
res = trips[car.vin].get_trips_as_dict()
293295
assert compare_dict(res, [{'consumption_km': 6.947368421052632,
@@ -309,7 +311,7 @@ def test_db_callback(self):
309311
get_new_test_db()
310312
Database.set_db_callback(callback_test)
311313
assert old_dummy_value == dummy_value
312-
Database.record_position(None, "xx", 11, latitude, longitude - 0.05, None, date0, 40, None, False)
314+
Database.record_position(None, "xx", 11, latitude, longitude - 0.05, None, date0, 40, None, False, None)
313315
assert old_dummy_value != dummy_value
314316

315317
def test_parse_hour(self):

tests/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def get_new_test_db():
3737

3838

3939
def record_position():
40-
Database.record_position(None, car.vin, 11, latitude, longitude - 0.05, None, date0, 40, None, False)
41-
Database.record_position(None, car.vin, 20, latitude, longitude, 32, date1, 35, None, False)
42-
Database.record_position(None, car.vin, 30, latitude, longitude, 42, date2, 30, None, False)
43-
Database.record_position(None, car.vin, None, latitude, longitude, 42, date2, 30, None, False)
40+
Database.record_position(None, car.vin, 11, latitude, longitude - 0.05, None, date0, 40, None, False, None)
41+
Database.record_position(None, car.vin, 20, latitude, longitude, 32, date1, 35, None, False, None)
42+
Database.record_position(None, car.vin, 30, latitude, longitude, 42, date2, 30, None, False, None)
43+
Database.record_position(None, car.vin, None, latitude, longitude, 42, date2, 30, None, False, None)
4444

4545

4646
def record_charging():

0 commit comments

Comments
 (0)