Skip to content

Commit

Permalink
Update API for personalized results (#103)
Browse files Browse the repository at this point in the history
* Update API for personalized results

* Allow non-signed in users to init

* Use entry within coordinator to get site_reference
  • Loading branch information
DCSBL authored Feb 29, 2024
1 parent cfb1ebc commit 7570e55
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
24 changes: 24 additions & 0 deletions custom_components/frank_energie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if entry.unique_id is None or entry.unique_id == "frank_energie_component":
hass.config_entries.async_update_entry(entry, unique_id=str("frank_energie"))

# Select site-reference, or find first one that has status 'IN_DELIVERY' if not set
if entry.data.get("site_reference") is None and entry.data.get(CONF_ACCESS_TOKEN) is not None:
api = FrankEnergie(
clientsession=async_get_clientsession(hass),
auth_token=entry.data.get(CONF_ACCESS_TOKEN, None),
refresh_token=entry.data.get(CONF_TOKEN, None),
)
me = await api.me()

# filter out all sites that are not in delivery
me.deliverySites = [site for site in me.deliverySites if site.status == "IN_DELIVERY"]

if len(me.deliverySites) == 0:
raise Exception("No suitable sites found for this account")

site = me.deliverySites[0]
hass.config_entries.async_update_entry(entry, data={**entry.data, "site_reference": site.reference})

# Update title
title = f"{site.address_street} {site.address_houseNumber}"
if site.address_houseNumberAddition is not None:
title += f" {site.address_houseNumberAddition}"
hass.config_entries.async_update_entry(entry, title=title)

# Initialise the coordinator and save it as domain-data
api = FrankEnergie(
clientsession=async_get_clientsession(hass),
Expand Down
9 changes: 5 additions & 4 deletions custom_components/frank_energie/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ class FrankEnergieCoordinator(DataUpdateCoordinator):
api: FrankEnergie

def __init__(
self, hass: HomeAssistant, entry: ConfigEntry, api: FrankEnergie
self, hass: HomeAssistant, entry: ConfigEntry, api: FrankEnergie,
) -> None:
"""Initialize the data object."""
self.hass = hass
self.entry = entry
self.api = api
self.site_reference = entry.data.get("site_reference", None)

super().__init__(
hass,
Expand All @@ -63,10 +64,10 @@ async def _async_update_data(self) -> FrankEnergieData:
prices_tomorrow = await self.__fetch_prices_with_fallback(tomorrow, day_after_tomorrow)

data_month_summary = (
await self.api.month_summary() if self.api.is_authenticated else None
await self.api.month_summary(self.site_reference) if self.api.is_authenticated else None
)
data_invoices = (
await self.api.invoices() if self.api.is_authenticated else None
await self.api.invoices(self.site_reference) if self.api.is_authenticated else None
)
except UpdateFailed as err:
# Check if we still have data to work with, if so, return this data. Still log the error as warning
Expand Down Expand Up @@ -101,7 +102,7 @@ async def __fetch_prices_with_fallback(self, start_date: date, end_date: date) -
if not self.api.is_authenticated:
return await self.api.prices(start_date, end_date)
else:
user_prices = await self.api.user_prices(start_date)
user_prices = await self.api.user_prices(start_date, self.site_reference)

if len(user_prices.gas.all) > 0 and len(user_prices.electricity.all) > 0:
# If user_prices are available for both gas and electricity return them
Expand Down
2 changes: 1 addition & 1 deletion custom_components/frank_energie/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"issue_tracker": "https://github.com/bajansen/home-assistant-frank_energie/issues",
"iot_class": "cloud_polling",
"requirements": [
"python-frank-energie==5.0.1"
"python-frank-energie==6.0.0"
],
"version": "0.0.0"
}

0 comments on commit 7570e55

Please sign in to comment.