Avoid many string lowers in the state machine (#109607)
This commit is contained in:
parent
2c91b31233
commit
e35c7fde89
2 changed files with 22 additions and 5 deletions
|
@ -1757,7 +1757,9 @@ class StateMachine:
|
|||
|
||||
Async friendly.
|
||||
"""
|
||||
return self._states_data.get(entity_id.lower())
|
||||
return self._states_data.get(entity_id) or self._states_data.get(
|
||||
entity_id.lower()
|
||||
)
|
||||
|
||||
def is_state(self, entity_id: str, state: str) -> bool:
|
||||
"""Test if entity exists and is in specified state.
|
||||
|
@ -1870,10 +1872,16 @@ class StateMachine:
|
|||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
entity_id = entity_id.lower()
|
||||
new_state = str(new_state)
|
||||
attributes = attributes or {}
|
||||
if (old_state := self._states_data.get(entity_id)) is None:
|
||||
old_state = self._states_data.get(entity_id)
|
||||
if old_state is None:
|
||||
# If the state is missing, try to convert the entity_id to lowercase
|
||||
# and try again.
|
||||
entity_id = entity_id.lower()
|
||||
old_state = self._states_data.get(entity_id)
|
||||
|
||||
if old_state is None:
|
||||
same_state = False
|
||||
same_attr = False
|
||||
last_changed = None
|
||||
|
|
|
@ -1194,8 +1194,8 @@ async def test_statemachine_remove(hass: HomeAssistant) -> None:
|
|||
assert len(events) == 1
|
||||
|
||||
|
||||
async def test_statemachine_case_insensitivty(hass: HomeAssistant) -> None:
|
||||
"""Test insensitivty."""
|
||||
async def test_state_machine_case_insensitivity(hass: HomeAssistant) -> None:
|
||||
"""Test setting and getting states entity_id insensitivity."""
|
||||
events = async_capture_events(hass, EVENT_STATE_CHANGED)
|
||||
|
||||
hass.states.async_set("light.BOWL", "off")
|
||||
|
@ -1204,6 +1204,15 @@ async def test_statemachine_case_insensitivty(hass: HomeAssistant) -> None:
|
|||
assert hass.states.is_state("light.bowl", "off")
|
||||
assert len(events) == 1
|
||||
|
||||
hass.states.async_set("ligHT.Bowl", "on")
|
||||
assert hass.states.get("light.bowl").state == "on"
|
||||
|
||||
hass.states.async_set("light.BOWL", "off")
|
||||
assert hass.states.get("light.BoWL").state == "off"
|
||||
|
||||
hass.states.async_set("light.bowl", "on")
|
||||
assert hass.states.get("light.bowl").state == "on"
|
||||
|
||||
|
||||
async def test_statemachine_last_changed_not_updated_on_same_state(
|
||||
hass: HomeAssistant,
|
||||
|
|
Loading…
Add table
Reference in a new issue