Attach Matter info to Google Assistant serialize (#103768)

This commit is contained in:
Paulus Schoutsen 2023-11-24 06:55:05 -05:00 committed by GitHub
parent 378a708bf7
commit 130822fcc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 3 deletions

View file

@ -15,7 +15,7 @@ from aiohttp.web import json_response
from awesomeversion import AwesomeVersion from awesomeversion import AwesomeVersion
from yarl import URL from yarl import URL
from homeassistant.components import webhook from homeassistant.components import matter, webhook
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS, ATTR_DEVICE_CLASS,
ATTR_SUPPORTED_FEATURES, ATTR_SUPPORTED_FEATURES,
@ -678,10 +678,18 @@ class GoogleEntity:
elif area_entry and area_entry.name: elif area_entry and area_entry.name:
device["roomHint"] = area_entry.name device["roomHint"] = area_entry.name
# Add deviceInfo
if not device_entry: if not device_entry:
return device return device
# Add Matter info
if "matter" in self.hass.config.components and (
matter_info := matter.get_matter_device_info(self.hass, device_entry.id)
):
device["matterUniqueId"] = matter_info["unique_id"]
device["matterOriginalVendorId"] = matter_info["vendor_id"]
device["matterOriginalProductId"] = matter_info["product_id"]
# Add deviceInfo
device_info = {} device_info = {}
if device_entry.manufacturer: if device_entry.manufacturer:

View file

@ -1,7 +1,7 @@
{ {
"domain": "google_assistant", "domain": "google_assistant",
"name": "Google Assistant", "name": "Google Assistant",
"after_dependencies": ["camera"], "after_dependencies": ["camera", "matter"],
"codeowners": ["@home-assistant/cloud"], "codeowners": ["@home-assistant/cloud"],
"dependencies": ["http"], "dependencies": ["http"],
"documentation": "https://www.home-assistant.io/integrations/google_assistant", "documentation": "https://www.home-assistant.io/integrations/google_assistant",

View file

@ -14,14 +14,17 @@ from homeassistant.components.google_assistant.const import (
SOURCE_LOCAL, SOURCE_LOCAL,
STORE_GOOGLE_LOCAL_WEBHOOK_ID, STORE_GOOGLE_LOCAL_WEBHOOK_ID,
) )
from homeassistant.components.matter.models import MatterDeviceInfo
from homeassistant.config import async_process_ha_core_config from homeassistant.config import async_process_ha_core_config
from homeassistant.core import HomeAssistant, State from homeassistant.core import HomeAssistant, State
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from . import MockConfig from . import MockConfig
from tests.common import ( from tests.common import (
MockConfigEntry,
async_capture_events, async_capture_events,
async_fire_time_changed, async_fire_time_changed,
async_mock_service, async_mock_service,
@ -73,6 +76,56 @@ async def test_google_entity_sync_serialize_with_local_sdk(hass: HomeAssistant)
assert "customData" not in serialized assert "customData" not in serialized
async def test_google_entity_sync_serialize_with_matter(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test sync serialize attributes of a GoogleEntity that is also a Matter device."""
entry = MockConfigEntry()
entry.add_to_hass(hass)
device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
manufacturer="Someone",
model="Some model",
sw_version="Some Version",
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity = entity_registry.async_get_or_create(
"light",
"test",
"1235",
suggested_object_id="ceiling_lights",
device_id=device.id,
)
hass.states.async_set("light.ceiling_lights", "off")
entity = helpers.GoogleEntity(
hass, MockConfig(hass=hass), hass.states.get("light.ceiling_lights")
)
serialized = entity.sync_serialize(None, "mock-uuid")
assert "matterUniqueId" not in serialized
assert "matterOriginalVendorId" not in serialized
assert "matterOriginalProductId" not in serialized
hass.config.components.add("matter")
with patch(
"homeassistant.components.matter.get_matter_device_info",
return_value=MatterDeviceInfo(
unique_id="mock-unique-id",
vendor_id="mock-vendor-id",
product_id="mock-product-id",
),
):
serialized = entity.sync_serialize("mock-user-id", "abcdef")
assert serialized["matterUniqueId"] == "mock-unique-id"
assert serialized["matterOriginalVendorId"] == "mock-vendor-id"
assert serialized["matterOriginalProductId"] == "mock-product-id"
async def test_config_local_sdk( async def test_config_local_sdk(
hass: HomeAssistant, hass_client: ClientSessionGenerator hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None: ) -> None: