From 43a1a679f9784170c4174052d8e463cff48a5818 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Wed, 27 Aug 2025 04:49:38 -0400 Subject: [PATCH] Add object_id to modern template syntax (#150489) Co-authored-by: Martin Hjelmare --- homeassistant/components/template/const.py | 1 + tests/components/template/test_template_entity.py | 10 ++++++++++ tests/components/template/test_trigger_entity.py | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/homeassistant/components/template/const.py b/homeassistant/components/template/const.py index 5ff2c0137ac..23b3608d5e0 100644 --- a/homeassistant/components/template/const.py +++ b/homeassistant/components/template/const.py @@ -26,6 +26,7 @@ TEMPLATE_ENTITY_BASE_SCHEMA = vol.Schema( vol.Optional(CONF_NAME): cv.template, vol.Optional(CONF_PICTURE): cv.template, vol.Optional(CONF_UNIQUE_ID): cv.string, + vol.Optional(CONF_OBJECT_ID): cv.string, } ) diff --git a/tests/components/template/test_template_entity.py b/tests/components/template/test_template_entity.py index 7fe3870ae1e..f9dd18a4866 100644 --- a/tests/components/template/test_template_entity.py +++ b/tests/components/template/test_template_entity.py @@ -18,3 +18,13 @@ async def test_template_entity_requires_hass_set(hass: HomeAssistant) -> None: entity.add_template_attribute("_hello", tpl_with_hass) assert len(entity._template_attrs.get(tpl_with_hass, [])) == 1 + + +async def test_object_id(hass: HomeAssistant) -> None: + """Test template entity creates suggested entity_id from the object_id.""" + + class TemplateTest(template_entity.TemplateEntity): + _entity_id_format = "test.{}" + + entity = TemplateTest(hass, {"object_id": "test"}, "a") + assert entity.entity_id == "test.test" diff --git a/tests/components/template/test_trigger_entity.py b/tests/components/template/test_trigger_entity.py index 65db69fa2b9..000206c0788 100644 --- a/tests/components/template/test_trigger_entity.py +++ b/tests/components/template/test_trigger_entity.py @@ -17,6 +17,7 @@ class TestEntity(trigger_entity.TriggerEntity): """Test entity class.""" __test__ = False + _entity_id_format = "test.{}" extra_template_keys = (CONF_STATE,) @property @@ -134,3 +135,10 @@ async def test_script_variables_from_coordinator(hass: HomeAssistant) -> None: coordinator._execute_update({"value": STATE_ON}) assert entity._render_script_variables() == {"value": STATE_ON} + + +async def test_object_id(hass: HomeAssistant) -> None: + """Test template entity creates suggested entity_id from the object_id.""" + coordinator = TriggerUpdateCoordinator(hass, {}) + entity = TestEntity(hass, coordinator, {"object_id": "test"}) + assert entity.entity_id == "test.test"