From b2f5ab200811c19284dea81ba298a4566fd87eda Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Fri, 11 Feb 2022 21:22:53 -0800 Subject: [PATCH] Publish Nest Motion/Person events with optional user defined zone information (#66187) Publish Nest events with zone information if present. User defined zones are configured in the Google Home app, and are published with Motion/Person event. --- homeassistant/components/nest/__init__.py | 2 ++ homeassistant/components/nest/events.py | 3 +- tests/components/nest/test_events.py | 36 ++++++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index 85513378ed7..1083b80ac47 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -158,6 +158,8 @@ class SignalUpdateCallback: "timestamp": event_message.timestamp, "nest_event_id": image_event.event_token, } + if image_event.zones: + message["zones"] = image_event.zones self._hass.bus.async_fire(NEST_EVENT, message) diff --git a/homeassistant/components/nest/events.py b/homeassistant/components/nest/events.py index 10983768e17..752ab0e5069 100644 --- a/homeassistant/components/nest/events.py +++ b/homeassistant/components/nest/events.py @@ -25,7 +25,8 @@ NEST_EVENT = "nest_event" # "device_id": "my-device-id", # "type": "camera_motion", # "timestamp": "2021-10-24T19:42:43.304000+00:00", -# "nest_event_id": "KcO1HIR9sPKQ2bqby_vTcCcEov..." +# "nest_event_id": "KcO1HIR9sPKQ2bqby_vTcCcEov...", +# "zones": ["Zone 1"], # }, # ... # } diff --git a/tests/components/nest/test_events.py b/tests/components/nest/test_events.py index ee286242a8c..0ab387a7dea 100644 --- a/tests/components/nest/test_events.py +++ b/tests/components/nest/test_events.py @@ -28,7 +28,7 @@ NEST_EVENT = "nest_event" EVENT_SESSION_ID = "CjY5Y3VKaTZwR3o4Y19YbTVfMF..." EVENT_ID = "FWWVQVUdGNUlTU2V4MGV2aTNXV..." -EVENT_KEYS = {"device_id", "type", "timestamp"} +EVENT_KEYS = {"device_id", "type", "timestamp", "zones"} def event_view(d: Mapping[str, Any]) -> Mapping[str, Any]: @@ -514,3 +514,37 @@ async def test_structure_update_event(hass): assert registry.async_get("camera.front") # Currently need a manual reload to detect the new entity assert not registry.async_get("camera.back") + + +async def test_event_zones(hass): + """Test events published with zone information.""" + events = async_capture_events(hass, NEST_EVENT) + subscriber = await async_setup_devices( + hass, + "sdm.devices.types.DOORBELL", + create_device_traits(["sdm.devices.traits.CameraMotion"]), + ) + registry = er.async_get(hass) + entry = registry.async_get("camera.front") + assert entry is not None + + event_map = { + "sdm.devices.events.CameraMotion.Motion": { + "eventSessionId": EVENT_SESSION_ID, + "eventId": EVENT_ID, + "zones": ["Zone 1"], + }, + } + + 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) == 1 + assert event_view(events[0].data) == { + "device_id": entry.device_id, + "type": "camera_motion", + "timestamp": event_time, + "zones": ["Zone 1"], + }