Skip to content

Commit

Permalink
Add support for entity aliases to Google Assistant (home-assistant#84405
Browse files Browse the repository at this point in the history
)
  • Loading branch information
frenck authored Dec 21, 2022
1 parent 0d87489 commit 1f0ea73
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
20 changes: 15 additions & 5 deletions homeassistant/components/google_assistant/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@
@callback
def _get_registry_entries(
hass: HomeAssistant, entity_id: str
) -> tuple[device_registry.DeviceEntry | None, area_registry.AreaEntry | None]:
) -> tuple[
entity_registry.RegistryEntry | None,
device_registry.DeviceEntry | None,
area_registry.AreaEntry | None,
]:
"""Get registry entries."""
ent_reg = entity_registry.async_get(hass)
dev_reg = device_registry.async_get(hass)
Expand All @@ -75,7 +79,7 @@ def _get_registry_entries(
else:
area_entry = None

return device_entry, area_entry
return entity_entry, device_entry, area_entry


class AbstractConfig(ABC):
Expand Down Expand Up @@ -588,7 +592,9 @@ def sync_serialize(self, agent_user_id, instance_uuid):
name = (entity_config.get(CONF_NAME) or state.name).strip()

# Find entity/device/area registry entries
device_entry, area_entry = _get_registry_entries(self.hass, self.entity_id)
entity_entry, device_entry, area_entry = _get_registry_entries(
self.hass, self.entity_id
)

# Build the device info
device = {
Expand All @@ -603,8 +609,12 @@ def sync_serialize(self, agent_user_id, instance_uuid):
}

# Add aliases
if aliases := entity_config.get(CONF_ALIASES):
device["name"]["nicknames"] = [name] + aliases
if (config_aliases := entity_config.get(CONF_ALIASES, [])) or (
entity_entry and entity_entry.aliases
):
device["name"]["nicknames"] = [name] + config_aliases
if entity_entry:
device["name"]["nicknames"].extend(entity_entry.aliases)

# Add local SDK info if enabled
if self.config.is_local_sdk_active and self.should_expose_local():
Expand Down
31 changes: 27 additions & 4 deletions tests/components/google_assistant/test_smart_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import ANY, call, patch

import pytest
from pytest_unordered import unordered

from homeassistant.components import camera
from homeassistant.components.climate import ATTR_MAX_TEMP, ATTR_MIN_TEMP, HVACMode
Expand Down Expand Up @@ -101,10 +102,21 @@ async def test_async_handle_message(hass):
await hass.async_block_till_done()


async def test_sync_message(hass):
async def test_sync_message(hass, registries):
"""Test a sync message."""
entity = registries.entity.async_get_or_create(
"light",
"test",
"unique-demo-light",
suggested_object_id="demo_light",
)
registries.entity.async_update_entity(
entity.entity_id,
aliases={"Stay", "Healthy"},
)

light = DemoLight(
None,
"unique-demo-light",
"Demo Light",
state=False,
hs_color=(180, 75),
Expand Down Expand Up @@ -150,7 +162,15 @@ async def test_sync_message(hass):
"id": "light.demo_light",
"name": {
"name": "Demo Light",
"nicknames": ["Demo Light", "Hello", "World"],
"nicknames": unordered(
[
"Demo Light",
"Hello",
"World",
"Stay",
"Healthy",
]
),
},
"traits": [
trait.TRAIT_BRIGHTNESS,
Expand Down Expand Up @@ -181,7 +201,10 @@ async def test_sync_message(hass):
{
"setting_name": "none",
"setting_values": [
{"lang": "en", "setting_synonym": ["none"]}
{
"lang": "en",
"setting_synonym": ["none"],
}
],
},
],
Expand Down

0 comments on commit 1f0ea73

Please sign in to comment.