diff --git a/homeassistant/components/homeassistant/triggers/time.py b/homeassistant/components/homeassistant/triggers/time.py index b17c4201bd2304..398a399f4c37fb 100644 --- a/homeassistant/components/homeassistant/triggers/time.py +++ b/homeassistant/components/homeassistant/triggers/time.py @@ -268,9 +268,9 @@ def update_entity_trigger( # entity update_entity_trigger(at_time, new_state=hass.states.get(at_time)) to_track.append(TrackEntity(at_time, update_entity_trigger_event)) - elif isinstance(at_time, dict) and CONF_OFFSET in at_time: - # entity with offset - entity_id: str = at_time.get(CONF_ENTITY_ID, "") + elif isinstance(at_time, dict): + # entity with optional offset + entity_id: str = at_time[CONF_ENTITY_ID] offset: timedelta = at_time.get(CONF_OFFSET, timedelta(0)) update_entity_trigger( entity_id, new_state=hass.states.get(entity_id), offset=offset diff --git a/tests/components/homeassistant/triggers/test_time.py b/tests/components/homeassistant/triggers/test_time.py index 3aeae2380610bc..5ab5b34a6f9163 100644 --- a/tests/components/homeassistant/triggers/test_time.py +++ b/tests/components/homeassistant/triggers/test_time.py @@ -732,6 +732,52 @@ async def test_if_fires_using_at_sensor_with_offset( ) +async def test_if_fires_using_at_sensor_dict_without_offset( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + service_calls: list[ServiceCall], +) -> None: + """Test for firing at sensor time using dict format without offset.""" + now = dt_util.now() + + trigger_dt = now.replace(hour=5, minute=0, second=0, microsecond=0) + timedelta(2) + + hass.states.async_set( + "sensor.next_alarm", + trigger_dt.isoformat(), + {ATTR_DEVICE_CLASS: SensorDeviceClass.TIMESTAMP}, + ) + + time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1) + + freezer.move_to(dt_util.as_utc(time_that_will_not_match_right_away)) + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time", + "at": { + "entity_id": "sensor.next_alarm", + }, + }, + "action": { + "service": "test.automation", + "data_template": {"some": "{{ trigger.entity_id }}"}, + }, + } + }, + ) + await hass.async_block_till_done() + + async_fire_time_changed(hass, trigger_dt + timedelta(seconds=1)) + await hass.async_block_till_done() + + assert len(service_calls) == 1 + assert service_calls[0].data["some"] == "sensor.next_alarm" + + @pytest.mark.parametrize( "conf", [