Skip to content

Commit

Permalink
feat: Added octopoints_per_kwh to joined saving session events (if kn…
Browse files Browse the repository at this point in the history
…own)
  • Loading branch information
BottlecapDave committed Nov 22, 2023
1 parent e10c6dd commit 350ad20
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions _docs/entities/octoplus.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Each joined event item will include the following attributes
| `start` | `datetime` | The date/time the event starts |
| `end` | `datetime` | The date/time the event starts |
| `rewarded_octopoints` | `integer` | The total number of octopoints that were awarded (if any or known) |
| `octopoints_per_kwh` | `integer` | The number of octopoints that are/were awarded per kwh saved during the event (if known) |

## Services

Expand Down
24 changes: 18 additions & 6 deletions custom_components/octopus_energy/coordinators/saving_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ async def async_refresh_saving_sessions(
"event_octopoints_per_kwh": available_event.octopoints
})

joined_events = []
for ev in result.joined_events:
# Find original event so we can retrieve the octopoints per kwh
original_event = None
for available_event in result.available_events:
if (available_event.id == ev.id):
original_event = available_event
break

joined_events.append({
"id": ev.id,
"start": ev.start,
"end": ev.end,
"rewarded_octopoints": ev.octopoints,
"octopoints_per_kwh": original_event.octopoints if original_event is not None else None
})

fire_event(EVENT_ALL_SAVING_SESSIONS, {
"account_id": account_id,
"available_events": list(map(lambda ev: {
Expand All @@ -88,12 +105,7 @@ async def async_refresh_saving_sessions(
"end": ev.end,
"octopoints_per_kwh": ev.octopoints
}, available_events)),
"joined_events": list(map(lambda ev: {
"id": ev.id,
"start": ev.start,
"end": ev.end,
"rewarded_octopoints": ev.octopoints
}, result.joined_events)),
"joined_events": joined_events,
})

return SavingSessionsCoordinatorResult(current, available_events, result.joined_events)
Expand Down
23 changes: 17 additions & 6 deletions tests/unit/coordinators/test_async_refresh_saving_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def assert_raised_new_saving_session_event(
def assert_raised_all_saving_session_event(
raised_event: dict,
account_id: str,
all_available_events: list[SavingSession],
available_events: list[SavingSession],
joined_events: list[SavingSession]
):
Expand Down Expand Up @@ -73,6 +74,16 @@ def assert_raised_all_saving_session_event(
assert "rewarded_octopoints" in actual_event
assert actual_event["rewarded_octopoints"] == expected_event.octopoints

expected_available_event = None
for event in all_available_events:
if event.id == expected_event.id:
expected_available_event = event
break

assert expected_available_event is not None
assert "octopoints_per_kwh" in actual_event
assert actual_event["octopoints_per_kwh"] == expected_available_event.octopoints

@pytest.mark.asyncio
async def test_when_now_is_not_at_30_minute_mark_and_previous_data_is_available_then_previous_data_returned():
# Arrange
Expand Down Expand Up @@ -146,7 +157,7 @@ async def async_mocked_get_saving_sessions(*args, **kwargs):
assert result is not None

assert len(actual_fired_events) == 1
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [], [])
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [], [], [])

@pytest.mark.asyncio
@pytest.mark.parametrize("minutes",[
Expand Down Expand Up @@ -187,7 +198,7 @@ async def async_mocked_get_saving_sessions(*args, **kwargs):
assert result is not None

assert len(actual_fired_events) == 1
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [], [expected_saving_session])
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [expected_saving_session], [], [expected_saving_session])

@pytest.mark.asyncio
@pytest.mark.parametrize("minutes",[
Expand Down Expand Up @@ -229,7 +240,7 @@ async def async_mocked_get_saving_sessions(*args, **kwargs):

assert len(actual_fired_events) == 2
assert_raised_new_saving_session_event(actual_fired_events[EVENT_NEW_SAVING_SESSION], account_id, expected_saving_session)
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [expected_saving_session], [])
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [expected_saving_session], [expected_saving_session], [])

@pytest.mark.asyncio
@pytest.mark.parametrize("minutes",[
Expand Down Expand Up @@ -271,7 +282,7 @@ async def async_mocked_get_saving_sessions(*args, **kwargs):

assert len(actual_fired_events) == 2
assert_raised_new_saving_session_event(actual_fired_events[EVENT_NEW_SAVING_SESSION], account_id, expected_saving_session)
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [expected_saving_session], [])
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [expected_saving_session], [expected_saving_session], [])

@pytest.mark.asyncio
@pytest.mark.parametrize("minutes",[
Expand Down Expand Up @@ -313,7 +324,7 @@ async def async_mocked_get_saving_sessions(*args, **kwargs):
assert result is not None

assert len(actual_fired_events) == 1
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [expected_saving_session], [])
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [expected_saving_session], [expected_saving_session], [])

@pytest.mark.asyncio
@pytest.mark.parametrize("minutes",[
Expand Down Expand Up @@ -356,7 +367,7 @@ async def async_mocked_get_saving_sessions(*args, **kwargs):

assert len(actual_fired_events) == 2
assert_raised_new_saving_session_event(actual_fired_events[EVENT_NEW_SAVING_SESSION], account_id, expected_saving_session)
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [expected_saving_session], [])
assert_raised_all_saving_session_event(actual_fired_events[EVENT_ALL_SAVING_SESSIONS], account_id, [expected_saving_session], [expected_saving_session], [])

@pytest.mark.asyncio
async def test_when_previous_data_is_out_of_date_then_new_date_is_retrieved():
Expand Down

0 comments on commit 350ad20

Please sign in to comment.