Skip to content

Commit

Permalink
Fire hourly event to all sensors
Browse files Browse the repository at this point in the history
All sensors are dependent upon the energyused-sensor, so we need to transmit a
RX event when hourly reset occurs so that they update their respective values
  • Loading branch information
epaulsen committed Aug 6, 2023
1 parent a580851 commit 377a176
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions custom_components/energytariff/sensor.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -569,19 +564,22 @@ 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:
return
self._energy = state.energy_consumed
self._effect = state.current_effect
self.__calculate()
self.schedule_update_ha_state(True)

def __calculate(self):
if (
Expand Down Expand Up @@ -625,7 +623,6 @@ def __calculate(self):
power = float(self._max_effect) * -1

self._state = power
self.schedule_update_ha_state(True)

return True

Expand Down

0 comments on commit 377a176

Please sign in to comment.