Skip to content

Commit 084b91f

Browse files
committed
#296: Internally mark the car as "shutdown" only after the car state changes.
This avoids looking for a "charging started" event as soon as the charging is completed
1 parent a6b5d58 commit 084b91f

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/handlers/vehicle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ async def handle_mqtt_command(self, *, topic: str, payload: str):
209209
match payload.strip().lower():
210210
case 'true':
211211
LOG.info("HV battery is now active")
212-
self.vehicle_state.set_hv_battery_active(True)
212+
self.vehicle_state.hv_battery_active = True
213213
case 'false':
214214
LOG.info("HV battery is now inactive")
215-
self.vehicle_state.set_hv_battery_active(False)
215+
self.vehicle_state.hv_battery_active = False
216216
case _:
217217
raise MqttGatewayException(f'Unsupported payload {payload}')
218218
case mqtt_topics.DRIVETRAIN_CHARGING_SET:

src/vehicle.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def __init__(
6262
self.last_car_shutdown = datetime.datetime.now()
6363
self.last_car_vehicle_message = datetime.datetime.min
6464
# treat high voltage battery as active, if we don't have any other information
65-
self.hv_battery_active = True
66-
self.hv_battery_active_from_car = True
65+
self.__hv_battery_active = True
66+
self.__hv_battery_active_from_car = True
6767
self.is_charging = False
6868
self.refresh_period_active = -1
6969
self.refresh_period_inactive = -1
@@ -197,7 +197,7 @@ def is_complete(self) -> bool:
197197

198198
def set_is_charging(self, is_charging: bool):
199199
self.is_charging = is_charging
200-
self.set_hv_battery_active(self.is_charging)
200+
self.hv_battery_active = self.is_charging
201201
self.publisher.publish_bool(self.get_topic(mqtt_topics.DRIVETRAIN_CHARGING), self.is_charging)
202202

203203
def handle_vehicle_status(self, vehicle_status: VehicleStatusResp) -> None:
@@ -367,19 +367,30 @@ def __publish_soc(self, soc) -> bool:
367367
return True
368368
return False
369369

370-
def set_hv_battery_active(self, hv_battery_active: bool):
371-
if (
372-
not hv_battery_active
373-
and self.hv_battery_active
374-
):
375-
self.last_car_shutdown = datetime.datetime.now()
370+
@property
371+
def hv_battery_active(self):
372+
return self.__hv_battery_active
376373

377-
self.hv_battery_active = hv_battery_active
378-
self.publisher.publish_bool(self.get_topic(mqtt_topics.DRIVETRAIN_HV_BATTERY_ACTIVE), hv_battery_active)
374+
@hv_battery_active.setter
375+
def hv_battery_active(self, new_state: bool):
376+
self.__hv_battery_active = new_state
377+
self.publisher.publish_bool(self.get_topic(mqtt_topics.DRIVETRAIN_HV_BATTERY_ACTIVE), new_state)
379378

380-
if hv_battery_active:
379+
if new_state:
381380
self.notify_car_activity()
382381

382+
@property
383+
def hv_battery_active_from_car(self):
384+
return self.__hv_battery_active_from_car
385+
386+
@hv_battery_active_from_car.setter
387+
def hv_battery_active_from_car(self, new_state):
388+
old_state = self.__hv_battery_active_from_car
389+
if old_state and not new_state:
390+
self.last_car_shutdown = datetime.datetime.now()
391+
LOG.info(f'Detected vehicle {self.vin} shutdown at {self.last_car_shutdown}')
392+
self.__hv_battery_active_from_car = new_state
393+
383394
def notify_car_activity(self):
384395
self.last_car_activity = datetime.datetime.now()
385396
self.publisher.publish_str(
@@ -835,9 +846,7 @@ def update_data_conflicting_in_vehicle_and_bms(
835846
f"Vehicle {self.vin} hv_battery_active={hv_battery_active}. "
836847
f"is_charging={self.is_charging} "
837848
f"hv_battery_active_from_car={self.hv_battery_active_from_car}")
838-
self.set_hv_battery_active(
839-
hv_battery_active
840-
)
849+
self.hv_battery_active = hv_battery_active
841850

842851
# We can read this from either the BMS or the Vehicle Info
843852
electric_range_published = False

0 commit comments

Comments
 (0)