diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index d8fd5bb..857ad89 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -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): @@ -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: @@ -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: @@ -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): @@ -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.""" @@ -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):