Use TimerEntityStateAttribute enum in timer (#175971)

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
epenet
2026-07-08 17:28:18 +02:00
committed by GitHub
co-authored by Claude Opus 4.8
parent bd575bbbce
commit 37143f048e
3 changed files with 31 additions and 14 deletions
+10 -4
View File
@@ -292,20 +292,26 @@ class Timer(collection.CollectionEntity, RestoreEntity):
# Begin restoring state
self._state = state.state
self._last_transition = state.attributes.get(ATTR_LAST_TRANSITION)
self._last_transition = state.attributes.get(
TimerEntityStateAttribute.LAST_TRANSITION
)
# Nothing more to do if the timer is idle
if self._state == STATUS_IDLE:
return
self._running_duration = cv.time_period(state.attributes[ATTR_DURATION])
self._running_duration = cv.time_period(
state.attributes[TimerEntityStateAttribute.DURATION]
)
# If the timer was paused, we restore the remaining time
if self._state == STATUS_PAUSED:
self._remaining = cv.time_period(state.attributes[ATTR_REMAINING])
self._remaining = cv.time_period(
state.attributes[TimerEntityStateAttribute.REMAINING]
)
return
# If we get here, the timer must have been active so we need to decide what
# to do based on end time and the current time
end = cv.datetime(state.attributes[ATTR_FINISHES_AT])
end = cv.datetime(state.attributes[TimerEntityStateAttribute.FINISHES_AT])
# If there is time remaining in the timer, restore the remaining time then
# start the timer
if (remaining := end - dt_util.utcnow().replace(microsecond=0)) > timedelta(0):
@@ -17,6 +17,7 @@ from . import (
STATUS_ACTIVE,
STATUS_IDLE,
STATUS_PAUSED,
TimerEntityStateAttribute,
)
_LOGGER = logging.getLogger(__name__)
@@ -45,15 +46,17 @@ async def _async_reproduce_state(
# Return if we are already at the right state.
if cur_state.state == state.state and cur_state.attributes.get(
ATTR_DURATION
) == state.attributes.get(ATTR_DURATION):
) == state.attributes.get(TimerEntityStateAttribute.DURATION):
return
service_data = {ATTR_ENTITY_ID: state.entity_id}
if state.state == STATUS_ACTIVE:
service = SERVICE_START
if ATTR_DURATION in state.attributes:
service_data[ATTR_DURATION] = state.attributes[ATTR_DURATION]
if TimerEntityStateAttribute.DURATION in state.attributes:
service_data[ATTR_DURATION] = state.attributes[
TimerEntityStateAttribute.DURATION
]
elif state.state == STATUS_PAUSED:
service = SERVICE_PAUSE
elif state.state == STATUS_IDLE:
+15 -7
View File
@@ -26,7 +26,8 @@ from homeassistant.helpers.trigger import (
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import dt as dt_util
from . import ATTR_FINISHES_AT, ATTR_LAST_TRANSITION, DOMAIN, STATUS_ACTIVE
from . import DOMAIN, STATUS_ACTIVE
from .const import TimerEntityStateAttribute
CONF_REMAINING = "remaining"
@@ -86,7 +87,9 @@ class TimeRemainingTrigger(Trigger):
if to_state.state != STATUS_ACTIVE:
return
finishes_at_str = to_state.attributes.get(ATTR_FINISHES_AT)
finishes_at_str = to_state.attributes.get(
TimerEntityStateAttribute.FINISHES_AT
)
if finishes_at_str is None:
return
@@ -166,19 +169,24 @@ class TimeRemainingTrigger(Trigger):
TRIGGERS: dict[str, type[Trigger]] = {
"cancelled": make_entity_target_state_trigger(
{DOMAIN: DomainSpec(value_source=ATTR_LAST_TRANSITION)}, "cancelled"
{DOMAIN: DomainSpec(value_source=TimerEntityStateAttribute.LAST_TRANSITION)},
"cancelled",
),
"finished": make_entity_target_state_trigger(
{DOMAIN: DomainSpec(value_source=ATTR_LAST_TRANSITION)}, "finished"
{DOMAIN: DomainSpec(value_source=TimerEntityStateAttribute.LAST_TRANSITION)},
"finished",
),
"paused": make_entity_target_state_trigger(
{DOMAIN: DomainSpec(value_source=ATTR_LAST_TRANSITION)}, "paused"
{DOMAIN: DomainSpec(value_source=TimerEntityStateAttribute.LAST_TRANSITION)},
"paused",
),
"restarted": make_entity_target_state_trigger(
{DOMAIN: DomainSpec(value_source=ATTR_LAST_TRANSITION)}, "restarted"
{DOMAIN: DomainSpec(value_source=TimerEntityStateAttribute.LAST_TRANSITION)},
"restarted",
),
"started": make_entity_target_state_trigger(
{DOMAIN: DomainSpec(value_source=ATTR_LAST_TRANSITION)}, "started"
{DOMAIN: DomainSpec(value_source=TimerEntityStateAttribute.LAST_TRANSITION)},
"started",
),
"remaining_time_reached": TimeRemainingTrigger,
}