Fix Sonos alarm 'scheduled_today' attribute logic (#82816)

fixes undefined
This commit is contained in:
jjlawren 2022-11-28 08:31:32 -06:00 committed by GitHub
parent db480191ad
commit f887aeedfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_time_change
from .const import (
DATA_SONOS,
@ -91,6 +92,8 @@ FEATURE_ICONS = {
ATTR_TOUCH_CONTROLS: "mdi:gesture-tap",
}
WEEKEND_DAYS = (0, 6)
async def async_setup_entry(
hass: HomeAssistant,
@ -233,6 +236,17 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity):
)
)
async def async_write_state_daily(now: datetime.datetime) -> None:
"""Update alarm state attributes each calendar day."""
_LOGGER.debug("Updating state attributes for %s", self.name)
self.async_write_ha_state()
self.async_on_remove(
async_track_time_change(
self.hass, async_write_state_daily, hour=0, minute=0, second=0
)
)
@property
def alarm(self) -> Alarm:
"""Return the alarm instance."""
@ -304,14 +318,12 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity):
def _is_today(self) -> bool:
"""Return whether this alarm is scheduled for today."""
recurrence = self.alarm.recurrence
timestr = int(datetime.datetime.today().strftime("%w"))
daynum = int(datetime.datetime.today().strftime("%w"))
return (
bool(recurrence[:2] == "ON" and str(timestr) in recurrence)
or bool(recurrence == "DAILY")
or bool(recurrence == "WEEKDAYS" and int(timestr) not in [0, 7])
or bool(recurrence == "ONCE")
or bool(recurrence == "WEEKDAYS" and int(timestr) not in [0, 7])
or bool(recurrence == "WEEKENDS" and int(timestr) not in range(1, 7))
recurrence in ("DAILY", "ONCE")
or (recurrence == "WEEKENDS" and daynum in WEEKEND_DAYS)
or (recurrence == "WEEKDAYS" and daynum not in WEEKEND_DAYS)
or (recurrence.startswith("ON_") and str(daynum) in recurrence)
)
@property