Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pyanglianwater/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ async def send_request(self, endpoint: str, body: dict, **kwargs):
"""Send a request to the API using the authentication handler."""
return await self._auth.send_request(endpoint=endpoint, body=body, **kwargs)

async def get_associated_accounts(self):
"""Get associated accounts."""
return await self.send_request(
endpoint="get_associated_accounts",
body=None
)

async def token_refresh(self):
"""Force token refresh."""
return await self._auth.send_refresh_request()
Expand Down
11 changes: 10 additions & 1 deletion pyanglianwater/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ def account_number(self) -> str:
return self._account_number
return self._decoded_access_token.get("extension_accountNumber", "")

@property
def business_partner_number(self) -> str:
"""Return business partner number."""
return self._decoded_access_token.get("extension_business_partner_number", "")

@property
def access_token(self) -> str:
"""Return the access token."""
Expand Down Expand Up @@ -353,7 +358,11 @@ async def send_request(self, endpoint: str, body: dict, **kwargs) -> dict:
_LOGGER.debug("Access token unavailable, not logged in.")
raise ExpiredAccessTokenError()
headers = self.get_authenticated_headers
built_url = AW_APP_BASEURL + endpoint_map["endpoint"].format(ACCOUNT_ID=self.encrypted_account_number, **kwargs)
built_url = AW_APP_BASEURL + endpoint_map["endpoint"].format(
ACCOUNT_ID=self.encrypted_account_number,
BUSINESS_PARTNER_ID=encrypt_string_to_charcode_hex(self.business_partner_number),
**kwargs
)
async with aiohttp.ClientSession() as _session:
async with _session.request(
method=endpoint_map["method"],
Expand Down
4 changes: 4 additions & 0 deletions pyanglianwater/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"smart?frequency={GRANULARITY}&start={START}&end={END}"
)
},
"get_associated_accounts": {
"method": "GET",
"endpoint": "/myaccount/v1/businesspartners/{BUSINESS_PARTNER_ID}/associatedaccounts"
}
}
AUTH_AW_BASE = "https://login.myaccount.anglianwater.co.uk"
AUTH_MSO_BASE = f"{AUTH_AW_BASE}/CustomerOnlineJourney.onmicrosoft.com/B2C_1A_SIGNUPORSIGNIN"
Expand Down
10 changes: 8 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import logging
import asyncio

import aiohttp

from dotenv import load_dotenv
from pyanglianwater import AnglianWater
from pyanglianwater.auth import MSOB2CAuth
Expand All @@ -21,6 +23,9 @@ async def main():
username=os.environ.get("AW_USERNAME") or input("Email: "),
password=os.environ.get("AW_PASSWORD") or input("Password: "),
refresh_token=os.environ.get("AW_REFRESH_TOKEN", None),
session=aiohttp.ClientSession(
cookie_jar=aiohttp.CookieJar(quote_cookie=False)
)
)
await authenticator.send_login_request()
_LOGGER.debug("Logged in. Ready..")
Expand All @@ -30,14 +35,15 @@ async def main():
except Exception as exc:
_LOGGER.error(exc)

water = await AnglianWater.create_from_authenticator(authenticator, area="Anglian")
water = AnglianWater(authenticator)
accounts = await water.api.get_associated_accounts()
await water.update()

while True:
for m in water.meters.values():
_LOGGER.debug(">> Meter %s", m.serial_number)
_LOGGER.debug(">> Last reading %s", m.last_reading)
_LOGGER.debug(">> Yesterday cost %s", m.get_yesterday_cost)
_LOGGER.debug(">> Yesterday cost %s", m.yesterday_water_cost)
_LOGGER.debug(">> Latest consumption %s", m.latest_consumption)
await asyncio.sleep(30)

Expand Down
Loading