diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index 4ac22f6..b236ed0 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -55,7 +55,8 @@ async def async_setup_entry( "Non-200 Status Code. The Glow API may be experiencing issues" ) else: - _LOGGER.exception("Unexpected exception: %s. Please open an issue", ex) + _LOGGER.exception( + "Unexpected exception: %s. Please open an issue", ex) for virtual_entity in virtual_entities: # Gather all resources for each virtual entity @@ -78,7 +79,8 @@ async def async_setup_entry( "Non-200 Status Code. The Glow API may be experiencing issues" ) else: - _LOGGER.exception("Unexpected exception: %s. Please open an issue", ex) + _LOGGER.exception( + "Unexpected exception: %s. Please open an issue", ex) # Loop through all resources and create sensors for resource in resources: @@ -90,7 +92,8 @@ async def async_setup_entry( # Standing and Rate sensors are handled by the coordinator coordinator = TariffCoordinator(hass, resource) - standing_sensor = Standing(coordinator, resource, virtual_entity) + standing_sensor = Standing( + coordinator, resource, virtual_entity) entities.append(standing_sensor) rate_sensor = Rate(coordinator, resource, virtual_entity) entities.append(rate_sensor) @@ -118,7 +121,8 @@ def supply_type(resource) -> str: return "electricity" if "gas.consumption" in resource.classifier: return "gas" - _LOGGER.error("Unknown classifier: %s. Please open an issue", resource.classifier) + _LOGGER.error("Unknown classifier: %s. Please open an issue", + resource.classifier) return "unknown" @@ -134,14 +138,14 @@ def device_name(resource, virtual_entity) -> str: async def should_update() -> bool: - """Check if time is between 0-5 or 30-35 minutes past the hour.""" + """Check if time is between 1-5 or 31-35 minutes past the hour.""" minutes = datetime.now().minute - if (0 <= minutes <= 5) or (30 <= minutes <= 35): + if (1 <= minutes <= 5) or (31 <= minutes <= 35): return True return False -async def daily_data(hass: HomeAssistant, resource) -> float: +async def daily_data(hass: HomeAssistant, resource) -> (float, str): """Get daily usage from the API.""" # 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 @@ -173,7 +177,8 @@ async def daily_data(hass: HomeAssistant, resource) -> float: "Non-200 Status Code. The Glow API may be experiencing issues" ) else: - _LOGGER.exception("Unexpected exception: %s. Please open an issue", ex) + _LOGGER.exception( + "Unexpected exception: %s. Please open an issue", ex) try: _LOGGER.debug( @@ -182,14 +187,16 @@ async def daily_data(hass: HomeAssistant, resource) -> float: readings = await hass.async_add_executor_job( resource.get_readings, t_from, t_to, "P1D", "sum", True ) - _LOGGER.debug("Successfully got daily usage for resource id %s", resource.id) _LOGGER.debug( - "Readings for %s has %s entries", resource.classifier, len(readings) + "Successfully got daily usage for resource id %s", resource.id) + _LOGGER.debug( + "Readings for %s has %s entries", resource.classifier, len( + readings) ) v = readings[0][1].value if len(readings) > 1: v += readings[1][1].value - return v + return (v, t_from) except requests.Timeout as ex: _LOGGER.error("Timeout: %s", ex) except requests.exceptions.ConnectionError as ex: @@ -201,7 +208,8 @@ async def daily_data(hass: HomeAssistant, resource) -> float: "Non-200 Status Code. The Glow API may be experiencing issues" ) else: - _LOGGER.exception("Unexpected exception: %s. Please open an issue", ex) + _LOGGER.exception( + "Unexpected exception: %s. Please open an issue", ex) return None @@ -232,7 +240,8 @@ async def tariff_data(hass: HomeAssistant, resource) -> float: "Non-200 Status Code. The Glow API may be experiencing issues" ) else: - _LOGGER.exception("Unexpected exception: %s. Please open an issue", ex) + _LOGGER.exception( + "Unexpected exception: %s. Please open an issue", ex) return None @@ -275,14 +284,14 @@ async def async_update(self) -> None: """Fetch new data for the sensor.""" # Get data on initial startup if not self.initialised: - value = await daily_data(self.hass, self.resource) + (value, t_from) = await daily_data(self.hass, self.resource) if value: self._attr_native_value = round(value, 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) + (value, t_from) = await daily_data(self.hass, self.resource) if value: self._attr_native_value = round(value, 2) @@ -294,7 +303,8 @@ 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 + _attr_last_reset = None def __init__(self, hass: HomeAssistant, resource, virtual_entity) -> None: """Initialize the sensor.""" @@ -320,16 +330,18 @@ def device_info(self) -> DeviceInfo: async def async_update(self) -> None: """Fetch new data for the sensor.""" if not self.initialised: - value = await daily_data(self.hass, self.resource) + (value, t_from) = await daily_data(self.hass, self.resource) if value: self._attr_native_value = round(value / 100, 2) + self._attr_last_reset = t_from 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) + (value, t_from) = await daily_data(self.hass, self.resource) if value: self._attr_native_value = round(value / 100, 2) + self._attr_last_reset = t_from class TariffCoordinator(DataUpdateCoordinator):