Skip to content

Commit

Permalink
fix: Fixed target rate sensor picking times an hour before/after requ…
Browse files Browse the repository at this point in the history
…ested timeframe - timezone issue
  • Loading branch information
BottlecapDave committed Apr 6, 2024
1 parent 3b228c9 commit 3651780
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
5 changes: 3 additions & 2 deletions custom_components/octopus_energy/target_rates/target_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def _handle_coordinator_update(self) -> None:
account_result = self._hass.data[DOMAIN][self._account_id][DATA_ACCOUNT]
account_info = account_result.account if account_result is not None else None

check_for_errors(self._hass, self._config, account_info, now())
current_local_date = now()
check_for_errors(self._hass, self._config, account_info, current_local_date)

# Find the current rate. Rates change a maximum of once every 30 minutes.
current_date = utcnow()
Expand Down Expand Up @@ -173,7 +174,7 @@ def _handle_coordinator_update(self) -> None:
find_highest_rates = (self._is_export and invert_target_rates == False) or (self._is_export == False and invert_target_rates)

applicable_rates = get_applicable_rates(
current_date,
current_local_date,
start_time,
end_time,
all_rates,
Expand Down
40 changes: 38 additions & 2 deletions tests/unit/target_rates/test_get_applicable_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ async def test_when_start_time_and_end_time_is_same_then_rates_are_shifted():
assert item["end"] == expected_first_valid_from + timedelta(minutes=30)
expected_first_valid_from = item["end"]


@pytest.mark.asyncio
async def test_when_start_time_is_after_end_time_and_rolling_target_then_rates_are_overnight():
# Arrange
Expand Down Expand Up @@ -254,4 +253,41 @@ async def test_when_available_rates_are_too_low_then_no_times_are_returned():
)

# Assert
assert result is None
assert result is None

@pytest.mark.asyncio
async def test_when_times_are_in_bst_then_rates_are_shifted():
# Arrange
current_date = datetime.strptime("2024-04-06T17:10:00+01:00", "%Y-%m-%dT%H:%M:%S%z")
target_start_time = "16:00"
target_end_time = "21:00"

period_from = datetime.strptime("2024-04-06T00:00:00+00:00", "%Y-%m-%dT%H:%M:%S%z")
period_to = datetime.strptime("2024-04-07T00:00:00+00:00", "%Y-%m-%dT%H:%M:%S%z")
expected_rates = [0.1, 0.2, 0.3, 0.2, 0.2, 0.1]

rates = create_rate_data(
period_from,
period_to,
expected_rates
)

# Act
result = get_applicable_rates(
current_date,
target_start_time,
target_end_time,
rates,
False
)

# Assert
assert result is not None
assert len(result) == 10
expected_first_valid_from = datetime.strptime("2024-04-06T15:00:00+00:00", "%Y-%m-%dT%H:%M:%S%z")
for item in result:
assert item["start"] == expected_first_valid_from
assert item["end"] == expected_first_valid_from + timedelta(minutes=30)
expected_first_valid_from = item["end"]

assert expected_first_valid_from == datetime.strptime("2024-04-06T20:00:00+00:00", "%Y-%m-%dT%H:%M:%S%z")

0 comments on commit 3651780

Please sign in to comment.