Skip to content

Commit

Permalink
fix(notifications): handle new recurrence rules
Browse files Browse the repository at this point in the history
Handle new Amazon reminder and alarm recurrence rules.

closes #1608
  • Loading branch information
alandtse committed Jun 26, 2022
1 parent 33f5d4c commit 2c70eda
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
9 changes: 9 additions & 0 deletions custom_components/alexa_media/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
"XXXX-WXX-7": "Every Sunday",
}

RECURRING_DAY = {
"MO": 1,
"TU": 2,
"WE": 3,
"TH": 4,
"FR": 5,
"SA": 6,
"SU": 7,
}
RECURRING_PATTERN_ISO_SET = {
None: {},
"P1D": {1, 2, 3, 4, 5, 6, 7},
Expand Down
24 changes: 18 additions & 6 deletions custom_components/alexa_media/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .alexa_entity import parse_temperature_from_coordinator
from .const import (
CONF_EXTENDED_ENTITY_DISCOVERY,
RECURRING_DAY,
RECURRING_PATTERN,
RECURRING_PATTERN_ISO_SET,
)
Expand Down Expand Up @@ -359,6 +360,7 @@ def _update_recurring_alarm(self, value):
next_item = value[1]
alarm = next_item[self._sensor_property]
reminder = None
recurrence = []
if isinstance(next_item[self._sensor_property], (int, float)):
reminder = True
alarm = dt.as_local(
Expand All @@ -367,12 +369,22 @@ def _update_recurring_alarm(self, value):
)
)
alarm_on = next_item["status"] == "ON"
recurring_pattern = next_item.get("recurringPattern")
r_rule_data = next_item.get("rRuleData")
if r_rule_data: # the new recurrence pattern; https://github.com/custom-components/alexa_media_player/issues/1608
next_trigger_times = r_rule_data.get("nextTriggerTimes")
weekdays = r_rule_data.get("byWeekDays")
if next_trigger_times:
alarm = next_trigger_times[0]
elif weekdays:
for day in weekdays:
recurrence.append(RECURRING_DAY[day])
else:
recurring_pattern = next_item.get("recurringPattern")
recurrence = RECURRING_PATTERN_ISO_SET.get(recurring_pattern)
while (
alarm_on
and recurring_pattern
and RECURRING_PATTERN_ISO_SET.get(recurring_pattern)
and alarm.isoweekday not in RECURRING_PATTERN_ISO_SET[recurring_pattern]
and recurrence
and alarm.isoweekday not in recurrence
and alarm < dt.now()
):
alarm += datetime.timedelta(days=1)
Expand All @@ -382,10 +394,10 @@ def _update_recurring_alarm(self, value):
_LOGGER.debug(
"%s with recurrence %s set to %s",
next_item["type"],
RECURRING_PATTERN.get(recurring_pattern),
recurrence,
alarm,
)
next_item[self._sensor_property] = alarm
next_item[self._sensor_property] = alarm
return value

@staticmethod
Expand Down

0 comments on commit 2c70eda

Please sign in to comment.