Skip to content

Commit

Permalink
fix: Fixed dispatching sensor incorrectly turning on when bump charge…
Browse files Browse the repository at this point in the history
…s are activated
  • Loading branch information
BottlecapDave committed Feb 17, 2024
1 parent 3716d23 commit b37e4b1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion _docs/entities/intelligent.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ If you are on the [intelligent tariff](https://octopus.energy/smart/intelligent-

`binary_sensor.octopus_energy_{{ACCOUNT_ID}}_intelligent_dispatching`

This sensor is used to determine if you're currently in a planned dispatch period (i.e. "smart-charge" determined by Octopus Energy) or are within the standard off peak period.
This sensor is used to determine if you're currently in a planned dispatch period (i.e. "smart-charge" determined by Octopus Energy) or are within the standard off peak period. This sensor **will not** come on during a bump charge.

!!! warning

Expand Down
3 changes: 3 additions & 0 deletions custom_components/octopus_energy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@

STORAGE_COMPLETED_DISPATCHES_NAME = "octopus_energy.{}-completed-intelligent-dispatches.json"

INTELLIGENT_SOURCE_SMART_CHARGE = "smart-charge"
INTELLIGENT_SOURCE_BUMP_CHARGE = "bump-charge"

REGEX_HOURS = "^[0-9]+(\\.[0-9]+)*$"
REGEX_TIME = "^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$"
REGEX_ENTITY_NAME = "^[a-z0-9_]+$"
Expand Down
24 changes: 16 additions & 8 deletions custom_components/octopus_energy/intelligent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..utils import OffPeakTime, get_active_tariff_code, get_tariff_parts

from ..const import DOMAIN, REFRESH_RATE_IN_MINUTES_INTELLIGENT
from ..const import DOMAIN, INTELLIGENT_SOURCE_BUMP_CHARGE, INTELLIGENT_SOURCE_SMART_CHARGE, REFRESH_RATE_IN_MINUTES_INTELLIGENT

from ..api_client.intelligent_settings import IntelligentSettings
from ..api_client.intelligent_dispatches import IntelligentDispatchItem, IntelligentDispatches
Expand Down Expand Up @@ -36,14 +36,22 @@ def mock_intelligent_dispatches() -> IntelligentDispatches:
utcnow().replace(hour=19, minute=0, second=0, microsecond=0),
utcnow().replace(hour=20, minute=0, second=0, microsecond=0),
1,
"smart-charge",
INTELLIGENT_SOURCE_SMART_CHARGE,
"home"
),
IntelligentDispatchItem(
utcnow().replace(hour=7, minute=0, second=0, microsecond=0),
utcnow().replace(hour=8, minute=0, second=0, microsecond=0),
4.6,
"smart-charge",
INTELLIGENT_SOURCE_SMART_CHARGE,
"home"
),

IntelligentDispatchItem(
utcnow().replace(hour=12, minute=0, second=0, microsecond=0),
utcnow().replace(hour=13, minute=0, second=0, microsecond=0),
4.6,
INTELLIGENT_SOURCE_BUMP_CHARGE,
"home"
)
]
Expand All @@ -55,7 +63,7 @@ def mock_intelligent_dispatches() -> IntelligentDispatches:
utcnow().replace(hour=10, minute=10, second=0, microsecond=0),
utcnow().replace(hour=10, minute=30, second=0, microsecond=0),
1.2,
"smart-charge",
INTELLIGENT_SOURCE_SMART_CHARGE,
"home"
)
)
Expand All @@ -66,7 +74,7 @@ def mock_intelligent_dispatches() -> IntelligentDispatches:
utcnow().replace(hour=18, minute=0, second=0, microsecond=0),
utcnow().replace(hour=18, minute=20, second=0, microsecond=0),
1.2,
"smart-charge",
INTELLIGENT_SOURCE_SMART_CHARGE,
"home"
)
)
Expand Down Expand Up @@ -141,7 +149,7 @@ def adjust_intelligent_rates(rates, planned_dispatches: list[IntelligentDispatch
adjusted_rates.append(rate)
continue

if __get_dispatch(rate, planned_dispatches, "smart-charge") is not None or __get_dispatch(rate, completed_dispatches, None) is not None:
if __get_dispatch(rate, planned_dispatches, INTELLIGENT_SOURCE_SMART_CHARGE) is not None or __get_dispatch(rate, completed_dispatches, None) is not None:
adjusted_rates.append({
"start": rate["start"],
"end": rate["end"],
Expand All @@ -157,13 +165,13 @@ def adjust_intelligent_rates(rates, planned_dispatches: list[IntelligentDispatch
def is_in_planned_dispatch(current_date: datetime, dispatches: list[IntelligentDispatchItem]) -> bool:
for dispatch in dispatches:
if (dispatch.start <= current_date and dispatch.end >= current_date):
return True
return dispatch.source == INTELLIGENT_SOURCE_SMART_CHARGE

return False

def is_in_bump_charge(current_date: datetime, dispatches: list[IntelligentDispatchItem]) -> bool:
for dispatch in dispatches:
if (dispatch.source == "bump-charge" and dispatch.start <= current_date and dispatch.end >= current_date):
if (dispatch.source == INTELLIGENT_SOURCE_BUMP_CHARGE and dispatch.start <= current_date and dispatch.end >= current_date):
return True

return False
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/intelligent/test_is_in_planned_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
(parse_datetime("2023-06-07T17:00:00Z"), True),
(parse_datetime("2023-06-07T18:00:00+01:00"), True),
# During bump charge
(parse_datetime("2023-06-07T18:00:00Z"), False),
(parse_datetime("2023-06-07T19:00:00+01:00"), False),
(parse_datetime("2023-06-07T19:00:00Z"), False),
(parse_datetime("2023-06-07T20:00:00+01:00"), False),
(parse_datetime("2023-06-07T15:59:59Z"), False),
(parse_datetime("2023-06-07T16:59:59+01:00"), False),
(parse_datetime("2023-06-07T17:00:01Z"), False),
Expand Down Expand Up @@ -60,6 +66,13 @@ async def test_when_dispatch_is_now_then_is_in_planned_dispatch_returns_correctl
1,
"smart-charge",
"home"
),
IntelligentDispatchItem(
as_utc(parse_datetime("2023-06-07T18:00:00Z")),
as_utc(parse_datetime("2023-06-07T19:00:00Z")),
1,
"bump-charge",
"home"
)
]

Expand Down

0 comments on commit b37e4b1

Please sign in to comment.