Skip to content

Commit

Permalink
fix: Fixed calculating total gas consumption from Home Pro when repor…
Browse files Browse the repository at this point in the history
…ted in cubic meters (1 hour dev time)
  • Loading branch information
BottlecapDave committed Jun 28, 2024
1 parent 4a110f3 commit 09965b5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ async def async_get_consumption(self, is_electricity: bool) -> list | None:
response_body = await self.__async_read_response__(response, url)
if (response_body is not None and "meter_consump" in response_body and "consum" in response_body["meter_consump"]):
data = response_body["meter_consump"]["consum"]
divisor = int(data["raw"]["divisor"], 16)
return [{
"total_consumption": int(data["consumption"]) / 1000,
"total_consumption": int(data["consumption"]) / divisor if divisor > 0 else None,
"demand": float(data["instdmand"]) if "instdmand" in data else None,
"start": datetime.fromtimestamp(int(response_body["meter_consump"]["time"]), timezone.utc),
"end": datetime.fromtimestamp(int(response_body["meter_consump"]["time"]), timezone.utc)
"end": datetime.fromtimestamp(int(response_body["meter_consump"]["time"]), timezone.utc),
"is_kwh": data["unit"] == 0
}]

return None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ 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 = convert_kwh_to_m3(consumption_data[-1]["total_consumption"], self._calorific_value) if consumption_data[-1]["total_consumption"] is not None else 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@
from ..coordinators.current_consumption import CurrentConsumptionCoordinatorResult
from .base import (OctopusEnergyGasSensor)
from ..utils.attributes import dict_to_typed_dict
from . import convert_m3_to_kwh

_LOGGER = logging.getLogger(__name__)

class OctopusEnergyCurrentTotalGasConsumptionKwh(CoordinatorEntity, OctopusEnergyGasSensor, RestoreSensor):
"""Sensor for displaying the current total gas consumption in kwh."""

def __init__(self, hass: HomeAssistant, coordinator, meter, point):
def __init__(self, hass: HomeAssistant, coordinator, meter, point, calorific_value: float):
"""Init sensor."""
CoordinatorEntity.__init__(self, coordinator)

self._state = None
self._last_reset = None
self._calorific_value = calorific_value

OctopusEnergyGasSensor.__init__(self, hass, meter, point)

Expand Down Expand Up @@ -98,6 +100,11 @@ def _handle_coordinator_update(self) -> None:

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,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/octopus_energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ async def async_setup_default_sensors(hass: HomeAssistant, config, async_add_ent

if home_pro_client is not None:
home_pro_consumption_coordinator = await async_create_home_pro_current_consumption_coordinator(hass, account_id, home_pro_client, False)
entities.append(OctopusEnergyCurrentTotalGasConsumptionKwh(hass, home_pro_consumption_coordinator, meter, point))
entities.append(OctopusEnergyCurrentTotalGasConsumptionKwh(hass, home_pro_consumption_coordinator, meter, point, calorific_value))
entities.append(OctopusEnergyCurrentTotalGasConsumptionCubicMeters(hass, home_pro_consumption_coordinator, meter, point, calorific_value))

if CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION in config and config[CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION] == True:
Expand All @@ -425,7 +425,7 @@ async def async_setup_default_sensors(hass: HomeAssistant, config, async_add_ent
entities.append(OctopusEnergyCurrentAccumulativeGasCost(hass, consumption_coordinator, gas_rate_coordinator, gas_standing_charges_coordinator, meter, point, calorific_value))

if home_pro_client is None:
entities.append(OctopusEnergyCurrentTotalGasConsumptionKwh(hass, consumption_coordinator, meter, point))
entities.append(OctopusEnergyCurrentTotalGasConsumptionKwh(hass, consumption_coordinator, meter, point, calorific_value))
entities.append(OctopusEnergyCurrentTotalGasConsumptionCubicMeters(hass, consumption_coordinator, meter, point, calorific_value))

entity_ids_to_migrate.append({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ async def test_when_get_consumption_is_called_then_data_is_returned(is_electrici
assert len(data) == 1

assert "demand" in data[0]
assert data[0]["demand"] >= 0
if is_electricity:
assert data[0]["demand"] >= 0
else:
assert data[0]["demand"] is None

assert "total_consumption" in data[0]
assert data[0]["total_consumption"] >= 0
assert data[0]["total_consumption"] >= 0

assert "start" in data[0]
assert "end" in data[0]

assert "is_kwh" in data[0]
assert data[0]["is_kwh"] == True

0 comments on commit 09965b5

Please sign in to comment.