diff --git a/homeassistant/components/text/trigger.py b/homeassistant/components/text/trigger.py index 932d4572138..c29668f4f52 100644 --- a/homeassistant/components/text/trigger.py +++ b/homeassistant/components/text/trigger.py @@ -17,12 +17,12 @@ class TextChangedTrigger(EntityTriggerBase): _domain = DOMAIN _schema = ENTITY_STATE_TRIGGER_SCHEMA - def is_from_state(self, from_state: State, to_state: State) -> bool: - """Check if the state matches the origin state.""" + def is_valid_transition(self, from_state: State, to_state: State) -> bool: + """Check if the old and new states are different.""" return from_state.state != to_state.state - def is_to_state(self, state: State) -> bool: - """Check if the state matches the target state.""" + def is_valid_state(self, state: State) -> bool: + """Check if the new state is not invalid.""" return state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN) diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index 033ebd93a29..bb14f51efb6 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -337,18 +337,18 @@ class EntityTriggerBase(Trigger): self._options = config.options or {} self._target = config.target - def is_from_state(self, from_state: State, to_state: State) -> bool: - """Check if the state matches the origin state.""" - return not self.is_to_state(from_state) + def is_valid_transition(self, from_state: State, to_state: State) -> bool: + """Check if the origin state is not an expected target states.""" + return not self.is_valid_state(from_state) @abc.abstractmethod - def is_to_state(self, state: State) -> bool: - """Check if the state matches the target state.""" + def is_valid_state(self, state: State) -> bool: + """Check if the new state matches the expected state(s).""" def check_all_match(self, entity_ids: set[str]) -> bool: """Check if all entity states match.""" return all( - self.is_to_state(state) + self.is_valid_state(state) for entity_id in entity_ids if (state := self._hass.states.get(entity_id)) is not None ) @@ -357,7 +357,7 @@ class EntityTriggerBase(Trigger): """Check that only one entity state matches.""" return ( sum( - self.is_to_state(state) + self.is_valid_state(state) for entity_id in entity_ids if (state := self._hass.states.get(entity_id)) is not None ) @@ -394,12 +394,12 @@ class EntityTriggerBase(Trigger): if not from_state or from_state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN): return - # The trigger should never fire if the previous state was not the from state - if not to_state or not self.is_from_state(from_state, to_state): + # The trigger should never fire if the new state is not valid + if not to_state or not self.is_valid_state(to_state): return - # The trigger should never fire if the new state is not the to state - if not self.is_to_state(to_state): + # The trigger should never fire if the transition is not valid + if not self.is_valid_transition(from_state, to_state): return if behavior == BEHAVIOR_LAST: @@ -433,8 +433,8 @@ class EntityStateTriggerBase(EntityTriggerBase): _to_state: str - def is_to_state(self, state: State) -> bool: - """Check if the state matches the target state.""" + def is_valid_state(self, state: State) -> bool: + """Check if the new state matches the expected state.""" return state.state == self._to_state @@ -444,12 +444,12 @@ class ConditionalEntityStateTriggerBase(EntityTriggerBase): _from_states: set[str] _to_states: set[str] - def is_from_state(self, from_state: State, to_state: State) -> bool: - """Check if the state matches the origin state.""" + def is_valid_transition(self, from_state: State, to_state: State) -> bool: + """Check if the origin state matches the expected ones.""" return from_state.state in self._from_states - def is_to_state(self, state: State) -> bool: - """Check if the state matches the target state.""" + def is_valid_state(self, state: State) -> bool: + """Check if the new state matches the expected states.""" return state.state in self._to_states @@ -459,8 +459,8 @@ class EntityStateAttributeTriggerBase(EntityTriggerBase): _attribute: str _attribute_to_state: str - def is_to_state(self, state: State) -> bool: - """Check if the state matches the target state.""" + def is_valid_state(self, state: State) -> bool: + """Check if the new state attribute matches the expected one.""" return state.attributes.get(self._attribute) == self._attribute_to_state