Compare commits
5 Commits
4.2.0.alph
...
4.2.0.alph
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8719349241 | ||
|
|
5540f6e8a9 | ||
|
|
18a72bd907 | ||
|
|
6a44d0dc4a | ||
|
|
9e15aa48b9 |
@@ -464,8 +464,8 @@ class BaseThermostat(ClimateEntity, RestoreEntity):
|
||||
self._cycle_min * 60,
|
||||
# Needed for time calculation
|
||||
get_tz(self._hass),
|
||||
# one digit after the coma for temperature
|
||||
1,
|
||||
# two digits after the coma for temperature slope calculation
|
||||
2,
|
||||
)
|
||||
|
||||
_LOGGER.debug(
|
||||
@@ -880,6 +880,11 @@ class BaseThermostat(ClimateEntity, RestoreEntity):
|
||||
"""Return the unit of measurement."""
|
||||
return self._unit
|
||||
|
||||
@property
|
||||
def ema_temperature(self) -> str:
|
||||
"""Return the EMA temperature."""
|
||||
return self._ema_temp
|
||||
|
||||
@property
|
||||
def hvac_mode(self) -> HVACMode | None:
|
||||
"""Return current operation."""
|
||||
@@ -1671,7 +1676,7 @@ class BaseThermostat(ClimateEntity, RestoreEntity):
|
||||
for under in self._underlyings:
|
||||
await under.turn_off()
|
||||
|
||||
async def _async_manage_window_auto(self):
|
||||
async def _async_manage_window_auto(self, in_cycle=False):
|
||||
"""The management of the window auto feature"""
|
||||
|
||||
async def dearm_window_auto(_):
|
||||
@@ -1701,9 +1706,17 @@ class BaseThermostat(ClimateEntity, RestoreEntity):
|
||||
if not self._window_auto_algo:
|
||||
return
|
||||
|
||||
slope = self._window_auto_algo.add_temp_measurement(
|
||||
temperature=self._ema_temp, datetime_measure=self._last_temperature_mesure
|
||||
)
|
||||
if in_cycle:
|
||||
slope = self._window_auto_algo.check_age_last_measurement(
|
||||
temperature=self._ema_temp,
|
||||
datetime_now=datetime.now(get_tz(self._hass)),
|
||||
)
|
||||
else:
|
||||
slope = self._window_auto_algo.add_temp_measurement(
|
||||
temperature=self._ema_temp,
|
||||
datetime_measure=self._last_temperature_mesure,
|
||||
)
|
||||
|
||||
_LOGGER.debug(
|
||||
"%s - Window auto is on, check the alert. last slope is %.3f",
|
||||
self,
|
||||
@@ -2052,6 +2065,9 @@ class BaseThermostat(ClimateEntity, RestoreEntity):
|
||||
self._attr_preset_mode,
|
||||
)
|
||||
|
||||
# check auto_window conditions
|
||||
await self._async_manage_window_auto(in_cycle=True)
|
||||
|
||||
# Issue 56 in over_climate mode, if the underlying climate is not initialized, try to initialize it
|
||||
for under in self._underlyings:
|
||||
if not under.is_initialized:
|
||||
|
||||
@@ -14,7 +14,7 @@ MIN_TIME_DECAY_SEC = 0
|
||||
# For example when using a half life of 10 minutes a measurement that is 60 minutes ago
|
||||
# (if there's nothing inbetween) would contribute to the smoothed value with 1,5%,
|
||||
# giving the current measurement 98,5% relevance. It could be wise to limit the alpha to e.g. 4x the half life (=0.9375).
|
||||
MAX_ALPHA = 0.9375
|
||||
MAX_ALPHA = 0.5
|
||||
|
||||
|
||||
class ExponentialMovingAverage:
|
||||
@@ -73,11 +73,13 @@ class ExponentialMovingAverage:
|
||||
self._last_timestamp = timestamp
|
||||
self._current_ema = new_ema
|
||||
_LOGGER.debug(
|
||||
"%s - alpha=%.2f new_ema=%.2f last_timestamp=%s",
|
||||
"%s - timestamp=%s alpha=%.2f measurement=%.2f current_ema=%.2f new_ema=%.2f",
|
||||
self,
|
||||
timestamp,
|
||||
alpha,
|
||||
measurement,
|
||||
self._current_ema,
|
||||
self._last_timestamp,
|
||||
new_ema,
|
||||
)
|
||||
|
||||
return round(self._current_ema, self._precision)
|
||||
|
||||
@@ -13,9 +13,13 @@ from datetime import datetime
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# To filter bad values
|
||||
MIN_DELTA_T_SEC = 15 # two temp mesure should be > 10 sec
|
||||
MIN_DELTA_T_SEC = 0 # two temp mesure should be > 0 sec
|
||||
MAX_SLOPE_VALUE = 2 # slope cannot be > 2 or < -2 -> else this is an aberrant point
|
||||
|
||||
MAX_DURATION_MIN = 30 # a fake data point is added in the cycle if last measurement was older than 30 min
|
||||
|
||||
MIN_NB_POINT = 4 # do not calculate slope until we have enough point
|
||||
|
||||
|
||||
class WindowOpenDetectionAlgorithm:
|
||||
"""The class that implements the algorithm listed above"""
|
||||
@@ -25,6 +29,7 @@ class WindowOpenDetectionAlgorithm:
|
||||
_last_slope: float
|
||||
_last_datetime: datetime
|
||||
_last_temperature: float
|
||||
_nb_point: int
|
||||
|
||||
def __init__(self, alert_threshold, end_alert_threshold) -> None:
|
||||
"""Initalize a new algorithm with the both threshold"""
|
||||
@@ -32,9 +37,24 @@ class WindowOpenDetectionAlgorithm:
|
||||
self._end_alert_threshold = end_alert_threshold
|
||||
self._last_slope = None
|
||||
self._last_datetime = None
|
||||
self._nb_point = 0
|
||||
|
||||
def check_age_last_measurement(self, temperature, datetime_now) -> float:
|
||||
""" " Check if last measurement is old and add
|
||||
a fake measurement point if this is the case
|
||||
"""
|
||||
if self._last_datetime is None:
|
||||
return self.add_temp_measurement(temperature, datetime_now)
|
||||
|
||||
delta_t_sec = float((datetime_now - self._last_datetime).total_seconds()) / 60.0
|
||||
if delta_t_sec >= MAX_DURATION_MIN:
|
||||
return self.add_temp_measurement(temperature, datetime_now, False)
|
||||
else:
|
||||
# do nothing
|
||||
return self._last_slope
|
||||
|
||||
def add_temp_measurement(
|
||||
self, temperature: float, datetime_measure: datetime
|
||||
self, temperature: float, datetime_measure: datetime, store_date: bool = True
|
||||
) -> float:
|
||||
"""Add a new temperature measurement
|
||||
returns the last slope
|
||||
@@ -43,6 +63,7 @@ class WindowOpenDetectionAlgorithm:
|
||||
_LOGGER.debug("First initialisation")
|
||||
self._last_datetime = datetime_measure
|
||||
self._last_temperature = temperature
|
||||
self._nb_point = self._nb_point + 1
|
||||
return None
|
||||
|
||||
_LOGGER.debug(
|
||||
@@ -72,22 +93,29 @@ class WindowOpenDetectionAlgorithm:
|
||||
)
|
||||
return lspe
|
||||
|
||||
# if self._last_slope is None:
|
||||
self._last_slope = round(new_slope, 4)
|
||||
# else:
|
||||
# self._last_slope = (0.5 * self._last_slope) + (0.5 * new_slope)
|
||||
if self._last_slope is None:
|
||||
self._last_slope = round(new_slope, 4)
|
||||
else:
|
||||
self._last_slope = round((0.2 * self._last_slope) + (0.8 * new_slope), 4)
|
||||
|
||||
# if we are in cycle check and so adding a fake datapoint, we don't store the event datetime
|
||||
# so that, when we will receive a real temperature point we will not calculate a wrong slope
|
||||
if store_date:
|
||||
self._last_datetime = datetime_measure
|
||||
|
||||
self._last_datetime = datetime_measure
|
||||
self._last_temperature = temperature
|
||||
|
||||
self._nb_point = self._nb_point + 1
|
||||
_LOGGER.debug(
|
||||
"delta_t=%.3f delta_temp=%.3f new_slope=%.3f last_slope=%s slope=%.3f",
|
||||
"delta_t=%.3f delta_temp=%.3f new_slope=%.3f last_slope=%s slope=%.3f nb_point=%s",
|
||||
delta_t,
|
||||
delta_temp,
|
||||
new_slope,
|
||||
lspe,
|
||||
self._last_slope,
|
||||
self._nb_point,
|
||||
)
|
||||
|
||||
return self._last_slope
|
||||
|
||||
def is_window_open_detected(self) -> bool:
|
||||
@@ -95,22 +123,20 @@ class WindowOpenDetectionAlgorithm:
|
||||
if self._alert_threshold is None:
|
||||
return False
|
||||
|
||||
return (
|
||||
self._last_slope < -self._alert_threshold
|
||||
if self._last_slope is not None
|
||||
else False
|
||||
)
|
||||
if self._nb_point < MIN_NB_POINT or self._last_slope is None:
|
||||
return False
|
||||
|
||||
return self._last_slope < -self._alert_threshold
|
||||
|
||||
def is_window_close_detected(self) -> bool:
|
||||
"""True if the last calculated slope is above (cause negative) the _end_alert_threshold"""
|
||||
if self._end_alert_threshold is None:
|
||||
return False
|
||||
|
||||
return (
|
||||
self._last_slope >= self._end_alert_threshold
|
||||
if self._last_slope is not None
|
||||
else False
|
||||
)
|
||||
if self._nb_point < MIN_NB_POINT or self._last_slope is None:
|
||||
return False
|
||||
|
||||
return self._last_slope >= self._end_alert_threshold
|
||||
|
||||
@property
|
||||
def last_slope(self) -> float:
|
||||
|
||||
@@ -51,6 +51,7 @@ async def async_setup_entry(
|
||||
LastTemperatureSensor(hass, unique_id, name, entry.data),
|
||||
LastExtTemperatureSensor(hass, unique_id, name, entry.data),
|
||||
TemperatureSlopeSensor(hass, unique_id, name, entry.data),
|
||||
EMATemperatureSensor(hass, unique_id, name, entry.data),
|
||||
]
|
||||
if entry.data.get(CONF_DEVICE_POWER):
|
||||
entities.append(EnergySensor(hass, unique_id, name, entry.data))
|
||||
@@ -505,17 +506,15 @@ class RegulatedTemperatureSensor(VersatileThermostatBaseEntity, SensorEntity):
|
||||
"""Called when my climate have change"""
|
||||
_LOGGER.debug("%s - climate state change", self._attr_unique_id)
|
||||
|
||||
if math.isnan(self.my_climate.regulated_target_temp) or math.isinf(
|
||||
self.my_climate.regulated_target_temp
|
||||
):
|
||||
raise ValueError(
|
||||
f"Sensor has illegal state {self.my_climate.regulated_target_temp}"
|
||||
)
|
||||
new_temp = self.my_climate.regulated_target_temp
|
||||
if new_temp is None:
|
||||
return
|
||||
|
||||
if math.isnan(new_temp) or math.isinf(new_temp):
|
||||
raise ValueError(f"Sensor has illegal state {new_temp}")
|
||||
|
||||
old_state = self._attr_native_value
|
||||
self._attr_native_value = round(
|
||||
self.my_climate.regulated_target_temp, self.suggested_display_precision
|
||||
)
|
||||
self._attr_native_value = round(new_temp, self.suggested_display_precision)
|
||||
if old_state != self._attr_native_value:
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
@@ -542,3 +541,54 @@ class RegulatedTemperatureSensor(VersatileThermostatBaseEntity, SensorEntity):
|
||||
def suggested_display_precision(self) -> int | None:
|
||||
"""Return the suggested number of decimal digits for display."""
|
||||
return 1
|
||||
|
||||
|
||||
class EMATemperatureSensor(VersatileThermostatBaseEntity, SensorEntity):
|
||||
"""Representation of a Exponential Moving Average temp"""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, unique_id, name, entry_infos) -> None:
|
||||
"""Initialize the regulated temperature sensor"""
|
||||
super().__init__(hass, unique_id, entry_infos.get(CONF_NAME))
|
||||
self._attr_name = "EMA temperature"
|
||||
self._attr_unique_id = f"{self._device_name}_ema_temperature"
|
||||
|
||||
@callback
|
||||
async def async_my_climate_changed(self, event: Event = None):
|
||||
"""Called when my climate have change"""
|
||||
_LOGGER.debug("%s - climate state change", self._attr_unique_id)
|
||||
|
||||
new_ema = self.my_climate.ema_temperature
|
||||
if new_ema is None:
|
||||
return
|
||||
|
||||
if math.isnan(new_ema) or math.isinf(new_ema):
|
||||
raise ValueError(f"Sensor has illegal state {new_ema}")
|
||||
|
||||
old_state = self._attr_native_value
|
||||
self._attr_native_value = new_ema
|
||||
if old_state != self._attr_native_value:
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
||||
@property
|
||||
def icon(self) -> str | None:
|
||||
return "mdi:thermometer-lines"
|
||||
|
||||
@property
|
||||
def device_class(self) -> SensorDeviceClass | None:
|
||||
return SensorDeviceClass.TEMPERATURE
|
||||
|
||||
@property
|
||||
def state_class(self) -> SensorStateClass | None:
|
||||
return SensorStateClass.MEASUREMENT
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self) -> str | None:
|
||||
if not self.my_climate:
|
||||
return UnitOfTemperature.CELSIUS
|
||||
return self.my_climate.temperature_unit
|
||||
|
||||
@property
|
||||
def suggested_display_precision(self) -> int | None:
|
||||
"""Return the suggested number of decimal digits for display."""
|
||||
return 2
|
||||
|
||||
@@ -363,12 +363,12 @@ async def test_over_climate_regulation_limitations(
|
||||
"custom_components.versatile_thermostat.commons.NowClass.get_now",
|
||||
return_value=event_timestamp,
|
||||
):
|
||||
await send_temperature_change_event(entity, 16, event_timestamp)
|
||||
await send_temperature_change_event(entity, 15, event_timestamp)
|
||||
await send_ext_temperature_change_event(entity, 12, event_timestamp)
|
||||
|
||||
# the regulated should have been done
|
||||
assert entity.regulated_target_temp != old_regulated_temp
|
||||
assert entity.regulated_target_temp >= entity.target_temperature
|
||||
assert (
|
||||
entity.regulated_target_temp == 17 + 0.5
|
||||
entity.regulated_target_temp == 17 + 1.5
|
||||
) # 0.7 without round_to_nearest
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
""" Test the OpenWindow algorithm """
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from custom_components.versatile_thermostat.open_window_algorithm import WindowOpenDetectionAlgorithm
|
||||
from custom_components.versatile_thermostat.open_window_algorithm import (
|
||||
WindowOpenDetectionAlgorithm,
|
||||
)
|
||||
|
||||
from .commons import * # pylint: disable=wildcard-import, unused-wildcard-import
|
||||
|
||||
@@ -19,18 +21,28 @@ async def test_open_window_algo(
|
||||
tz = get_tz(hass) # pylint: disable=invalid-name
|
||||
now = datetime.now(tz)
|
||||
|
||||
event_timestamp = now - timedelta(minutes=5)
|
||||
event_timestamp = now - timedelta(minutes=10)
|
||||
last_slope = the_algo.add_temp_measurement(
|
||||
temperature=10, datetime_measure=event_timestamp
|
||||
)
|
||||
|
||||
# We need at least 2 measurement
|
||||
# We need at least 4 measurement
|
||||
assert last_slope is None
|
||||
assert the_algo.last_slope is None
|
||||
assert the_algo.is_window_close_detected() is False
|
||||
assert the_algo.is_window_open_detected() is False
|
||||
|
||||
event_timestamp = now - timedelta(minutes=4)
|
||||
event_timestamp = now - timedelta(minutes=9)
|
||||
last_slope = the_algo.add_temp_measurement(
|
||||
temperature=10, datetime_measure=event_timestamp
|
||||
)
|
||||
|
||||
event_timestamp = now - timedelta(minutes=8)
|
||||
last_slope = the_algo.add_temp_measurement(
|
||||
temperature=10, datetime_measure=event_timestamp
|
||||
)
|
||||
|
||||
event_timestamp = now - timedelta(minutes=7)
|
||||
last_slope = the_algo.add_temp_measurement(
|
||||
temperature=10, datetime_measure=event_timestamp
|
||||
)
|
||||
@@ -41,19 +53,19 @@ async def test_open_window_algo(
|
||||
assert the_algo.is_window_close_detected() is True
|
||||
assert the_algo.is_window_open_detected() is False
|
||||
|
||||
event_timestamp = now - timedelta(minutes=3)
|
||||
event_timestamp = now - timedelta(minutes=6)
|
||||
last_slope = the_algo.add_temp_measurement(
|
||||
temperature=9, datetime_measure=event_timestamp
|
||||
)
|
||||
|
||||
# A slope is calculated
|
||||
assert last_slope == -0.5
|
||||
assert the_algo.last_slope == -0.5
|
||||
assert last_slope == -0.8
|
||||
assert the_algo.last_slope == -0.8
|
||||
assert the_algo.is_window_close_detected() is False
|
||||
assert the_algo.is_window_open_detected() is False
|
||||
|
||||
# A new temperature with 2 degre less in one minute (value will be rejected)
|
||||
event_timestamp = now - timedelta(minutes=2)
|
||||
event_timestamp = now - timedelta(minutes=5)
|
||||
last_slope = the_algo.add_temp_measurement(
|
||||
temperature=7, datetime_measure=event_timestamp
|
||||
)
|
||||
@@ -65,7 +77,7 @@ async def test_open_window_algo(
|
||||
assert the_algo.is_window_open_detected() is True
|
||||
|
||||
# A new temperature with 1 degre less
|
||||
event_timestamp = now - timedelta(minutes=1)
|
||||
event_timestamp = now - timedelta(minutes=4)
|
||||
last_slope = the_algo.add_temp_measurement(
|
||||
temperature=6, datetime_measure=event_timestamp
|
||||
)
|
||||
@@ -77,7 +89,7 @@ async def test_open_window_algo(
|
||||
assert the_algo.is_window_open_detected() is True
|
||||
|
||||
# A new temperature with 0 degre less
|
||||
event_timestamp = now - timedelta(minutes=0)
|
||||
event_timestamp = now - timedelta(minutes=3)
|
||||
last_slope = the_algo.add_temp_measurement(
|
||||
temperature=6, datetime_measure=event_timestamp
|
||||
)
|
||||
@@ -89,7 +101,7 @@ async def test_open_window_algo(
|
||||
assert the_algo.is_window_open_detected() is False
|
||||
|
||||
# A new temperature with 1 degre more
|
||||
event_timestamp = now + timedelta(minutes=1)
|
||||
event_timestamp = now + timedelta(minutes=2)
|
||||
last_slope = the_algo.add_temp_measurement(
|
||||
temperature=7, datetime_measure=event_timestamp
|
||||
)
|
||||
|
||||
@@ -477,6 +477,14 @@ async def test_window_auto_auto_stop(hass: HomeAssistant, skip_hass_states_is_st
|
||||
|
||||
assert entity.window_state is STATE_OFF
|
||||
|
||||
# Initialize the slope algo with 2 measurements
|
||||
# event_timestamp = now - timedelta(minutes=9)
|
||||
# await send_temperature_change_event(entity, 19, event_timestamp)
|
||||
# event_timestamp = now - timedelta(minutes=8)
|
||||
# await send_temperature_change_event(entity, 19, event_timestamp)
|
||||
# event_timestamp = now - timedelta(minutes=7)
|
||||
# await send_temperature_change_event(entity, 19, event_timestamp)
|
||||
|
||||
# Make the temperature down
|
||||
with patch(
|
||||
"custom_components.versatile_thermostat.base_thermostat.BaseThermostat.send_event"
|
||||
@@ -486,6 +494,7 @@ async def test_window_auto_auto_stop(hass: HomeAssistant, skip_hass_states_is_st
|
||||
"custom_components.versatile_thermostat.underlyings.UnderlyingClimate.is_device_active",
|
||||
return_value=True,
|
||||
):
|
||||
# This is the 3rd measurment. Slope is not ready
|
||||
event_timestamp = now - timedelta(minutes=4)
|
||||
await send_temperature_change_event(entity, 19, event_timestamp)
|
||||
|
||||
@@ -531,7 +540,7 @@ async def test_window_auto_auto_stop(hass: HomeAssistant, skip_hass_states_is_st
|
||||
assert entity.window_auto_state == STATE_ON
|
||||
assert entity.hvac_mode is HVACMode.OFF
|
||||
|
||||
# Waits for automatic disable
|
||||
# Waits for automatic disable
|
||||
with patch(
|
||||
"custom_components.versatile_thermostat.base_thermostat.BaseThermostat.send_event"
|
||||
) as mock_send_event, patch(
|
||||
|
||||
Reference in New Issue
Block a user