Skip to content

Commit 6463d8b

Browse files
fix: Updated API for wheel of fortune which now supports claiming octopoints (25 minutes dev time)
1 parent 5f1dc1b commit 6463d8b

File tree

5 files changed

+37
-32
lines changed

5 files changed

+37
-32
lines changed

_docs/services.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ For automation examples, please refer to the available [blueprints](./blueprints
178178

179179
This service allows the user to perform a spin on the [wheel of fortune](./entities/wheel_of_fortune.md) that is awarded to users every month. No point letting them go to waste :)
180180

181-
!!! warning
182-
183-
Due to an ongoing issue with the underlying API, this will not award octopoints if used. If you are on Octoplus, it is advised not to use this service.
184-
185181
| Attribute | Optional | Description |
186182
| ------------------------ | -------- | --------------------------------------------------------------------------------------------------------------------- |
187183
| `target.entity_id` | `no` | The name of the wheel of fortune sensor that represents the type of spin to be made. This should always point at one of the [wheel of fortune sensors](./entities/wheel_of_fortune.md) entities. |

custom_components/octopus_energy/api_client/__init__.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -301,20 +301,18 @@
301301
}}'''
302302

303303
wheel_of_fortune_query = '''query {{
304-
wheelOfFortuneSpins(accountNumber: "{account_id}") {{
305-
electricity {{
306-
remainingSpinsThisMonth
307-
}}
308-
gas {{
309-
remainingSpinsThisMonth
310-
}}
304+
electricity: wheelOfFortuneSpinsAllowed(fuelType:ELECTRICITY, accountNumber: "{account_id}") {{
305+
spinsAllowed
306+
}}
307+
gas: wheelOfFortuneSpinsAllowed(fuelType:GAS, accountNumber: "{account_id}") {{
308+
spinsAllowed
311309
}}
312310
}}'''
313311

314312
wheel_of_fortune_mutation = '''mutation {{
315-
spinWheelOfFortune(input: {{ accountNumber: "{account_id}", supplyType: {supply_type}, termsAccepted: true }}) {{
316-
spinResult {{
317-
prizeAmount
313+
spinWheelOfFortune(input: {{ accountNumber: "{account_id}", fuelType: {fuel_type} }}) {{
314+
prize {{
315+
value
318316
}}
319317
}}
320318
}}'''
@@ -643,6 +641,7 @@ def __init__(self, api_key, electricity_price_cap = None, gas_price_cap = None,
643641

644642
self._api_key = api_key
645643
self._base_url = 'https://api.octopus.energy'
644+
self._backend_base_url = 'https://api.backend.octopus.energy'
646645

647646
self._graphql_token = None
648647
self._graphql_expiration = None
@@ -1752,20 +1751,21 @@ async def async_get_wheel_of_fortune_spins(self, account_id: str) -> WheelOfFort
17521751
try:
17531752
request_context = "wheel-of-fortune"
17541753
client = self._create_client_session()
1755-
url = f'{self._base_url}/v1/graphql/'
1754+
url = f'{self._backend_base_url}/v1/graphql/'
17561755
payload = { "query": wheel_of_fortune_query.format(account_id=account_id) }
1757-
headers = { "Authorization": f"JWT {self._graphql_token}", integration_context_header: request_context }
1756+
headers = { "Authorization": f"{self._graphql_token}", integration_context_header: request_context }
17581757
async with client.post(url, json=payload, headers=headers) as response:
17591758
response_body = await self.__async_read_response__(response, url)
17601759
_LOGGER.debug(f'async_get_wheel_of_fortune_spins: {response_body}')
17611760

17621761
if (response_body is not None and "data" in response_body and
1763-
"wheelOfFortuneSpins" in response_body["data"]):
1762+
"electricity" in response_body["data"] and
1763+
"gas" in response_body["data"]):
17641764

1765-
spins = response_body["data"]["wheelOfFortuneSpins"]
1765+
spins = response_body["data"]
17661766
return WheelOfFortuneSpinsResponse(
1767-
int(spins["electricity"]["remainingSpinsThisMonth"]) if "electricity" in spins and "remainingSpinsThisMonth" in spins["electricity"] else 0,
1768-
int(spins["gas"]["remainingSpinsThisMonth"]) if "gas" in spins and "remainingSpinsThisMonth" in spins["gas"] else 0
1767+
int(spins["electricity"]["spinsAllowed"]) if "electricity" in spins and "spinsAllowed" in spins["electricity"] else 0,
1768+
int(spins["gas"]["spinsAllowed"]) if "gas" in spins and "spinsAllowed" in spins["gas"] else 0
17691769
)
17701770
else:
17711771
_LOGGER.error("Failed to retrieve wheel of fortune spins")
@@ -1783,20 +1783,20 @@ async def async_spin_wheel_of_fortune(self, account_id: str, is_electricity: boo
17831783
try:
17841784
request_context = "spin-wheel-of-fortune"
17851785
client = self._create_client_session()
1786-
url = f'{self._base_url}/v1/graphql/'
1787-
payload = { "query": wheel_of_fortune_mutation.format(account_id=account_id, supply_type="ELECTRICITY" if is_electricity == True else "GAS") }
1788-
headers = { "Authorization": f"JWT {self._graphql_token}", integration_context_header: request_context }
1786+
url = f'{self._backend_base_url}/v1/graphql/'
1787+
payload = { "query": wheel_of_fortune_mutation.format(account_id=account_id, fuel_type="ELECTRICITY" if is_electricity == True else "GAS") }
1788+
headers = { "Authorization": f"{self._graphql_token}", integration_context_header: request_context }
17891789
async with client.post(url, json=payload, headers=headers) as response:
17901790
response_body = await self.__async_read_response__(response, url)
17911791
_LOGGER.debug(f'async_spin_wheel_of_fortune: {response_body}')
17921792

17931793
if (response_body is not None and
17941794
"data" in response_body and
17951795
"spinWheelOfFortune" in response_body["data"] and
1796-
"spinResult" in response_body["data"]["spinWheelOfFortune"] and
1797-
"prizeAmount" in response_body["data"]["spinWheelOfFortune"]["spinResult"]):
1796+
"prize" in response_body["data"]["spinWheelOfFortune"] and
1797+
"value" in response_body["data"]["spinWheelOfFortune"]["prize"]):
17981798

1799-
return int(response_body["data"]["spinWheelOfFortune"]["spinResult"]["prizeAmount"])
1799+
return int(response_body["data"]["spinWheelOfFortune"]["prize"]["value"])
18001800
else:
18011801
_LOGGER.error("Failed to spin wheel of fortune")
18021802

custom_components/octopus_energy/api_client/intelligent_dispatches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(
2222
self.source = source
2323
self.location = location
2424

25-
def to_dict(self, ignore_none: bool):
25+
def to_dict(self, ignore_none: bool = True):
2626
data = {
2727
"start": self.start,
2828
"end": self.end,

custom_components/octopus_energy/intelligent/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,15 @@ def dictionary_list_to_dispatches(dispatches: list):
215215

216216
return items
217217

218-
def dispatches_to_dictionary_list(dispatches: list[IntelligentDispatchItem]):
218+
def dispatches_to_dictionary_list(dispatches: list[IntelligentDispatchItem], ignore_none: bool):
219+
items = []
220+
if (dispatches is not None):
221+
for dispatch in dispatches:
222+
items.append(dispatch.to_dict(ignore_none))
223+
224+
return items
225+
226+
def simple_dispatches_to_dictionary_list(dispatches: list[SimpleIntelligentDispatchItem]):
219227
items = []
220228
if (dispatches is not None):
221229
for dispatch in dispatches:

custom_components/octopus_energy/intelligent/dispatching.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from homeassistant.helpers.restore_state import RestoreEntity
1616

1717
from ..intelligent import (
18-
dispatches_to_dictionary_list
18+
dispatches_to_dictionary_list,
19+
simple_dispatches_to_dictionary_list
1920
)
2021

2122
from ..utils import get_off_peak_times
@@ -95,9 +96,9 @@ def _handle_coordinator_update(self) -> None:
9596
current_date = utcnow()
9697

9798
self.__init_attributes__(
98-
dispatches_to_dictionary_list(result.dispatches.planned) if result is not None else [],
99-
dispatches_to_dictionary_list(result.dispatches.completed if result is not None and result.dispatches is not None else []) if result is not None else [],
100-
dispatches_to_dictionary_list(result.dispatches.started if result is not None and result.dispatches is not None else []) if result is not None else [],
99+
dispatches_to_dictionary_list(result.dispatches.planned, ignore_none=True) if result is not None else [],
100+
dispatches_to_dictionary_list(result.dispatches.completed if result is not None and result.dispatches is not None else [], ignore_none=False) if result is not None else [],
101+
simple_dispatches_to_dictionary_list(result.dispatches.started if result is not None and result.dispatches is not None else []) if result is not None else [],
101102
)
102103

103104
off_peak_times = get_off_peak_times(current_date, rates, True)

0 commit comments

Comments
 (0)