Skip to content

Commit 9696351

Browse files
authored
Merge pull request #361 from fersingb/fix_rewards
Moved to the new reward API
2 parents 3098dd5 + 6b98123 commit 9696351

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

pyhilo/api.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ async def async_get_access_token(self) -> str:
153153
@property
154154
def urn(self) -> str | None:
155155
"""Extract URN from the JWT access token."""
156+
if self._urn is not None:
157+
return self._urn
158+
156159
try:
157160
if not self._oauth_session.valid_token:
158161
return None
@@ -170,7 +173,7 @@ def urn(self) -> str | None:
170173
self._urn = urn_claim[0] # Get the first URN from the array
171174
else:
172175
self._urn = None
173-
176+
174177
return self._urn
175178
except (IndexError, json.JSONDecodeError, KeyError):
176179
LOG.error("Failed to extract URN from access token")
@@ -299,11 +302,12 @@ async def _async_request(
299302
def _get_url(
300303
self,
301304
endpoint: Union[str, None],
302-
location_id: int,
303305
gd: bool = False,
304306
drms: bool = False,
305307
events: bool = False,
306308
challenge: bool = False,
309+
location_id: Union[int, None] = None,
310+
urn: Union[str, None] = None,
307311
) -> str:
308312
"""Generate a path to the requested endpoint.
309313
@@ -329,9 +333,12 @@ def _get_url(
329333
base = API_EVENTS_ENDPOINT + API_NOTIFICATIONS_ENDPOINT
330334
if challenge:
331335
base = API_CHALLENGE_ENDPOINT
332-
url = base + "/Locations/" + str(location_id)
336+
337+
url = base + (f"/Locations/{urn}" if urn else f"/Locations/{location_id}")
338+
333339
if endpoint:
334-
url += "/" + str(endpoint)
340+
url += f"/{endpoint}"
341+
335342
return url
336343

337344
async def _async_handle_on_backoff(self, _: dict[str, Any]) -> None:
@@ -537,7 +544,7 @@ async def get_location_ids(self) -> tuple[int, str]:
537544

538545
async def get_devices(self, location_id: int) -> list[dict[str, Any]]:
539546
"""Get list of all devices"""
540-
url = self._get_url("Devices", location_id)
547+
url = self._get_url("Devices", location_id=location_id)
541548
LOG.debug("Devices URL is %s", url)
542549
devices: list[dict[str, Any]] = await self.async_request("get", url)
543550
devices.append(await self.get_gateway(location_id))
@@ -554,7 +561,9 @@ async def _set_device_attribute(
554561
value: Union[str, float, int, None],
555562
) -> None:
556563
"""Sets device attributes"""
557-
url = self._get_url(f"Devices/{device.id}/Attributes", device.location_id)
564+
url = self._get_url(
565+
f"Devices/{device.id}/Attributes", location_id=device.location_id
566+
)
558567
LOG.debug("Device Attribute URL is %s", url)
559568
await self.async_request("put", url, json={key.hilo_attribute: value})
560569

@@ -582,7 +591,7 @@ async def get_event_notifications(self, location_id: int) -> dict[str, Any]:
582591
"notificationDataJSON": "{\"NotificationType\":null,\"Title\":\"\",\"SubTitle\":null,\"Body\":\"Test manuel de l’alarme détecté.\",\"Badge\":0,\"Sound\":null,\"Data\":null,\"Tags\":null,\"Type\":\"TestDetected\",\"DeviceId\":324236,\"LocationId\":4051}",
583592
"viewed": false
584593
}"""
585-
url = self._get_url(None, location_id, events=True)
594+
url = self._get_url(None, events=True, location_id=location_id)
586595
LOG.debug("Event Notifications URL is %s", url)
587596
return cast(dict[str, Any], await self.async_request("get", url))
588597

@@ -650,7 +659,7 @@ async def get_gd_events(
650659
}
651660
"""
652661
# ic-dev21 need to check but this is probably dead code
653-
url = self._get_url("Events", location_id, True)
662+
url = self._get_url("Events", True, location_id=location_id)
654663
if not event_id:
655664
url += "?active=true"
656665
else:
@@ -659,7 +668,8 @@ async def get_gd_events(
659668
LOG.debug("get_gd_events URL is %s", url)
660669
return cast(dict[str, Any], await self.async_request("get", url))
661670

662-
async def get_seasons(self, location_id: int) -> dict[str, Any]:
671+
# keep location_id for now for backward compatibility with existing hilo branch
672+
async def get_seasons(self, location_id: int) -> list[dict[str, Any]]:
663673
"""This will return the rewards and current season total
664674
https://api.hiloenergie.com/challenge/v1/api/Locations/XXXX/Seasons
665675
[
@@ -678,13 +688,34 @@ async def get_seasons(self, location_id: int) -> dict[str, Any]:
678688
}
679689
]
680690
"""
681-
url = self._get_url("Seasons", location_id, challenge=True)
691+
url = self._get_url("seasonssummary", challenge=True, urn=self.urn)
682692
LOG.debug("Seasons URL is %s", url)
683-
return cast(dict[str, Any], await self.async_request("get", url))
693+
694+
seasons = await self.async_request("get", url)
695+
LOG.debug("Seasons API response: %s", seasons)
696+
697+
all_seasons: list[dict[str, Any]] = []
698+
699+
for season_data in seasons:
700+
season = season_data.get("season")
701+
ratePlan = season_data.get("ratePlan")
702+
periodId = season_data.get("periodId")
703+
704+
url = self._get_url(
705+
f"rates/{ratePlan}/seasons/{season}/events?periodId={periodId}",
706+
challenge=True,
707+
urn=self.urn,
708+
)
709+
LOG.debug("Seasons Events URL is %s", url)
710+
season_events = await self.async_request("get", url)
711+
LOG.debug("Season %s Events API response: %s", season, season_events)
712+
all_seasons.append(season_events)
713+
714+
return all_seasons
684715

685716
async def get_gateway(self, location_id: int) -> dict[str, Any]:
686717
"""Gets info about the Hilo hub (gateway)"""
687-
url = self._get_url("Gateways/Info", location_id)
718+
url = self._get_url("Gateways/Info", location_id=location_id)
688719
LOG.debug("Gateway URL is %s", url)
689720
req = await self.async_request("get", url)
690721
saved_attrs = [
@@ -727,7 +758,7 @@ async def get_weather(self, location_id: int) -> dict[str, Any]:
727758
}
728759
]
729760
"""
730-
url = self._get_url("Weather", location_id)
761+
url = self._get_url("Weather", location_id=location_id)
731762
LOG.debug("Weather URL is %s", url)
732763
response = await self.async_request("get", url)
733764
LOG.debug("Weather API response: %s", response)

pyhilo/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
LOG: Final = logging.getLogger(__package__)
88
DEFAULT_STATE_FILE: Final = "hilo_state.yaml"
99
REQUEST_RETRY: Final = 9
10-
PYHILO_VERSION: Final = "2025.12.04"
10+
PYHILO_VERSION: Final = "2025.12.05"
1111
# TODO: Find a way to keep previous line in sync with pyproject.toml automatically
1212

1313
CONTENT_TYPE_FORM: Final = "application/x-www-form-urlencoded"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ exclude = ".venv/.*"
4040

4141
[tool.poetry]
4242
name = "python-hilo"
43-
version = "2025.12.4"
43+
version = "2025.12.5"
4444
description = "A Python3, async interface to the Hilo API"
4545
readme = "README.md"
4646
authors = ["David Vallee Delisle <me@dvd.dev>"]

0 commit comments

Comments
 (0)