diff --git a/homeassistant/core.py b/homeassistant/core.py index 4c59e88e840..0f2a28b2fa4 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -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 diff --git a/tests/test_core.py b/tests/test_core.py index 4136249f993..efc0b875b4f 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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,