From 32fb66efb675e5642c1b3330554e610c801a721a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sp=C3=B6rk?= Date: Sat, 16 Dec 2023 15:27:19 +0100 Subject: [PATCH] Fix #199 Fix refactoring gone wrong --- custom_components/wnsm/api/client.py | 36 +++++++++++++++++++++ custom_components/wnsm/base_sensor.py | 12 ++++++- custom_components/wnsm/live_sensor.py | 18 ++--------- custom_components/wnsm/statistics_sensor.py | 2 +- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/custom_components/wnsm/api/client.py b/custom_components/wnsm/api/client.py index 30a00d4..780163b 100644 --- a/custom_components/wnsm/api/client.py +++ b/custom_components/wnsm/api/client.py @@ -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. diff --git a/custom_components/wnsm/base_sensor.py b/custom_components/wnsm/base_sensor.py index 7b2cf38..7c752e8 100644 --- a/custom_components/wnsm/base_sensor.py +++ b/custom_components/wnsm/base_sensor.py @@ -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}") diff --git a/custom_components/wnsm/live_sensor.py b/custom_components/wnsm/live_sensor.py index 003f91d..f06fe41 100644 --- a/custom_components/wnsm/live_sensor.py +++ b/custom_components/wnsm/live_sensor.py @@ -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 @@ -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 diff --git a/custom_components/wnsm/statistics_sensor.py b/custom_components/wnsm/statistics_sensor.py index c1b62d9..8f363de 100644 --- a/custom_components/wnsm/statistics_sensor.py +++ b/custom_components/wnsm/statistics_sensor.py @@ -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