From 388a5d8c9174baa29b3cf51693428952e5ed8aa1 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 19 Oct 2020 09:42:00 +0200 Subject: [PATCH] Add entity ID to time trigger (#41933) --- .../components/homeassistant/triggers/time.py | 23 +++++++++++++++---- .../homeassistant/triggers/test_time.py | 12 +++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/homeassistant/triggers/time.py b/homeassistant/components/homeassistant/triggers/time.py index 20abcedb341..adfe592319b 100644 --- a/homeassistant/components/homeassistant/triggers/time.py +++ b/homeassistant/components/homeassistant/triggers/time.py @@ -37,11 +37,18 @@ async def async_attach_trigger(hass, config, action, automation_info): job = HassJob(action) @callback - def time_automation_listener(description, now): + def time_automation_listener(description, now, *, entity_id=None): """Listen for time changes and calls action.""" hass.async_run_hass_job( job, - {"trigger": {"platform": "time", "now": now, "description": description}}, + { + "trigger": { + "platform": "time", + "now": now, + "description": description, + "entity_id": entity_id, + } + }, ) @callback @@ -84,14 +91,22 @@ async def async_attach_trigger(hass, config, action, automation_info): if trigger_dt >= dt_util.now(): remove = async_track_point_in_time( hass, - partial(time_automation_listener, f"time set in {entity_id}"), + partial( + time_automation_listener, + f"time set in {entity_id}", + entity_id=entity_id, + ), trigger_dt, ) elif has_time: # Else if it has time, then track time change. remove = async_track_time_change( hass, - partial(time_automation_listener, f"time set in {entity_id}"), + partial( + time_automation_listener, + f"time set in {entity_id}", + entity_id=entity_id, + ), hour=hour, minute=minute, second=second, diff --git a/tests/components/homeassistant/triggers/test_time.py b/tests/components/homeassistant/triggers/test_time.py index d38f82be11c..db10e836629 100644 --- a/tests/components/homeassistant/triggers/test_time.py +++ b/tests/components/homeassistant/triggers/test_time.py @@ -93,7 +93,7 @@ async def test_if_fires_using_at_input_datetime(hass, calls, has_date, has_time) time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1) - some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}" + some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}" with patch( "homeassistant.util.dt.utcnow", return_value=dt_util.as_utc(time_that_will_not_match_right_away), @@ -117,7 +117,10 @@ async def test_if_fires_using_at_input_datetime(hass, calls, has_date, has_time) await hass.async_block_till_done() assert len(calls) == 1 - assert calls[0].data["some"] == f"time-{trigger_dt.day}-{trigger_dt.hour}" + assert ( + calls[0].data["some"] + == f"time-{trigger_dt.day}-{trigger_dt.hour}-input_datetime.trigger" + ) if has_date: trigger_dt += timedelta(days=1) @@ -138,7 +141,10 @@ async def test_if_fires_using_at_input_datetime(hass, calls, has_date, has_time) await hass.async_block_till_done() assert len(calls) == 2 - assert calls[1].data["some"] == f"time-{trigger_dt.day}-{trigger_dt.hour}" + assert ( + calls[1].data["some"] + == f"time-{trigger_dt.day}-{trigger_dt.hour}-input_datetime.trigger" + ) async def test_if_fires_using_multiple_at(hass, calls):