Publish timestamps in nest events (#44641)

This commit is contained in:
Allen Porter 2020-12-30 01:23:48 -08:00 committed by GitHub
parent a212248f8d
commit baacf2cd7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 14 deletions

View File

@ -138,6 +138,7 @@ class SignalUpdateCallback:
message = { message = {
"device_id": device_entry.id, "device_id": device_entry.id,
"type": event_type, "type": event_type,
"timestamp": event_message.timestamp,
} }
self._hass.bus.async_fire(NEST_EVENT, message) self._hass.bus.async_fire(NEST_EVENT, message)

View File

@ -10,6 +10,7 @@ from homeassistant.components.device_automation.exceptions import (
from homeassistant.components.nest import DOMAIN from homeassistant.components.nest import DOMAIN
from homeassistant.components.nest.events import NEST_EVENT from homeassistant.components.nest.events import NEST_EVENT
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
from .common import async_setup_sdm_platform 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.""" """Test camera_motion triggers firing."""
assert await setup_automation(hass, DEVICE_ID, "camera_motion") 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) hass.bus.async_fire(NEST_EVENT, message)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@ -224,7 +225,7 @@ async def test_fires_on_camera_person(hass, calls):
"""Test camera_person triggers firing.""" """Test camera_person triggers firing."""
assert await setup_automation(hass, DEVICE_ID, "camera_person") 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) hass.bus.async_fire(NEST_EVENT, message)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@ -235,7 +236,7 @@ async def test_fires_on_camera_sound(hass, calls):
"""Test camera_person triggers firing.""" """Test camera_person triggers firing."""
assert await setup_automation(hass, DEVICE_ID, "camera_sound") 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) hass.bus.async_fire(NEST_EVENT, message)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@ -246,7 +247,7 @@ async def test_fires_on_doorbell_chime(hass, calls):
"""Test doorbell_chime triggers firing.""" """Test doorbell_chime triggers firing."""
assert await setup_automation(hass, DEVICE_ID, "doorbell_chime") 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) hass.bus.async_fire(NEST_EVENT, message)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 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.""" """Test for turn_on and turn_off triggers firing."""
assert await setup_automation(hass, DEVICE_ID, "camera_motion") 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) hass.bus.async_fire(NEST_EVENT, message)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 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.""" """Test for turn_on and turn_off triggers firing."""
assert await setup_automation(hass, DEVICE_ID, "camera_motion") 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) hass.bus.async_fire(NEST_EVENT, message)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0

View File

@ -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.""" """Create an EventMessage for a single event type."""
events = { events = {
event_type: { event_type: {
@ -65,12 +65,14 @@ def create_event(event_type, device_id=DEVICE_ID):
return create_events(events=events, 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.""" """Create an EventMessage for events."""
if not timestamp:
timestamp = utcnow()
return EventMessage( return EventMessage(
{ {
"eventId": "some-event-id", "eventId": "some-event-id",
"timestamp": utcnow().isoformat(timespec="seconds"), "timestamp": timestamp.isoformat(timespec="seconds"),
"resourceUpdate": { "resourceUpdate": {
"name": device_id, "name": device_id,
"events": events, "events": events,
@ -102,15 +104,18 @@ async def test_doorbell_chime_event(hass):
assert device.model == "Doorbell" assert device.model == "Doorbell"
assert device.identifiers == {("nest", DEVICE_ID)} assert device.identifiers == {("nest", DEVICE_ID)}
timestamp = utcnow()
await subscriber.async_receive_event( 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() await hass.async_block_till_done()
event_time = timestamp.replace(microsecond=0)
assert len(events) == 1 assert len(events) == 1
assert events[0].data == { assert events[0].data == {
"device_id": entry.device_id, "device_id": entry.device_id,
"type": "doorbell_chime", "type": "doorbell_chime",
"timestamp": event_time,
} }
@ -126,15 +131,18 @@ async def test_camera_motion_event(hass):
entry = registry.async_get("camera.front") entry = registry.async_get("camera.front")
assert entry is not None assert entry is not None
timestamp = utcnow()
await subscriber.async_receive_event( 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() await hass.async_block_till_done()
event_time = timestamp.replace(microsecond=0)
assert len(events) == 1 assert len(events) == 1
assert events[0].data == { assert events[0].data == {
"device_id": entry.device_id, "device_id": entry.device_id,
"type": "camera_motion", "type": "camera_motion",
"timestamp": event_time,
} }
@ -150,15 +158,18 @@ async def test_camera_sound_event(hass):
entry = registry.async_get("camera.front") entry = registry.async_get("camera.front")
assert entry is not None assert entry is not None
timestamp = utcnow()
await subscriber.async_receive_event( 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() await hass.async_block_till_done()
event_time = timestamp.replace(microsecond=0)
assert len(events) == 1 assert len(events) == 1
assert events[0].data == { assert events[0].data == {
"device_id": entry.device_id, "device_id": entry.device_id,
"type": "camera_sound", "type": "camera_sound",
"timestamp": event_time,
} }
@ -174,15 +185,18 @@ async def test_camera_person_event(hass):
entry = registry.async_get("camera.front") entry = registry.async_get("camera.front")
assert entry is not None assert entry is not None
timestamp = utcnow()
await subscriber.async_receive_event( 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() await hass.async_block_till_done()
event_time = timestamp.replace(microsecond=0)
assert len(events) == 1 assert len(events) == 1
assert events[0].data == { assert events[0].data == {
"device_id": entry.device_id, "device_id": entry.device_id,
"type": "camera_person", "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() await hass.async_block_till_done()
event_time = timestamp.replace(microsecond=0)
assert len(events) == 2 assert len(events) == 2
assert events[0].data == { assert events[0].data == {
"device_id": entry.device_id, "device_id": entry.device_id,
"type": "camera_motion", "type": "camera_motion",
"timestamp": event_time,
} }
assert events[1].data == { assert events[1].data == {
"device_id": entry.device_id, "device_id": entry.device_id,
"type": "camera_person", "type": "camera_person",
"timestamp": event_time,
} }