From 582883d8044d64c3bd4ae36a5dee24a796f97cb6 Mon Sep 17 00:00:00 2001 From: Aaron Schneider Date: Wed, 3 Mar 2021 16:20:48 +0100 Subject: [PATCH] _is_today function added --- custom_components/sonos_alarm/switch.py | 30 +++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/custom_components/sonos_alarm/switch.py b/custom_components/sonos_alarm/switch.py index 03d98f1..20029c9 100644 --- a/custom_components/sonos_alarm/switch.py +++ b/custom_components/sonos_alarm/switch.py @@ -97,11 +97,11 @@ def _discovered_alarm(soco): _LOGGER.debug("Adding discovery job") hass.async_add_executor_job(_discovery) - class SonosAlarmSwitch(SwitchEntity): """Switch class for Sonos alarms.""" def __init__(self, soco, alarm): + _LOGGER.debug("Init Sonos alarms switch.") """Init Sonos alarms switch.""" self._icon = "mdi:alarm" self._soco = soco @@ -121,7 +121,6 @@ def __init__(self, soco, alarm): # pylint: disable=protected-access if one_alarm._alarm_id == self._id: self.alarm = one_alarm - self._is_on = self.alarm.enabled self._attributes = { ATTR_TIME: str(self.alarm.start_time), @@ -130,9 +129,11 @@ def __init__(self, soco, alarm): ATTR_INCLUDE_LINKED_ZONES: self.alarm.include_linked_zones, ATTR_RECURRENCE: str(self.alarm.recurrence), ATTR_PLAY_MODE: str(self.alarm.play_mode), - ATTR_SCHEDULED_TODAY: self.alarm.is_valid_recurrence(int(datetime.today().strftime('%w'))) + ATTR_SCHEDULED_TODAY: self._is_today } + _LOGGER.debug(self.alarm.recurrence) super().__init__() + _LOGGER.debug("reached end of init") def update(self, now=None): """Retrieve latest state.""" @@ -145,7 +146,7 @@ def update(self, now=None): self._attributes[ATTR_RECURRENCE] = str(self.alarm.recurrence) self._attributes[ATTR_VOLUME] = self.alarm.volume / 100 self._attributes[ATTR_PLAY_MODE] = str(self.alarm.play_mode) - self._attributes[ATTR_SCHEDULED_TODAY] = self.alarm.is_valid_recurrence(int(datetime.today().strftime('%w'))) + self._attributes[ATTR_SCHEDULED_TODAY] = self._is_today self._attributes[ ATTR_INCLUDE_LINKED_ZONES ] = self.alarm.include_linked_zones @@ -159,6 +160,27 @@ def update(self, now=None): ) self._is_available = False + @property + def _is_today(self): + recurrance = self.alarm.recurrence + timestr = int(datetime.today().strftime('%w')) + if recurrance[:2] == "ON": + if str(timestr) in recurrance: + return True + else: + return False + else: + if recurrance == "DAILY": + return True + elif recurrance == "ONCE": + return True + elif recurrance == "WEEKDAYS" and int(timestr) not in [0, 7]: + return True + elif recurrance == "WEEKENDS" and int(timestr) not in range(1, 7): + return True + else: + return False + @property def name(self): """Return name of Sonos alarm switch."""