mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix Sonos alarm 'scheduled_today' attribute logic (#82816)
fixes undefined
This commit is contained in:
parent
db480191ad
commit
f887aeedfe
@ -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.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity import EntityCategory
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.event import async_track_time_change
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DATA_SONOS,
|
DATA_SONOS,
|
||||||
@ -91,6 +92,8 @@ FEATURE_ICONS = {
|
|||||||
ATTR_TOUCH_CONTROLS: "mdi:gesture-tap",
|
ATTR_TOUCH_CONTROLS: "mdi:gesture-tap",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WEEKEND_DAYS = (0, 6)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
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
|
@property
|
||||||
def alarm(self) -> Alarm:
|
def alarm(self) -> Alarm:
|
||||||
"""Return the alarm instance."""
|
"""Return the alarm instance."""
|
||||||
@ -304,14 +318,12 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity):
|
|||||||
def _is_today(self) -> bool:
|
def _is_today(self) -> bool:
|
||||||
"""Return whether this alarm is scheduled for today."""
|
"""Return whether this alarm is scheduled for today."""
|
||||||
recurrence = self.alarm.recurrence
|
recurrence = self.alarm.recurrence
|
||||||
timestr = int(datetime.datetime.today().strftime("%w"))
|
daynum = int(datetime.datetime.today().strftime("%w"))
|
||||||
return (
|
return (
|
||||||
bool(recurrence[:2] == "ON" and str(timestr) in recurrence)
|
recurrence in ("DAILY", "ONCE")
|
||||||
or bool(recurrence == "DAILY")
|
or (recurrence == "WEEKENDS" and daynum in WEEKEND_DAYS)
|
||||||
or bool(recurrence == "WEEKDAYS" and int(timestr) not in [0, 7])
|
or (recurrence == "WEEKDAYS" and daynum not in WEEKEND_DAYS)
|
||||||
or bool(recurrence == "ONCE")
|
or (recurrence.startswith("ON_") and str(daynum) in recurrence)
|
||||||
or bool(recurrence == "WEEKDAYS" and int(timestr) not in [0, 7])
|
|
||||||
or bool(recurrence == "WEEKENDS" and int(timestr) not in range(1, 7))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
Loading…
x
Reference in New Issue
Block a user