mirror of
https://github.com/home-assistant/core.git
synced 2026-08-03 20:24:55 +02:00
Fix time trigger crash when using entity_id dict format without offset (#171006)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
@@ -268,9 +268,9 @@ async def async_attach_trigger( # noqa: C901
|
||||
# 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
|
||||
|
||||
@@ -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",
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user