mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Support area on entities for google assistant (#44300)
* Support area on entities * Don't let registry area override config
This commit is contained in:
parent
1ca99c4fa4
commit
56b3e0b52e
@ -17,6 +17,7 @@ from homeassistant.const import (
|
|||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Context, HomeAssistant, State, callback
|
from homeassistant.core import Context, HomeAssistant, State, callback
|
||||||
|
from homeassistant.helpers.area_registry import AreaEntry
|
||||||
from homeassistant.helpers.event import async_call_later
|
from homeassistant.helpers.event import async_call_later
|
||||||
from homeassistant.helpers.network import get_url
|
from homeassistant.helpers.network import get_url
|
||||||
from homeassistant.helpers.storage import Store
|
from homeassistant.helpers.storage import Store
|
||||||
@ -39,6 +40,29 @@ SYNC_DELAY = 15
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def _get_area(hass, entity_id) -> Optional[AreaEntry]:
|
||||||
|
"""Calculate the area for a entity_id."""
|
||||||
|
dev_reg, ent_reg, area_reg = await gather(
|
||||||
|
hass.helpers.device_registry.async_get_registry(),
|
||||||
|
hass.helpers.entity_registry.async_get_registry(),
|
||||||
|
hass.helpers.area_registry.async_get_registry(),
|
||||||
|
)
|
||||||
|
|
||||||
|
entity_entry = ent_reg.async_get(entity_id)
|
||||||
|
if not entity_entry:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if entity_entry.area_id:
|
||||||
|
area_id = entity_entry.area_id
|
||||||
|
else:
|
||||||
|
device_entry = dev_reg.devices.get(entity_entry.device_id)
|
||||||
|
if not (device_entry and device_entry.area_id):
|
||||||
|
return None
|
||||||
|
area_id = device_entry.area_id
|
||||||
|
|
||||||
|
return area_reg.areas.get(area_id)
|
||||||
|
|
||||||
|
|
||||||
class AbstractConfig(ABC):
|
class AbstractConfig(ABC):
|
||||||
"""Hold the configuration for Google Assistant."""
|
"""Hold the configuration for Google Assistant."""
|
||||||
|
|
||||||
@ -450,25 +474,10 @@ class GoogleEntity:
|
|||||||
room = entity_config.get(CONF_ROOM_HINT)
|
room = entity_config.get(CONF_ROOM_HINT)
|
||||||
if room:
|
if room:
|
||||||
device["roomHint"] = room
|
device["roomHint"] = room
|
||||||
return device
|
else:
|
||||||
|
area = await _get_area(self.hass, state.entity_id)
|
||||||
dev_reg, ent_reg, area_reg = await gather(
|
if area and area.name:
|
||||||
self.hass.helpers.device_registry.async_get_registry(),
|
device["roomHint"] = area.name
|
||||||
self.hass.helpers.entity_registry.async_get_registry(),
|
|
||||||
self.hass.helpers.area_registry.async_get_registry(),
|
|
||||||
)
|
|
||||||
|
|
||||||
entity_entry = ent_reg.async_get(state.entity_id)
|
|
||||||
if not (entity_entry and entity_entry.device_id):
|
|
||||||
return device
|
|
||||||
|
|
||||||
device_entry = dev_reg.devices.get(entity_entry.device_id)
|
|
||||||
if not (device_entry and device_entry.area_id):
|
|
||||||
return device
|
|
||||||
|
|
||||||
area_entry = area_reg.areas.get(device_entry.area_id)
|
|
||||||
if area_entry and area_entry.name:
|
|
||||||
device["roomHint"] = area_entry.name
|
|
||||||
|
|
||||||
return device
|
return device
|
||||||
|
|
||||||
|
@ -152,7 +152,8 @@ async def test_sync_message(hass):
|
|||||||
|
|
||||||
|
|
||||||
# pylint: disable=redefined-outer-name
|
# pylint: disable=redefined-outer-name
|
||||||
async def test_sync_in_area(hass, registries):
|
@pytest.mark.parametrize("area_on_device", [True, False])
|
||||||
|
async def test_sync_in_area(area_on_device, hass, registries):
|
||||||
"""Test a sync message where room hint comes from area."""
|
"""Test a sync message where room hint comes from area."""
|
||||||
area = registries.area.async_create("Living Room")
|
area = registries.area.async_create("Living Room")
|
||||||
|
|
||||||
@ -160,10 +161,17 @@ async def test_sync_in_area(hass, registries):
|
|||||||
config_entry_id="1234",
|
config_entry_id="1234",
|
||||||
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||||
)
|
)
|
||||||
registries.device.async_update_device(device.id, area_id=area.id)
|
registries.device.async_update_device(
|
||||||
|
device.id, area_id=area.id if area_on_device else None
|
||||||
|
)
|
||||||
|
|
||||||
entity = registries.entity.async_get_or_create(
|
entity = registries.entity.async_get_or_create(
|
||||||
"light", "test", "1235", suggested_object_id="demo_light", device_id=device.id
|
"light",
|
||||||
|
"test",
|
||||||
|
"1235",
|
||||||
|
suggested_object_id="demo_light",
|
||||||
|
device_id=device.id,
|
||||||
|
area_id=area.id if not area_on_device else None,
|
||||||
)
|
)
|
||||||
|
|
||||||
light = DemoLight(
|
light = DemoLight(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user