From 37143f048e5d17939924498bb747b9dc096232e1 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 8 Jul 2026 17:28:18 +0200 Subject: [PATCH] Use TimerEntityStateAttribute enum in timer (#175971) Co-authored-by: Claude Opus 4.8 (1M context) --- homeassistant/components/timer/__init__.py | 14 ++++++++---- .../components/timer/reproduce_state.py | 9 +++++--- homeassistant/components/timer/trigger.py | 22 +++++++++++++------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/timer/__init__.py b/homeassistant/components/timer/__init__.py index 90a72ec4119f..de19c3cf1e71 100644 --- a/homeassistant/components/timer/__init__.py +++ b/homeassistant/components/timer/__init__.py @@ -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): diff --git a/homeassistant/components/timer/reproduce_state.py b/homeassistant/components/timer/reproduce_state.py index 95cec586c3d9..10bef2a774d1 100644 --- a/homeassistant/components/timer/reproduce_state.py +++ b/homeassistant/components/timer/reproduce_state.py @@ -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: diff --git a/homeassistant/components/timer/trigger.py b/homeassistant/components/timer/trigger.py index 8f4152ce507f..28349cff3b2a 100644 --- a/homeassistant/components/timer/trigger.py +++ b/homeassistant/components/timer/trigger.py @@ -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, }