Skip to content

Commit

Permalink
fix: When using the join saving session service, related data updates…
Browse files Browse the repository at this point in the history
… are forced in the next cycle
  • Loading branch information
BottlecapDave committed Nov 25, 2023
1 parent 08af21b commit 626c1c1
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 18 deletions.
4 changes: 2 additions & 2 deletions _docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- [Gas Entities](#gas-entities)
- [Home Mini](#home-mini)
- [Intelligent](#intelligent)
- [Saving Sessions](#saving-sessions)
- [Octoplus](#octoplus)
- [Wheel of Fortune](#wheel-of-fortune)
- [Target Rate Sensors](#target-rate-sensors)
- [Events](#events)
Expand Down Expand Up @@ -47,7 +47,7 @@ If you are on the [intelligent tariff](https://octopus.energy/smart/intelligent-

> Please note: If you switch to the intelligent tariff after you have installed the integration, you will need to reload the integration or restart your Home Assistant instance.
### Saving Sessions
### Octoplus

To support Octopus Energy's [octoplus programme](https://octopus.energy/octoplus/). [Full list of octoplus entites](./entities/octoplus.md).

Expand Down
2 changes: 1 addition & 1 deletion _docs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ automations:
## join_octoplus_saving_session_event
Service for joining a new saving session event
Service for joining a new saving session event. When used, it may take a couple of minutes for the other sensors to refresh the changes.
| Attribute | Optional | Description |
| ------------------------ | -------- | --------------------------------------------------------------------------------------------------------------------- |
Expand Down
2 changes: 2 additions & 0 deletions custom_components/octopus_energy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
DATA_GAS_STANDING_CHARGE_KEY = "GAS_STANDING_CHARGES_{}_{}"
DATA_WHEEL_OF_FORTUNE_SPINS = "WHEEL_OF_FORTUNE_SPINS"

DATA_SAVING_SESSIONS_FORCE_UPDATE = "SAVING_SESSIONS_FORCE_UPDATE"

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

STORAGE_COMPLETED_DISPATCHES_NAME = "octopus_energy.{}-completed-intelligent-dispatches.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from ..const import (
COORDINATOR_REFRESH_IN_SECONDS,
DATA_SAVING_SESSIONS_FORCE_UPDATE,
DOMAIN,
DATA_CLIENT,
DATA_ACCOUNT_ID,
Expand Down Expand Up @@ -110,16 +111,18 @@ async def async_update_saving_sessions():
# Only get data every half hour or if we don't have any data
current = now()
client: OctopusEnergyApiClient = hass.data[DOMAIN][DATA_CLIENT]
force_update = hass.data[DOMAIN][DATA_SAVING_SESSIONS_FORCE_UPDATE] if DATA_SAVING_SESSIONS_FORCE_UPDATE in hass.data[DOMAIN] else False

result = await async_refresh_saving_sessions(
current,
client,
account_id,
hass.data[DOMAIN][DATA_SAVING_SESSIONS] if DATA_SAVING_SESSIONS in hass.data[DOMAIN] else None,
hass.data[DOMAIN][DATA_SAVING_SESSIONS] if DATA_SAVING_SESSIONS in hass.data[DOMAIN] and force_update == False else None,
hass.bus.async_fire
)

hass.data[DOMAIN][DATA_SAVING_SESSIONS] = result
hass.data[DOMAIN][DATA_SAVING_SESSIONS_FORCE_UPDATE] = False
return hass.data[DOMAIN][DATA_SAVING_SESSIONS]

hass.data[DOMAIN][DATA_SAVING_SESSIONS_COORDINATOR] = DataUpdateCoordinator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)
from homeassistant.helpers.restore_state import RestoreEntity

from ..const import EVENT_ALL_SAVING_SESSIONS
from ..const import DATA_SAVING_SESSIONS_FORCE_UPDATE, DOMAIN, EVENT_ALL_SAVING_SESSIONS

from ..api_client import OctopusEnergyApiClient
from ..utils.attributes import dict_to_typed_dict
Expand Down Expand Up @@ -68,16 +68,4 @@ async def async_join_saving_session_event(self, event_code: str):
if (result.is_successful == False):
raise Exception(result.errors[0])
else:
attributes = self.state_attributes
if ("available_events" in attributes and attributes["available_events"] is not None):
new_available_events = []
for available_event in attributes["available_events"]:
if (available_event["code"] != event_code):
new_available_events.append(available_event)

attributes["available_events"] = new_available_events
self._trigger_event(
EVENT_ALL_SAVING_SESSIONS,
attributes
)
self.async_write_ha_state()
self._hass.data[DOMAIN][DATA_SAVING_SESSIONS_FORCE_UPDATE] = True

0 comments on commit 626c1c1

Please sign in to comment.