From a03cfab83baa7a8d5c1519a98a5215d8b060676b Mon Sep 17 00:00:00 2001 From: BottlecapDave Date: Sat, 29 Jun 2024 14:32:39 +0100 Subject: [PATCH] fix: Separate stopping demand sensor from updating if total consumption is not available from Home Pro (30 minutes dev time) --- .../current_consumption_home_pro.py | 2 -- .../electricity/current_total_consumption.py | 23 ++++++++-------- .../current_total_consumption_cubic_meters.py | 25 ++++++++--------- .../gas/current_total_consumption_kwh.py | 27 +++++++++---------- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/custom_components/octopus_energy/coordinators/current_consumption_home_pro.py b/custom_components/octopus_energy/coordinators/current_consumption_home_pro.py index 149b4e48..3e033e9c 100644 --- a/custom_components/octopus_energy/coordinators/current_consumption_home_pro.py +++ b/custom_components/octopus_energy/coordinators/current_consumption_home_pro.py @@ -31,8 +31,6 @@ async def async_get_home_pro_consumption( try: data = await client.async_get_consumption(is_electricity) if data is not None: - if len(data) < 1 or data[0]["total_consumption"] is None or data[0]["total_consumption"] <= 0: - raise ApiException('total_consumption invalid') _LOGGER.debug(f'Retrieved current consumption data from home pro for {"electricity" if is_electricity else "gas"}') return CurrentConsumptionCoordinatorResult(current_date, 1, REFRESH_RATE_IN_MINUTES_HOME_PRO_CONSUMPTION, data) except Exception as e: diff --git a/custom_components/octopus_energy/electricity/current_total_consumption.py b/custom_components/octopus_energy/electricity/current_total_consumption.py index 40ce5226..cb6b4d5e 100644 --- a/custom_components/octopus_energy/electricity/current_total_consumption.py +++ b/custom_components/octopus_energy/electricity/current_total_consumption.py @@ -101,17 +101,18 @@ def _handle_coordinator_update(self) -> None: if (consumption_data is not None and len(consumption_data) > 0): _LOGGER.debug(f"Calculated total electricity consumption for '{self._mpan}/{self._serial_number}'...") - self._state = consumption_data[-1]["total_consumption"] - self._last_reset = current - - self._attributes = { - "mpan": self._mpan, - "serial_number": self._serial_number, - "is_export": self._is_export, - "is_smart_meter": self._is_smart_meter, - "last_evaluated": current, - "data_last_retrieved": consumption_result.last_retrieved if consumption_result is not None else None - } + if consumption_data[-1]["total_consumption"] is not None: + self._state = consumption_data[-1]["total_consumption"] + self._last_reset = current + + self._attributes = { + "mpan": self._mpan, + "serial_number": self._serial_number, + "is_export": self._is_export, + "is_smart_meter": self._is_smart_meter, + "last_evaluated": current, + "data_last_retrieved": consumption_result.last_retrieved if consumption_result is not None else None + } self._attributes = dict_to_typed_dict(self._attributes) super()._handle_coordinator_update() diff --git a/custom_components/octopus_energy/gas/current_total_consumption_cubic_meters.py b/custom_components/octopus_energy/gas/current_total_consumption_cubic_meters.py index 79c6e59f..0c472de0 100644 --- a/custom_components/octopus_energy/gas/current_total_consumption_cubic_meters.py +++ b/custom_components/octopus_energy/gas/current_total_consumption_cubic_meters.py @@ -98,18 +98,19 @@ def _handle_coordinator_update(self) -> None: if (consumption_data is not None and len(consumption_data) > 0): _LOGGER.debug(f"Calculated total gas consumption for '{self._mprn}/{self._serial_number}'...") - if "is_kwh" not in consumption_data[-1] or consumption_data[-1]["is_kwh"] == True: - self._state = convert_kwh_to_m3(consumption_data[-1]["total_consumption"], self._calorific_value) if consumption_data[-1]["total_consumption"] is not None else None - else: - self._state = consumption_data[-1]["total_consumption"] - - self._attributes = { - "mprn": self._mprn, - "serial_number": self._serial_number, - "is_smart_meter": self._is_smart_meter, - "last_evaluated": current, - "data_last_retrieved": consumption_result.last_retrieved if consumption_result is not None else None - } + if consumption_data[-1]["total_consumption"] is not None: + if "is_kwh" not in consumption_data[-1] or consumption_data[-1]["is_kwh"] == True: + self._state = convert_kwh_to_m3(consumption_data[-1]["total_consumption"], self._calorific_value) if consumption_data[-1]["total_consumption"] is not None else None + else: + self._state = consumption_data[-1]["total_consumption"] + + self._attributes = { + "mprn": self._mprn, + "serial_number": self._serial_number, + "is_smart_meter": self._is_smart_meter, + "last_evaluated": current, + "data_last_retrieved": consumption_result.last_retrieved if consumption_result is not None else None + } self._attributes = dict_to_typed_dict(self._attributes) super()._handle_coordinator_update() diff --git a/custom_components/octopus_energy/gas/current_total_consumption_kwh.py b/custom_components/octopus_energy/gas/current_total_consumption_kwh.py index 934c299e..6aca14e9 100644 --- a/custom_components/octopus_energy/gas/current_total_consumption_kwh.py +++ b/custom_components/octopus_energy/gas/current_total_consumption_kwh.py @@ -98,20 +98,19 @@ def _handle_coordinator_update(self) -> None: if (consumption_data is not None and len(consumption_data) > 0): _LOGGER.debug(f"Calculated total gas consumption for '{self._mprn}/{self._serial_number}'...") - self._state = consumption_data[-1]["total_consumption"] - - if "is_kwh" not in consumption_data[-1] or consumption_data[-1]["is_kwh"] == True: - self._state = consumption_data[-1]["total_consumption"] - else: - self._state = convert_m3_to_kwh(consumption_data[-1]["total_consumption"], self._calorific_value) if consumption_data[-1]["total_consumption"] is not None else None - - self._attributes = { - "mprn": self._mprn, - "serial_number": self._serial_number, - "is_smart_meter": self._is_smart_meter, - "last_evaluated": current, - "data_last_retrieved": consumption_result.last_retrieved if consumption_result is not None else None - } + if consumption_data[-1]["total_consumption"] is not None: + if "is_kwh" not in consumption_data[-1] or consumption_data[-1]["is_kwh"] == True: + self._state = consumption_data[-1]["total_consumption"] + else: + self._state = convert_m3_to_kwh(consumption_data[-1]["total_consumption"], self._calorific_value) if consumption_data[-1]["total_consumption"] is not None else None + + self._attributes = { + "mprn": self._mprn, + "serial_number": self._serial_number, + "is_smart_meter": self._is_smart_meter, + "last_evaluated": current, + "data_last_retrieved": consumption_result.last_retrieved if consumption_result is not None else None + } self._attributes = dict_to_typed_dict(self._attributes) super()._handle_coordinator_update()