diff --git a/custom_components/energytariff/sensor.py b/custom_components/energytariff/sensor.py index aaf3a27..02f1618 100644 --- a/custom_components/energytariff/sensor.py +++ b/custom_components/energytariff/sensor.py @@ -1,6 +1,6 @@ """Sensor platform for grid-cap-watcher.""" from logging import getLogger -from datetime import datetime, timedelta +from datetime import datetime from typing import Any import voluptuous as vol from homeassistant.util import dt @@ -105,7 +105,6 @@ class GridCapWatcherEnergySensor(RestoreSensor): _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR def __init__(self, hass, config, rx_coord: GridCapacityCoordinator): - self._hass = hass self._effect_sensor_id = config.get(CONF_EFFECT_ENTITY) self._precision = get_rounding_precision(config) @@ -142,6 +141,7 @@ def hourly_reset(self, time): _LOGGER.debug("Hourly reset") self._state = 0 self.async_schedule_update_ha_state(True) + self.fire_event(0, time) async_track_point_in_time( self._hass, self.hourly_reset, start_of_next_hour(time) ) @@ -210,7 +210,6 @@ class GridCapWatcherEstimatedEnergySensor(SensorEntity): _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR def __init__(self, hass, config, rx_coord: GridCapacityCoordinator): - self._hass = hass self._effect_sensor_id = config.get(CONF_EFFECT_ENTITY) self._coordinator = rx_coord @@ -281,7 +280,6 @@ class GridCapWatcherCurrentEffectLevelThreshold(RestoreSensor, RestoreEntity): _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR def __init__(self, hass, config, rx_coord: GridCapacityCoordinator): - self._hass = hass self._effect_sensor_id = config.get(CONF_EFFECT_ENTITY) self._coordinator = rx_coord @@ -358,7 +356,6 @@ def calculate_level(self) -> bool: found_threshold = self.get_level(average_value) if found_threshold is not None: - self._state = found_threshold["threshold"] self.schedule_update_ha_state(True) @@ -422,7 +419,6 @@ class GridCapWatcherAverageThreePeakHours(RestoreSensor, RestoreEntity): _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR def __init__(self, hass, config, rx_coord: GridCapacityCoordinator): - self._hass = hass self._effect_sensor_id = config.get(CONF_EFFECT_ENTITY) self._coordinator = rx_coord @@ -537,7 +533,6 @@ class GridCapWatcherAvailableEffectRemainingHour(RestoreSensor, RestoreEntity): _attr_native_unit_of_measurement = UnitOfPower.WATT def __init__(self, hass, config, rx_coord: GridCapacityCoordinator): - self._hass = hass self._effect_sensor_id = config.get(CONF_EFFECT_ENTITY) self._effect = None @@ -569,12 +564,14 @@ async def async_added_to_hass(self) -> None: self.attr["grid_threshold_level"] = savedstate.attributes[ "grid_threshold_level" ] + self.__calculate() def _threshold_state_change(self, state: GridThresholdData): if state is None: return self.attr["grid_threshold_level"] = state.level self.__calculate() + self.schedule_update_ha_state(True) def _effect_state_change(self, state: EnergyData): if state is None: @@ -582,6 +579,7 @@ def _effect_state_change(self, state: EnergyData): self._energy = state.energy_consumed self._effect = state.current_effect self.__calculate() + self.schedule_update_ha_state(True) def __calculate(self): if ( @@ -625,7 +623,6 @@ def __calculate(self): power = float(self._max_effect) * -1 self._state = power - self.schedule_update_ha_state(True) return True