Skip to content

Commit

Permalink
Fix response unpacking of zaehlpunkt call
Browse files Browse the repository at this point in the history
Fixes #215
  • Loading branch information
DarwinsBuddy committed Jan 26, 2024
1 parent d23eb25 commit e1a45ea
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions custom_components/wnsm/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,16 @@ def _call_api(

return response.json()

def _get_first_zaehlpunkt(self) -> (str, str):
def get_zaehlpunkt(self, zaehlpunkt: str = None) -> (str, str):
zps = self.zaehlpunkte()[0]
customer_id = zps["geschaeftspartner"]
zp = zps["zaehlpunkte"][0]["zaehlpunktnummer"]
if zaehlpunkt is not None:
zp = [z for z in zps["zaehlpunkte"] if z["zaehlpunktnummer"] == zaehlpunkt]
zp = zp[0]["zaehlpunktnummer"] if len(zp) > 0 else None
else:
zp = zps["zaehlpunkte"][0]["zaehlpunktnummer"]
return customer_id, zp

def get_zaehlpunkt(self, zaehlpunkt: str) -> (str, str):
zps = self.zaehlpunkte()[0]
customer_id = zps["geschaeftspartner"]
zp = [z for z in zps["zaehlpunkte"] if z["zaehlpunktnummer"] == zaehlpunkt]
return customer_id, zp[0] if len(zp) > 0 else None

def zaehlpunkte(self):
"""Returns zaehlpunkte for currently logged in user."""
return self._call_api("zaehlpunkte")
Expand Down Expand Up @@ -289,7 +287,7 @@ def verbrauch(
'messdaten/CUSTOMER_ID/ZAEHLPUNKT/verbrauchRaw'
"""
if zaehlpunkt is None or customer_id is None:
customer_id, zaehlpunkt = self._get_first_zaehlpunkt()
customer_id, zaehlpunkt = self.get_zaehlpunkt()
endpoint = f"messdaten/{customer_id}/{zaehlpunkt}/verbrauch"
query = const.build_verbrauchs_args(
# This one does not have a dateTo...
Expand Down Expand Up @@ -326,7 +324,7 @@ def verbrauchRaw(
if date_to is None:
date_to = datetime.now()
if zaehlpunkt is None or customer_id is None:
customer_id, zaehlpunkt = self._get_first_zaehlpunkt()
customer_id, zaehlpunkt = self.get_zaehlpunkt()
endpoint = f"messdaten/{customer_id}/{zaehlpunkt}/verbrauchRaw"
query = dict(
# These are the only three fields that are used for that endpoint:
Expand Down Expand Up @@ -360,7 +358,7 @@ def ereignisse(
if date_to is None:
date_to = datetime.now()
if zaehlpunkt is None:
customer_id, zaehlpunkt = self._get_first_zaehlpunkt()
customer_id, zaehlpunkt = self.get_zaehlpunkt()
query = {
"zaehlpunkt": zaehlpunkt,
"dateFrom": self._dt_string(date_from),
Expand Down Expand Up @@ -413,7 +411,7 @@ def historical_data(
If date_from is not given but date_until, again a three year span is assumed.
"""
if zaehlpunktnummer is None:
customer_id, zaehlpunkt = self._get_first_zaehlpunkt()
customer_id, zaehlpunkt = self.get_zaehlpunkt()
else:
customer_id, zaehlpunkt = self.get_zaehlpunkt(zaehlpunktnummer)

Expand Down Expand Up @@ -472,10 +470,7 @@ def bewegungsdaten(
else:
rolle = "V002"

if zaehlpunktnummer is None:
customer_id, zaehlpunkt = self._get_first_zaehlpunkt()
else:
customer_id, zaehlpunkt = self.get_zaehlpunkt(zaehlpunktnummer)
customer_id, zaehlpunkt = self.get_zaehlpunkt(zaehlpunktnummer)

if date_until is None:
date_until = date.today()
Expand Down

0 comments on commit e1a45ea

Please sign in to comment.