Skip to content

Commit

Permalink
Fix #199
Browse files Browse the repository at this point in the history
Fix refactoring gone wrong
  • Loading branch information
DarwinsBuddy committed Dec 16, 2023
1 parent 54e330e commit 32fb66e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
36 changes: 36 additions & 0 deletions custom_components/wnsm/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,42 @@ def verbrauch(
)
return self._call_api(endpoint, query=query)

def verbrauchRaw(
self,
customer_id: str,
zaehlpunkt: str,
date_from: datetime,
date_to: datetime = None,
resolution: const.Resolution = const.Resolution.HOUR
):
"""Returns energy usage.
This can be used to query the daily consumption for a long period of time,
for example several months or a week.
Args:
customer_id (str): Customer ID returned by zaehlpunkt call ("geschaeftspartner")
zaehlpunkt (str, optional): id for desired smartmeter.
If None, check for first meter in user profile.
date_from (datetime): Start date for energy usage request
date_to (datetime, optional): End date for energy usage request.
Defaults to datetime.now()
resolution (const.Resolution, optional): Specify either 1h or 15min resolution
Returns:
dict: JSON response of api call to
'messdaten/CUSTOMER_ID/ZAEHLPUNKT/verbrauchRaw'
"""
if date_to is None:
date_to = datetime.now()
if zaehlpunkt is None or customer_id is None:
customerId, zaehlpunkt = self._get_first_zaehlpunkt()
endpoint = f"messdaten/{customer_id}/{zaehlpunkt}/verbrauchRaw"
query = const.build_verbrauchs_args(
dateFrom=self._dt_string(date_from),
dateTo=self._dt_string(date_to),
granularity="DAY",
dayViewResolution=resolution.value
)
return self._call_api(endpoint, query=query)

def profil(self):
"""Returns profile of a logged-in user.
Expand Down
12 changes: 11 additions & 1 deletion custom_components/wnsm/base_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,17 @@ async def get_zaehlpunkt(self, smartmeter: Smartmeter) -> dict[str, str]:
async def get_consumption(self, smartmeter: Smartmeter, start_date: datetime):
"""Return 24h of hourly consumption starting from a date"""
response = await self.hass.async_add_executor_job(
smartmeter.verbrauch, self._attr_extra_state_attributes.get('customerId'), self.zaehlpunkt, start_date
smartmeter.verbrauchRaw, self._attr_extra_state_attributes.get('customerId'), self.zaehlpunkt, start_date
)
if "Exception" in response:
raise RuntimeError(f"Cannot access daily consumption: {response}")

return translate_dict(response, ATTRS_VERBRAUCH_CALL)

async def get_consumption_raw(self, smartmeter: Smartmeter, start_date: datetime):
"""Return 24h of hourly consumption starting from a date"""
response = await self.hass.async_add_executor_job(
smartmeter.verbrauchRaw, self._attr_extra_state_attributes.get('customerId'), self.zaehlpunkt, start_date
)
if "Exception" in response:
raise RuntimeError(f"Cannot access daily consumption: {response}")
Expand Down
18 changes: 3 additions & 15 deletions custom_components/wnsm/live_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ class LiveSensor(BaseSensor, SensorEntity):
def __init__(self, username: str, password: str, zaehlpunkt: str) -> None:
super().__init__(username, password, zaehlpunkt)

async def get_daily_consumption(self, smartmeter: Smartmeter, date: datetime):
"""
asynchronously get and parse /tages_verbrauch response
Returns response already sanitzied of the specified zahlpunkt in ctor
"""
response = await self.hass.async_add_executor_job(
smartmeter.tages_verbrauch, date, self.zaehlpunkt
)
if "Exception" in response:
raise RuntimeError("Cannot access daily consumption: ", response)
return response

async def async_update(self):
"""
update sensor
Expand Down Expand Up @@ -59,14 +47,14 @@ async def async_update(self):
else:
# if not, we'll have to guesstimate (because api is shitty-pom-fritty)
# for that zaehlpunkt
yesterdays_consumption = await self.get_daily_consumption(
yesterdays_consumption = await self.get_consumption_raw(
smartmeter, before(today())
)
if (
"values" in yesterdays_consumption
and "statistics" in yesterdays_consumption
and "consumptionAverage" in yesterdays_consumption
):
avg = yesterdays_consumption["statistics"]["average"]
avg = yesterdays_consumption["consumptionAverage"]
yesterdays_sum = sum(
(
y["value"] if y["value"] is not None else avg
Expand Down
2 changes: 1 addition & 1 deletion custom_components/wnsm/statistics_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ async def _import_statistics(self, smartmeter: Smartmeter, start: datetime, tota
_LOGGER.debug("Selecting data up to %s" % now)
while start < now:
_LOGGER.debug("Select 24h of Data, using sum=%.3f, start=%s" % (total_usage, start))
consumption = await self.get_consumption(smartmeter, start)
consumption = await self.get_consumption_raw(smartmeter, start)
_LOGGER.debug(consumption)
last_ts = start
start += timedelta(hours=24) # Next batch. Setting this here should avoid endless loops
Expand Down

0 comments on commit 32fb66e

Please sign in to comment.