Skip to content

Commit

Permalink
_is_today function added
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronDavidSchneider committed Mar 3, 2021
1 parent 2421ccc commit 582883d
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions custom_components/sonos_alarm/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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."""
Expand All @@ -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
Expand All @@ -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."""
Expand Down

0 comments on commit 582883d

Please sign in to comment.