From dcee78b7473002a3ec2eb667f3996745455a560e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 24 Apr 2021 07:14:31 -0700 Subject: [PATCH] Template sensor/binary sensors without trigger now respect section unique id (#49613) --- homeassistant/components/template/__init__.py | 11 ++++++-- .../components/template/binary_sensor.py | 13 ++++++++-- homeassistant/components/template/sensor.py | 13 ++++++++-- .../components/template/test_binary_sensor.py | 26 +++++++++++++++++-- tests/components/template/test_sensor.py | 22 +++++++++++++--- 5 files changed, 74 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/template/__init__.py b/homeassistant/components/template/__init__.py index dfacac17a9b..3e34b927971 100644 --- a/homeassistant/components/template/__init__.py +++ b/homeassistant/components/template/__init__.py @@ -6,7 +6,11 @@ import logging from typing import Callable from homeassistant import config as conf_util -from homeassistant.const import EVENT_HOMEASSISTANT_START, SERVICE_RELOAD +from homeassistant.const import ( + CONF_UNIQUE_ID, + EVENT_HOMEASSISTANT_START, + SERVICE_RELOAD, +) from homeassistant.core import CoreState, Event, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import ( @@ -84,7 +88,10 @@ async def _process_config(hass, hass_config): hass, platform_domain, DOMAIN, - {"entities": conf_section[platform_domain]}, + { + "unique_id": conf_section.get(CONF_UNIQUE_ID), + "entities": conf_section[platform_domain], + }, hass_config, ) ) diff --git a/homeassistant/components/template/binary_sensor.py b/homeassistant/components/template/binary_sensor.py index 83c31406c4a..2e1d2f71590 100644 --- a/homeassistant/components/template/binary_sensor.py +++ b/homeassistant/components/template/binary_sensor.py @@ -133,7 +133,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( @callback -def _async_create_template_tracking_entities(async_add_entities, hass, definitions): +def _async_create_template_tracking_entities( + async_add_entities, hass, definitions: list[dict], unique_id_prefix: str | None +): """Create the template binary sensors.""" sensors = [] @@ -152,6 +154,9 @@ def _async_create_template_tracking_entities(async_add_entities, hass, definitio delay_off_raw = entity_conf.get(CONF_DELAY_OFF) unique_id = entity_conf.get(CONF_UNIQUE_ID) + if unique_id and unique_id_prefix: + unique_id = f"{unique_id_prefix}-{unique_id}" + sensors.append( BinarySensorTemplate( hass, @@ -179,6 +184,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= async_add_entities, hass, rewrite_legacy_to_modern_conf(config[CONF_SENSORS]), + None, ) return @@ -190,7 +196,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= return _async_create_template_tracking_entities( - async_add_entities, hass, discovery_info["entities"] + async_add_entities, + hass, + discovery_info["entities"], + discovery_info["unique_id"], ) diff --git a/homeassistant/components/template/sensor.py b/homeassistant/components/template/sensor.py index 224756c2012..d470964465a 100644 --- a/homeassistant/components/template/sensor.py +++ b/homeassistant/components/template/sensor.py @@ -140,7 +140,9 @@ PLATFORM_SCHEMA = vol.All( @callback -def _async_create_template_tracking_entities(async_add_entities, hass, definitions): +def _async_create_template_tracking_entities( + async_add_entities, hass, definitions: list[dict], unique_id_prefix: str | None +): """Create the template sensors.""" sensors = [] @@ -158,6 +160,9 @@ def _async_create_template_tracking_entities(async_add_entities, hass, definitio attribute_templates = entity_conf.get(CONF_ATTRIBUTES, {}) unique_id = entity_conf.get(CONF_UNIQUE_ID) + if unique_id and unique_id_prefix: + unique_id = f"{unique_id_prefix}-{unique_id}" + sensors.append( SensorTemplate( hass, @@ -184,6 +189,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= async_add_entities, hass, rewrite_legacy_to_modern_conf(config[CONF_SENSORS]), + None, ) return @@ -195,7 +201,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= return _async_create_template_tracking_entities( - async_add_entities, hass, discovery_info["entities"] + async_add_entities, + hass, + discovery_info["entities"], + discovery_info["unique_id"], ) diff --git a/tests/components/template/test_binary_sensor.py b/tests/components/template/test_binary_sensor.py index 3c38b184418..70356405867 100644 --- a/tests/components/template/test_binary_sensor.py +++ b/tests/components/template/test_binary_sensor.py @@ -842,8 +842,16 @@ async def test_unique_id(hass): """Test unique_id option only creates one binary sensor per id.""" await setup.async_setup_component( hass, - binary_sensor.DOMAIN, + "template", { + "template": { + "unique_id": "group-id", + "binary_sensor": { + "name": "top-level", + "unique_id": "sensor-id", + "state": "on", + }, + }, "binary_sensor": { "platform": "template", "sensors": { @@ -864,7 +872,21 @@ async def test_unique_id(hass): await hass.async_start() await hass.async_block_till_done() - assert len(hass.states.async_all()) == 1 + assert len(hass.states.async_all()) == 2 + + ent_reg = entity_registry.async_get(hass) + + assert len(ent_reg.entities) == 2 + assert ( + ent_reg.async_get_entity_id("binary_sensor", "template", "group-id-sensor-id") + is not None + ) + assert ( + ent_reg.async_get_entity_id( + "binary_sensor", "template", "not-so-unique-anymore" + ) + is not None + ) async def test_template_validation_error(hass, caplog): diff --git a/tests/components/template/test_sensor.py b/tests/components/template/test_sensor.py index b510a0c75f8..4047a822432 100644 --- a/tests/components/template/test_sensor.py +++ b/tests/components/template/test_sensor.py @@ -636,8 +636,12 @@ async def test_unique_id(hass): """Test unique_id option only creates one sensor per id.""" await async_setup_component( hass, - sensor.DOMAIN, + "template", { + "template": { + "unique_id": "group-id", + "sensor": {"name": "top-level", "unique_id": "sensor-id", "state": "5"}, + }, "sensor": { "platform": "template", "sensors": { @@ -650,7 +654,7 @@ async def test_unique_id(hass): "value_template": "{{ false }}", }, }, - } + }, }, ) @@ -658,7 +662,19 @@ async def test_unique_id(hass): await hass.async_start() await hass.async_block_till_done() - assert len(hass.states.async_all()) == 1 + assert len(hass.states.async_all()) == 2 + + ent_reg = entity_registry.async_get(hass) + + assert len(ent_reg.entities) == 2 + assert ( + ent_reg.async_get_entity_id("sensor", "template", "group-id-sensor-id") + is not None + ) + assert ( + ent_reg.async_get_entity_id("sensor", "template", "not-so-unique-anymore") + is not None + ) async def test_sun_renders_once_per_sensor(hass):