Rework on Google Assistant doorbell support (#100930)

* Rework on Google Assistant doorbell event

* Additional comment on syncing notificatiions

* Update homeassistant/components/google_assistant/trait.py

* Only sync event if state attr changed

* Update comment
This commit is contained in:
Jan Bouwhuis 2023-10-07 19:39:04 +02:00 committed by GitHub
parent 8c2a2e5c37
commit 4709e60ff6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 4 deletions

View File

@ -240,7 +240,7 @@ class AbstractConfig(ABC):
async def async_sync_notification( async def async_sync_notification(
self, agent_user_id: str, event_id: str, payload: dict[str, Any] self, agent_user_id: str, event_id: str, payload: dict[str, Any]
) -> HTTPStatus: ) -> HTTPStatus:
"""Sync notification to Google.""" """Sync notifications to Google."""
# Remove any pending sync # Remove any pending sync
self._google_sync_unsub.pop(agent_user_id, lambda: None)() self._google_sync_unsub.pop(agent_user_id, lambda: None)()
status = await self.async_report_state(payload, agent_user_id, event_id) status = await self.async_report_state(payload, agent_user_id, event_id)

View File

@ -80,7 +80,15 @@ def async_enable_report_state(hass: HomeAssistant, google_config: AbstractConfig
): ):
return return
if (notifications := entity.notifications_serialize()) is not None: # We only trigger notifications on changes in the state value, not attributes.
# This is mainly designed for our event entity types
# We need to synchronize notifications using a `SYNC` response,
# together with other state changes.
if (
old_state
and old_state.state != new_state.state
and (notifications := entity.notifications_serialize()) is not None
):
event_id = uuid4().hex event_id = uuid4().hex
payload = { payload = {
"devices": {"notifications": {entity.state.entity_id: notifications}} "devices": {"notifications": {entity.state.entity_id: notifications}}

View File

@ -382,10 +382,14 @@ class ObjectDetection(_Trait):
return None return None
# Only notify if last event was less then 30 seconds ago # Only notify if last event was less then 30 seconds ago
time_stamp = datetime.fromisoformat(self.state.state) time_stamp: datetime = datetime.fromisoformat(self.state.state)
if (utcnow() - time_stamp) > timedelta(seconds=30): if (utcnow() - time_stamp) > timedelta(seconds=30):
return None return None
# A doorbell event is treated as an object detection of 1 unclassified object.
# The implementation follows the pattern from the Smart Home Doorbell Guide:
# https://developers.home.google.com/cloud-to-cloud/guides/doorbell
# The detectionTimestamp is the time in ms from January 1, 1970, 00:00:00 (UTC)
return { return {
"ObjectDetection": { "ObjectDetection": {
"objects": { "objects": {

View File

@ -223,7 +223,7 @@ async def test_onoff_input_boolean(hass: HomeAssistant) -> None:
@pytest.mark.freeze_time("2023-08-01T00:02:57+00:00") @pytest.mark.freeze_time("2023-08-01T00:02:57+00:00")
async def test_doorbell_event(hass: HomeAssistant) -> None: async def test_doorbell_event(hass: HomeAssistant) -> None:
"""Test doorbell event trait support for input_boolean domain.""" """Test doorbell event trait support for event domain."""
assert trait.ObjectDetection.supported(event.DOMAIN, 0, "doorbell", None) assert trait.ObjectDetection.supported(event.DOMAIN, 0, "doorbell", None)
state = State( state = State(