Template sensor/binary sensors without trigger now respect section unique id (#49613)

This commit is contained in:
Paulus Schoutsen 2021-04-24 07:14:31 -07:00 committed by GitHub
parent b0fecdcc3d
commit dcee78b747
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 11 deletions

View File

@ -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,
)
)

View File

@ -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"],
)

View File

@ -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"],
)

View File

@ -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):

View File

@ -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):