1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Rename trigger helper state checkers (#158537)

Co-authored-by: Robert Resch <robert@resch.dev>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Abílio Costa
2025-12-10 14:56:56 +00:00
committed by GitHub
parent 5992898340
commit 7c7c0aad25
2 changed files with 23 additions and 23 deletions

View File

@@ -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)

View File

@@ -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