Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix timestamp text #1646

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/control/auto_phase_switch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self,
Params("1to3, enough power, start timer", max_current_single_phase=16, timestamp_auto_phase_switch=None,
phases_to_use=1, required_current=6, evu_surplus=-800, reserved_evu_overhang=0, get_currents=[15.6, 0, 0],
get_power=3450, state=ChargepointState.CHARGING_ALLOWED, expected_phases_to_use=1, expected_current=6,
expected_message=Ev.PHASE_SWITCH_DELAY_TEXT.format("Umschaltung von 1 auf 3", "7 Min. 0 Sek."),
expected_message=Ev.PHASE_SWITCH_DELAY_TEXT.format("Umschaltung von 1 auf 3", "7 Min."),
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.PHASE_SWITCH_DELAY),
Params("1to3, not enough power, start timer", max_current_single_phase=16, timestamp_auto_phase_switch=None,
Expand All @@ -76,7 +76,7 @@ def __init__(self,
timestamp_auto_phase_switch=1652682952.0, phases_to_use=1, required_current=6,
evu_surplus=-1200, reserved_evu_overhang=460, get_currents=[15.6, 0, 0], get_power=3450,
state=ChargepointState.PHASE_SWITCH_DELAY, expected_phases_to_use=1, expected_current=6,
expected_message=Ev.PHASE_SWITCH_DELAY_TEXT.format("Umschaltung von 1 auf 3", "2 Min. 0 Sek."),
expected_message=Ev.PHASE_SWITCH_DELAY_TEXT.format("Umschaltung von 1 auf 3", "2 Min."),
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.PHASE_SWITCH_DELAY),
Params("1to3, not enough power, timer not expired", max_current_single_phase=16,
Expand All @@ -96,15 +96,15 @@ def __init__(self,
phases_to_use=3, required_current=6, evu_surplus=0, reserved_evu_overhang=0,
get_currents=[4.5, 4.4, 5.8], get_power=3381, state=ChargepointState.CHARGING_ALLOWED,
expected_phases_to_use=3, expected_current=6,
expected_message="Umschaltung von 3 auf 1 Phasen in 9 Min. 0 Sek..",
expected_message="Umschaltung von 3 auf 1 Phasen in 9 Min..",
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.PHASE_SWITCH_DELAY),
Params("3to1, not enough power, timer not expired", max_current_single_phase=16,
timestamp_auto_phase_switch=1652682952.0,
phases_to_use=3, required_current=6, evu_surplus=0, reserved_evu_overhang=-460,
get_currents=[4.5, 4.4, 5.8], get_power=3381, state=ChargepointState.PHASE_SWITCH_DELAY,
expected_phases_to_use=3, expected_current=6,
expected_message="Umschaltung von 3 auf 1 Phasen in 4 Min. 0 Sek..",
expected_message="Umschaltung von 3 auf 1 Phasen in 4 Min..",
expected_timestamp_auto_phase_switch=1652683252.0,
expected_state=ChargepointState.PHASE_SWITCH_DELAY),
Params("3to1, enough power, timer not expired", max_current_single_phase=16,
Expand Down
13 changes: 9 additions & 4 deletions packages/helpermodules/timecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import copy
import logging
import datetime
import math
from dateutil.relativedelta import relativedelta
from typing import Dict, List, Optional, Tuple, TypeVar, Union

Expand Down Expand Up @@ -344,6 +343,12 @@ def convert_timedelta_to_time_string(timedelta_obj: datetime.timedelta) -> str:


def convert_timestamp_delta_to_time_string(timestamp: int, delta: int) -> str:
diff = delta - (create_timestamp() - timestamp)
minute_diff = int(diff/60)
return f"{f'{minute_diff} Min. ' if minute_diff > 0 else ''}{math.ceil(diff%60)} Sek."
diff = int(delta - (create_timestamp() - timestamp))
seconds_diff = diff % 60
minute_diff = int((diff - seconds_diff) / 60)
if minute_diff > 0 and seconds_diff > 0:
return f"{minute_diff} Min. {seconds_diff} Sek."
elif minute_diff > 0:
return f"{minute_diff} Min."
elif seconds_diff > 0:
return f"{seconds_diff} Sek."
15 changes: 12 additions & 3 deletions packages/helpermodules/timecheck_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,21 @@ def test_check_timeframe(plan: Union[AutolockPlan, TimeChargingPlan], now: str,
assert state == expected_state


def test_convert_timestamp_delta_to_time_string():
@pytest.mark.parametrize("timestamp, expected",
[
pytest.param(1652683202, "40 Sek."),
pytest.param(1652683222, "1 Min."),
pytest.param(1652683221.8, "59 Sek."),
pytest.param(1652683222.2, "1 Min."),
pytest.param(1652683232, "1 Min. 10 Sek.")
]
)
def test_convert_timestamp_delta_to_time_string(timestamp, expected):
# setup
delta = 90

# execution
time_string = timecheck.convert_timestamp_delta_to_time_string(1652683202, delta)
time_string = timecheck.convert_timestamp_delta_to_time_string(timestamp, delta)

# evaluation
assert time_string == "40 Sek."
assert time_string == expected