diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index e4be96cbf14..1240d30f027 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -138,6 +138,7 @@ class SignalUpdateCallback: message = { "device_id": device_entry.id, "type": event_type, + "timestamp": event_message.timestamp, } self._hass.bus.async_fire(NEST_EVENT, message) diff --git a/tests/components/nest/test_device_trigger.py b/tests/components/nest/test_device_trigger.py index 29091f32f51..b7c75862153 100644 --- a/tests/components/nest/test_device_trigger.py +++ b/tests/components/nest/test_device_trigger.py @@ -10,6 +10,7 @@ from homeassistant.components.device_automation.exceptions import ( from homeassistant.components.nest import DOMAIN from homeassistant.components.nest.events import NEST_EVENT from homeassistant.setup import async_setup_component +from homeassistant.util.dt import utcnow from .common import async_setup_sdm_platform @@ -213,7 +214,7 @@ async def test_fires_on_camera_motion(hass, calls): """Test camera_motion triggers firing.""" assert await setup_automation(hass, DEVICE_ID, "camera_motion") - message = {"device_id": DEVICE_ID, "type": "camera_motion"} + message = {"device_id": DEVICE_ID, "type": "camera_motion", "timestamp": utcnow()} hass.bus.async_fire(NEST_EVENT, message) await hass.async_block_till_done() assert len(calls) == 1 @@ -224,7 +225,7 @@ async def test_fires_on_camera_person(hass, calls): """Test camera_person triggers firing.""" assert await setup_automation(hass, DEVICE_ID, "camera_person") - message = {"device_id": DEVICE_ID, "type": "camera_person"} + message = {"device_id": DEVICE_ID, "type": "camera_person", "timestamp": utcnow()} hass.bus.async_fire(NEST_EVENT, message) await hass.async_block_till_done() assert len(calls) == 1 @@ -235,7 +236,7 @@ async def test_fires_on_camera_sound(hass, calls): """Test camera_person triggers firing.""" assert await setup_automation(hass, DEVICE_ID, "camera_sound") - message = {"device_id": DEVICE_ID, "type": "camera_sound"} + message = {"device_id": DEVICE_ID, "type": "camera_sound", "timestamp": utcnow()} hass.bus.async_fire(NEST_EVENT, message) await hass.async_block_till_done() assert len(calls) == 1 @@ -246,7 +247,7 @@ async def test_fires_on_doorbell_chime(hass, calls): """Test doorbell_chime triggers firing.""" assert await setup_automation(hass, DEVICE_ID, "doorbell_chime") - message = {"device_id": DEVICE_ID, "type": "doorbell_chime"} + message = {"device_id": DEVICE_ID, "type": "doorbell_chime", "timestamp": utcnow()} hass.bus.async_fire(NEST_EVENT, message) await hass.async_block_till_done() assert len(calls) == 1 @@ -257,7 +258,11 @@ async def test_trigger_for_wrong_device_id(hass, calls): """Test for turn_on and turn_off triggers firing.""" assert await setup_automation(hass, DEVICE_ID, "camera_motion") - message = {"device_id": "wrong-device-id", "type": "camera_motion"} + message = { + "device_id": "wrong-device-id", + "type": "camera_motion", + "timestamp": utcnow(), + } hass.bus.async_fire(NEST_EVENT, message) await hass.async_block_till_done() assert len(calls) == 0 @@ -267,7 +272,11 @@ async def test_trigger_for_wrong_event_type(hass, calls): """Test for turn_on and turn_off triggers firing.""" assert await setup_automation(hass, DEVICE_ID, "camera_motion") - message = {"device_id": DEVICE_ID, "type": "wrong-event-type"} + message = { + "device_id": DEVICE_ID, + "type": "wrong-event-type", + "timestamp": utcnow(), + } hass.bus.async_fire(NEST_EVENT, message) await hass.async_block_till_done() assert len(calls) == 0 diff --git a/tests/components/nest/test_events.py b/tests/components/nest/test_events.py index 12314f60561..7295d134087 100644 --- a/tests/components/nest/test_events.py +++ b/tests/components/nest/test_events.py @@ -54,7 +54,7 @@ def create_device_traits(event_trait): } -def create_event(event_type, device_id=DEVICE_ID): +def create_event(event_type, device_id=DEVICE_ID, timestamp=None): """Create an EventMessage for a single event type.""" events = { event_type: { @@ -65,12 +65,14 @@ def create_event(event_type, device_id=DEVICE_ID): return create_events(events=events, device_id=device_id) -def create_events(events, device_id=DEVICE_ID): +def create_events(events, device_id=DEVICE_ID, timestamp=None): """Create an EventMessage for events.""" + if not timestamp: + timestamp = utcnow() return EventMessage( { "eventId": "some-event-id", - "timestamp": utcnow().isoformat(timespec="seconds"), + "timestamp": timestamp.isoformat(timespec="seconds"), "resourceUpdate": { "name": device_id, "events": events, @@ -102,15 +104,18 @@ async def test_doorbell_chime_event(hass): assert device.model == "Doorbell" assert device.identifiers == {("nest", DEVICE_ID)} + timestamp = utcnow() await subscriber.async_receive_event( - create_event("sdm.devices.events.DoorbellChime.Chime") + create_event("sdm.devices.events.DoorbellChime.Chime", timestamp=timestamp) ) await hass.async_block_till_done() + event_time = timestamp.replace(microsecond=0) assert len(events) == 1 assert events[0].data == { "device_id": entry.device_id, "type": "doorbell_chime", + "timestamp": event_time, } @@ -126,15 +131,18 @@ async def test_camera_motion_event(hass): entry = registry.async_get("camera.front") assert entry is not None + timestamp = utcnow() await subscriber.async_receive_event( - create_event("sdm.devices.events.CameraMotion.Motion") + create_event("sdm.devices.events.CameraMotion.Motion", timestamp=timestamp) ) await hass.async_block_till_done() + event_time = timestamp.replace(microsecond=0) assert len(events) == 1 assert events[0].data == { "device_id": entry.device_id, "type": "camera_motion", + "timestamp": event_time, } @@ -150,15 +158,18 @@ async def test_camera_sound_event(hass): entry = registry.async_get("camera.front") assert entry is not None + timestamp = utcnow() await subscriber.async_receive_event( - create_event("sdm.devices.events.CameraSound.Sound") + create_event("sdm.devices.events.CameraSound.Sound", timestamp=timestamp) ) await hass.async_block_till_done() + event_time = timestamp.replace(microsecond=0) assert len(events) == 1 assert events[0].data == { "device_id": entry.device_id, "type": "camera_sound", + "timestamp": event_time, } @@ -174,15 +185,18 @@ async def test_camera_person_event(hass): entry = registry.async_get("camera.front") assert entry is not None + timestamp = utcnow() await subscriber.async_receive_event( - create_event("sdm.devices.events.CameraPerson.Person") + create_event("sdm.devices.events.CameraPerson.Person", timestamp=timestamp) ) await hass.async_block_till_done() + event_time = timestamp.replace(microsecond=0) assert len(events) == 1 assert events[0].data == { "device_id": entry.device_id, "type": "camera_person", + "timestamp": event_time, } @@ -209,17 +223,21 @@ async def test_camera_multiple_event(hass): }, } - await subscriber.async_receive_event(create_events(event_map)) + timestamp = utcnow() + await subscriber.async_receive_event(create_events(event_map, timestamp=timestamp)) await hass.async_block_till_done() + event_time = timestamp.replace(microsecond=0) assert len(events) == 2 assert events[0].data == { "device_id": entry.device_id, "type": "camera_motion", + "timestamp": event_time, } assert events[1].data == { "device_id": entry.device_id, "type": "camera_person", + "timestamp": event_time, }