Skip to content

Commit

Permalink
Merge pull request #2 from mcc05/main
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister authored Dec 18, 2024
2 parents 5bba4b0 + d4b3955 commit 4c2b193
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions custom_components/hildebrandglow_dcc/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ async def should_update() -> bool:

async def daily_data(hass: HomeAssistant, resource) -> float:
"""Get daily usage from the API."""
v = 0.0
# If it's before 01:06, we need to fetch yesterday's data
# Should only need to be before 00:36 but gas data can be 30 minutes behind electricity data
if datetime.now().time() <= time(1, 5):
Expand Down Expand Up @@ -184,12 +185,30 @@ async def daily_data(hass: HomeAssistant, resource) -> float:
)
_LOGGER.debug("Successfully got daily usage for resource id %s", resource.id)
_LOGGER.debug(
"Readings for %s has %s entries", resource.classifier, len(readings)
"Readings for %s has %s entries ", resource.classifier, len(readings),
)


v = readings[0][1].value
_LOGGER.debug("1st reading %f:",v)

if (not isinstance(v,float)):
v= 0.0
return v

if (resource.classifier == "electricity.consumption.cost" or resource.classifier == "gas.consumption.cost"):
if len(readings) > 1:
_LOGGER.debug("2nd cost reading %f:",readings[1][1].value)
v = readings[1][1].value
_LOGGER.debug("reading cost %f:",v)
return v

if len(readings) > 1:
v += readings[1][1].value
_LOGGER.debug("2nd reading %f:",readings[1][1].value)
v += readings[1][1].value
_LOGGER.debug("reading total %f:",v)
return v

except requests.Timeout as ex:
_LOGGER.error("Timeout: %s", ex)
except requests.exceptions.ConnectionError as ex:
Expand All @@ -202,7 +221,7 @@ async def daily_data(hass: HomeAssistant, resource) -> float:
)
else:
_LOGGER.exception("Unexpected exception: %s. Please open an issue", ex)
return None
return v


async def tariff_data(hass: HomeAssistant, resource) -> float:
Expand Down Expand Up @@ -276,15 +295,13 @@ async def async_update(self) -> None:
# Get data on initial startup
if not self.initialised:
value = await daily_data(self.hass, self.resource)
if value:
self._attr_native_value = round(value, 2)
self.initialised = True
self._attr_native_value = round(value, 3)
self.initialised = True
else:
# Only update the sensor if it's between 0-5 or 30-35 minutes past the hour
if await should_update():
value = await daily_data(self.hass, self.resource)
if value:
self._attr_native_value = round(value, 2)
self._attr_native_value = round(value, 3)


class Cost(SensorEntity):
Expand All @@ -294,7 +311,7 @@ class Cost(SensorEntity):
_attr_has_entity_name = True
_attr_name = "Cost (today)"
_attr_native_unit_of_measurement = "GBP"
_attr_state_class = SensorStateClass.TOTAL_INCREASING
_attr_state_class = SensorStateClass.TOTAL

def __init__(self, hass: HomeAssistant, resource, virtual_entity) -> None:
"""Initialize the sensor."""
Expand All @@ -321,15 +338,13 @@ async def async_update(self) -> None:
"""Fetch new data for the sensor."""
if not self.initialised:
value = await daily_data(self.hass, self.resource)
if value:
self._attr_native_value = round(value / 100, 2)
self.initialised = True
self._attr_native_value = round(value / 100, 2)
self.initialised = True
else:
# Only update the sensor if it's between 0-5 or 30-35 minutes past the hour
if await should_update():
value = await daily_data(self.hass, self.resource)
if value:
self._attr_native_value = round(value / 100, 2)
self._attr_native_value = round(value / 100, 2)


class TariffCoordinator(DataUpdateCoordinator):
Expand Down

0 comments on commit 4c2b193

Please sign in to comment.