Skip to content

Commit 6af7e6c

Browse files
authored
Update charge_template.py - optional expects remaining time untill plan target (#2936)
* Update charge_template.py - optional expects remaining time untill plan target legacy code (before price based charging) calculated remaining time in selected_plan as time until charge start. This does not make sense when looking for cheap time slots before the plan target time. * improve charge message * fix flake
1 parent b8af57b commit 6af7e6c

2 files changed

Lines changed: 40 additions & 23 deletions

File tree

packages/control/ev/charge_template.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -603,27 +603,44 @@ def scheduled_charging_calc_current(self,
603603
# ist.
604604
if plan.et_active:
605605
def get_hours_message() -> str:
606+
def end_of_today_timestamp() -> int:
607+
return datetime.datetime.now().replace(
608+
hour=23, minute=59, second=59, microsecond=999000).timestamp()
609+
606610
def is_loading_hour(hour: int) -> bool:
607611
return data.data.optional_data.et_is_charging_allowed_hours_list(hour)
608-
return ("Geladen wird "+("jetzt und "
609-
if is_loading_hour(hour_list)
610-
else '') +
611-
"zu folgenden Uhrzeiten: " +
612-
", ".join([tomorrow(hour) +
613-
datetime.datetime.fromtimestamp(hour).strftime('%-H:%M')
614-
for hour in (sorted(hour_list)
615-
if not is_loading_hour(hour_list)
616-
else (sorted(hour_list)[1:] if len(hour_list) > 1 else []))])
617-
+ ".")
618-
619-
def end_of_today_timestamp() -> int:
620-
return datetime.datetime.now().replace(
621-
hour=23, minute=59, second=59, microsecond=999000).timestamp()
622-
623-
def tomorrow(timestamp: int) -> str:
624-
return 'morgen ' if end_of_today_timestamp() < timestamp else ''
612+
613+
def convert_loading_hours_to_string(hour_list: List[int]) -> str:
614+
if 1 < len(hour_list):
615+
times_string = ", ".join(hour.strftime('%-H:%M') for hour in hour_list[:-1])
616+
return times_string + " und " + hour_list[-1].strftime('%-H:%M')
617+
else:
618+
return ", ".join(hour.strftime('%-H:%M') for hour in hour_list)
619+
midnight = end_of_today_timestamp()
620+
loading_times_today = [datetime.datetime.fromtimestamp(hour)
621+
for hour in sorted(hour_list) if hour <= midnight]
622+
loading_times_today = (loading_times_today[1:]
623+
if is_loading_hour(hour_list) else loading_times_today)
624+
loading_times_tomorrow = [datetime.datetime.fromtimestamp(hour)
625+
for hour in sorted(hour_list) if hour > midnight]
626+
627+
loading_message = "Geladen wird "+("jetzt"
628+
if is_loading_hour(hour_list)
629+
else '')
630+
loading_message += ((" und " if is_loading_hour(hour_list) else "") +
631+
f"heute {convert_loading_hours_to_string(loading_times_today)}"
632+
if 0 < len(loading_times_today)
633+
else '')
634+
loading_message += (" sowie "
635+
if 0 < len(loading_times_tomorrow)
636+
else '')
637+
loading_message += (f"morgen {convert_loading_hours_to_string(loading_times_tomorrow)}"
638+
if 0 < len(loading_times_tomorrow)
639+
else '')
640+
return loading_message + '.'
641+
625642
hour_list = data.data.optional_data.et_get_loading_hours(
626-
selected_plan.duration, selected_plan.remaining_time)
643+
selected_plan.duration, selected_plan.duration + selected_plan.remaining_time)
627644

628645
log.debug(f"Günstige Ladezeiten: {hour_list}")
629646
if data.data.optional_data.et_is_charging_allowed_hours_list(hour_list):

packages/control/ev/charge_template_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,39 +311,39 @@ def test_scheduled_charging_calc_current_no_plans():
311311
14,
312312
"instant_charging",
313313
ChargeTemplate.SCHEDULED_CHARGING_CHEAP_HOUR.format(
314-
"Geladen wird jetzt und zu folgenden Uhrzeiten: morgen 8:00."),
314+
"Geladen wird jetzt sowie morgen 8:00."),
315315
3),
316316
id="cheap_hour_charge_with_instant_charging"),
317317
pytest.param(True, 79, 80, 70, LOADING_HOURS_TODAY,
318318
(
319319
14,
320320
"instant_charging",
321321
ChargeTemplate.SCHEDULED_CHARGING_CHEAP_HOUR.format(
322-
"Geladen wird jetzt und zu folgenden Uhrzeiten: ."),
322+
"Geladen wird jetzt."),
323323
3),
324324
id="SOC limit reached but scheduled SOC not, no further loading hours"),
325325
pytest.param(False, 79, 80, 90, LOADING_HOURS_TODAY,
326326
(
327327
6,
328328
"pv_charging",
329329
ChargeTemplate.SCHEDULED_CHARGING_EXPENSIVE_HOUR.format(
330-
"Geladen wird zu folgenden Uhrzeiten: 8:00."),
330+
"Geladen wird heute 8:00."),
331331
0),
332332
id="expensive_hour_charge_with_pv"),
333333
pytest.param(False, 79, 80, 70, LOADING_HOURS_TODAY,
334334
(
335335
0,
336336
"stop",
337337
ChargeTemplate.SCHEDULED_CHARGING_EXPENSIVE_HOUR_REACHED_MAX_SOC.format(
338-
"Geladen wird zu folgenden Uhrzeiten: 8:00."),
338+
"Geladen wird heute 8:00."),
339339
3),
340340
id="expensive_hour_no_charge_with_pv "),
341341
pytest.param(False, 79, 80, 70, LOADING_HOURS_TODAY + LOADING_HOURS_TOMORROW,
342342
(
343343
0,
344344
"stop",
345345
ChargeTemplate.SCHEDULED_CHARGING_EXPENSIVE_HOUR_REACHED_MAX_SOC.format(
346-
"Geladen wird zu folgenden Uhrzeiten: 8:00, morgen 8:00."),
346+
"Geladen wird heute 8:00 sowie morgen 8:00."),
347347
3),
348348
id="expensive_hour_no_charge_with_pv scheduled for tomorrow"),
349349
pytest.param(False, 79, 60, 80, LOADING_HOURS_TODAY,

0 commit comments

Comments
 (0)