Add the ability to bind the template helper entity to a device (#117753)

This commit is contained in:
dougiteixeira 2024-06-22 07:45:06 -03:00 committed by GitHub
parent f258034f9c
commit 6e32a96ff3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 472 additions and 9 deletions

View file

@ -21,7 +21,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
from homeassistant.core import Context, CoreState, HomeAssistant, State, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.helpers.template import Template
from homeassistant.setup import ATTR_COMPONENT, async_setup_component
@ -1896,3 +1896,40 @@ async def test_trigger_action(
assert len(events) == 1
assert events[0].context.parent_id == context.id
async def test_device_id(hass: HomeAssistant) -> None:
"""Test for device for Template."""
device_registry = dr.async_get(hass)
device_config_entry = MockConfigEntry()
device_config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=device_config_entry.entry_id,
identifiers={("sensor", "identifier_test")},
connections={("mac", "30:31:32:33:34:35")},
)
await hass.async_block_till_done()
assert device_entry is not None
assert device_entry.id is not None
template_config_entry = MockConfigEntry(
data={},
domain=template.DOMAIN,
options={
"name": "My template",
"state": "{{10}}",
"template_type": "sensor",
"device_id": device_entry.id,
},
title="My template",
)
template_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(template_config_entry.entry_id)
await hass.async_block_till_done()
entity_registry = er.async_get(hass)
template_entity = entity_registry.async_get("sensor.my_template")
assert template_entity is not None
assert template_entity.device_id == device_entry.id