Skip to content

Commit

Permalink
added time and day related evu events (related BenPru#38) BenPru#248
Browse files Browse the repository at this point in the history
  • Loading branch information
Gifford47 committed Apr 10, 2024
1 parent 6ad33e6 commit f41cf79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions custom_components/luxtronik/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 18 additions & 1 deletion custom_components/luxtronik/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
)

Expand All @@ -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:
Expand All @@ -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 (
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f41cf79

Please sign in to comment.