From f41cf79562a8d14b8b541537e3dc3bd580394fbb Mon Sep 17 00:00:00 2001 From: Gifford47 Date: Wed, 10 Apr 2024 14:49:43 +0200 Subject: [PATCH 1/3] added time and day related evu events (related #38) #248 --- custom_components/luxtronik/const.py | 1 + custom_components/luxtronik/sensor.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/custom_components/luxtronik/const.py b/custom_components/luxtronik/const.py index 3b9f127..e0d9adc 100644 --- a/custom_components/luxtronik/const.py +++ b/custom_components/luxtronik/const.py @@ -731,6 +731,7 @@ class SensorAttrKey(StrEnum): EVU_SECOND_START_TIME = "EVU_second_start_time" EVU_SECOND_END_TIME = "EVU_second_end_time" EVU_MINUTES_UNTIL_NEXT_EVENT = "EVU_minutes_until_next_event" + EVU_DAYS = "EVU_days" TIMESTAMP = "timestamp" CODE = "code" CAUSE = "cause" diff --git a/custom_components/luxtronik/sensor.py b/custom_components/luxtronik/sensor.py index 2c055f1..b678290 100644 --- a/custom_components/luxtronik/sensor.py +++ b/custom_components/luxtronik/sensor.py @@ -187,6 +187,7 @@ class LuxtronikStatusSensorEntity(LuxtronikSensorEntity, SensorEntity): _attr_cache[SA.EVU_FIRST_END_TIME] = time.min _attr_cache[SA.EVU_SECOND_START_TIME] = time.min _attr_cache[SA.EVU_SECOND_END_TIME] = time.min + _attr_cache[SA.EVU_DAYS] = [] _unrecorded_attributes = frozenset( LuxtronikSensorEntity._unrecorded_attributes @@ -198,6 +199,7 @@ class LuxtronikStatusSensorEntity(LuxtronikSensorEntity, SensorEntity): SA.EVU_SECOND_START_TIME, SA.EVU_SECOND_END_TIME, SA.EVU_MINUTES_UNTIL_NEXT_EVENT, + SA.EVU_DAYS, } ) @@ -212,6 +214,7 @@ def _handle_coordinator_update( super()._handle_coordinator_update(data) time_now = time(datetime.now().hour, datetime.now().minute) evu = LuxOperationMode.evu.value + weekday = datetime.today().weekday() if self._attr_native_value is None or self._last_state is None: pass elif self._attr_native_value == evu and str(self._last_state) != evu: @@ -228,6 +231,8 @@ def _handle_coordinator_update( self._attr_cache[SA.EVU_FIRST_START_TIME] = time_now else: self._attr_cache[SA.EVU_SECOND_START_TIME] = time_now + if weekday not in self._attr_cache[SA.EVU_DAYS]: + self._attr_cache[SA.EVU_DAYS].append(weekday) elif self._attr_native_value != evu and str(self._last_state) == evu: # evu end if ( @@ -384,7 +389,19 @@ def _calc_next_evu_event_minutes(self) -> int | None: if evu_time == time.min: return None evu_hours = (24 if evu_time < time_now else 0) + evu_time.hour - return (evu_hours - time_now.hour) * 60 + evu_time.minute - time_now.minute + weekday = datetime.today().weekday() + evu_pause = 0 + if not self._attr_cache[SA.EVU_DAYS] and weekday not in self._attr_cache[SA.EVU_DAYS]: + evu_pause += 1440 + for i in range(1, 7): + if weekday+i > 6: + i = -7+i + if weekday+i in self._attr_cache[SA.EVU_DAYS]: + return (evu_hours - time_now.hour) * 60 + evu_time.minute - time_now.minute + evu_pause + else: + evu_pause += 1440 + else: + return (evu_hours - time_now.hour) * 60 + evu_time.minute - time_now.minute def _get_next_evu_event_time(self) -> time: event: time = time.min From 3f392c63c85e2790ae5a6a200bc9fb7886221035 Mon Sep 17 00:00:00 2001 From: Gifford47 Date: Wed, 10 Apr 2024 15:07:08 +0200 Subject: [PATCH 2/3] added time and day related evu events (related #38) #248 --- custom_components/luxtronik/const.py | 1 + custom_components/luxtronik/sensor.py | 19 ++++++++++++++++++- .../luxtronik/sensor_entities_predefined.py | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/custom_components/luxtronik/const.py b/custom_components/luxtronik/const.py index 3b9f127..e0d9adc 100644 --- a/custom_components/luxtronik/const.py +++ b/custom_components/luxtronik/const.py @@ -731,6 +731,7 @@ class SensorAttrKey(StrEnum): EVU_SECOND_START_TIME = "EVU_second_start_time" EVU_SECOND_END_TIME = "EVU_second_end_time" EVU_MINUTES_UNTIL_NEXT_EVENT = "EVU_minutes_until_next_event" + EVU_DAYS = "EVU_days" TIMESTAMP = "timestamp" CODE = "code" CAUSE = "cause" diff --git a/custom_components/luxtronik/sensor.py b/custom_components/luxtronik/sensor.py index 2c055f1..b678290 100644 --- a/custom_components/luxtronik/sensor.py +++ b/custom_components/luxtronik/sensor.py @@ -187,6 +187,7 @@ class LuxtronikStatusSensorEntity(LuxtronikSensorEntity, SensorEntity): _attr_cache[SA.EVU_FIRST_END_TIME] = time.min _attr_cache[SA.EVU_SECOND_START_TIME] = time.min _attr_cache[SA.EVU_SECOND_END_TIME] = time.min + _attr_cache[SA.EVU_DAYS] = [] _unrecorded_attributes = frozenset( LuxtronikSensorEntity._unrecorded_attributes @@ -198,6 +199,7 @@ class LuxtronikStatusSensorEntity(LuxtronikSensorEntity, SensorEntity): SA.EVU_SECOND_START_TIME, SA.EVU_SECOND_END_TIME, SA.EVU_MINUTES_UNTIL_NEXT_EVENT, + SA.EVU_DAYS, } ) @@ -212,6 +214,7 @@ def _handle_coordinator_update( super()._handle_coordinator_update(data) time_now = time(datetime.now().hour, datetime.now().minute) evu = LuxOperationMode.evu.value + weekday = datetime.today().weekday() if self._attr_native_value is None or self._last_state is None: pass elif self._attr_native_value == evu and str(self._last_state) != evu: @@ -228,6 +231,8 @@ def _handle_coordinator_update( self._attr_cache[SA.EVU_FIRST_START_TIME] = time_now else: self._attr_cache[SA.EVU_SECOND_START_TIME] = time_now + if weekday not in self._attr_cache[SA.EVU_DAYS]: + self._attr_cache[SA.EVU_DAYS].append(weekday) elif self._attr_native_value != evu and str(self._last_state) == evu: # evu end if ( @@ -384,7 +389,19 @@ def _calc_next_evu_event_minutes(self) -> int | None: if evu_time == time.min: return None evu_hours = (24 if evu_time < time_now else 0) + evu_time.hour - return (evu_hours - time_now.hour) * 60 + evu_time.minute - time_now.minute + weekday = datetime.today().weekday() + evu_pause = 0 + if not self._attr_cache[SA.EVU_DAYS] and weekday not in self._attr_cache[SA.EVU_DAYS]: + evu_pause += 1440 + for i in range(1, 7): + if weekday+i > 6: + i = -7+i + if weekday+i in self._attr_cache[SA.EVU_DAYS]: + return (evu_hours - time_now.hour) * 60 + evu_time.minute - time_now.minute + evu_pause + else: + evu_pause += 1440 + else: + return (evu_hours - time_now.hour) * 60 + evu_time.minute - time_now.minute def _get_next_evu_event_time(self) -> time: event: time = time.min diff --git a/custom_components/luxtronik/sensor_entities_predefined.py b/custom_components/luxtronik/sensor_entities_predefined.py index f06e669..96476c4 100644 --- a/custom_components/luxtronik/sensor_entities_predefined.py +++ b/custom_components/luxtronik/sensor_entities_predefined.py @@ -51,6 +51,7 @@ attr(SA.EVU_FIRST_END_TIME, LC.UNSET, None, True), attr(SA.EVU_SECOND_START_TIME, LC.UNSET, None, True), attr(SA.EVU_SECOND_END_TIME, LC.UNSET, None, True), + attr(SA.EVU_DAYS, LC.UNSET, None, True), ), options=[e.value for e in LuxOperationMode], update_interval=UPDATE_INTERVAL_NORMAL, From 609a46631ea57307a1727729f8902b607c930308 Mon Sep 17 00:00:00 2001 From: Gifford47 Date: Wed, 10 Apr 2024 16:35:27 +0200 Subject: [PATCH 3/3] Merge branch 'main' of https://github.com/Gifford47/luxtronik --- custom_components/luxtronik/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/luxtronik/sensor.py b/custom_components/luxtronik/sensor.py index b678290..a382ae5 100644 --- a/custom_components/luxtronik/sensor.py +++ b/custom_components/luxtronik/sensor.py @@ -392,7 +392,7 @@ def _calc_next_evu_event_minutes(self) -> int | None: weekday = datetime.today().weekday() evu_pause = 0 if not self._attr_cache[SA.EVU_DAYS] and weekday not in self._attr_cache[SA.EVU_DAYS]: - evu_pause += 1440 + evu_pause += (24 - datetime.now().hour)*60 - datetime.now().minute for i in range(1, 7): if weekday+i > 6: i = -7+i