mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Template sensor/binary sensors without trigger now respect section unique id (#49613)
This commit is contained in:
parent
b0fecdcc3d
commit
dcee78b747
@ -6,7 +6,11 @@ import logging
|
|||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
from homeassistant import config as conf_util
|
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.core import CoreState, Event, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import (
|
||||||
@ -84,7 +88,10 @@ async def _process_config(hass, hass_config):
|
|||||||
hass,
|
hass,
|
||||||
platform_domain,
|
platform_domain,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
{"entities": conf_section[platform_domain]},
|
{
|
||||||
|
"unique_id": conf_section.get(CONF_UNIQUE_ID),
|
||||||
|
"entities": conf_section[platform_domain],
|
||||||
|
},
|
||||||
hass_config,
|
hass_config,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -133,7 +133,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@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."""
|
"""Create the template binary sensors."""
|
||||||
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)
|
delay_off_raw = entity_conf.get(CONF_DELAY_OFF)
|
||||||
unique_id = entity_conf.get(CONF_UNIQUE_ID)
|
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(
|
sensors.append(
|
||||||
BinarySensorTemplate(
|
BinarySensorTemplate(
|
||||||
hass,
|
hass,
|
||||||
@ -179,6 +184,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
async_add_entities,
|
async_add_entities,
|
||||||
hass,
|
hass,
|
||||||
rewrite_legacy_to_modern_conf(config[CONF_SENSORS]),
|
rewrite_legacy_to_modern_conf(config[CONF_SENSORS]),
|
||||||
|
None,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -190,7 +196,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
return
|
return
|
||||||
|
|
||||||
_async_create_template_tracking_entities(
|
_async_create_template_tracking_entities(
|
||||||
async_add_entities, hass, discovery_info["entities"]
|
async_add_entities,
|
||||||
|
hass,
|
||||||
|
discovery_info["entities"],
|
||||||
|
discovery_info["unique_id"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -140,7 +140,9 @@ PLATFORM_SCHEMA = vol.All(
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@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."""
|
"""Create the template sensors."""
|
||||||
sensors = []
|
sensors = []
|
||||||
|
|
||||||
@ -158,6 +160,9 @@ def _async_create_template_tracking_entities(async_add_entities, hass, definitio
|
|||||||
attribute_templates = entity_conf.get(CONF_ATTRIBUTES, {})
|
attribute_templates = entity_conf.get(CONF_ATTRIBUTES, {})
|
||||||
unique_id = entity_conf.get(CONF_UNIQUE_ID)
|
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(
|
sensors.append(
|
||||||
SensorTemplate(
|
SensorTemplate(
|
||||||
hass,
|
hass,
|
||||||
@ -184,6 +189,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
async_add_entities,
|
async_add_entities,
|
||||||
hass,
|
hass,
|
||||||
rewrite_legacy_to_modern_conf(config[CONF_SENSORS]),
|
rewrite_legacy_to_modern_conf(config[CONF_SENSORS]),
|
||||||
|
None,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -195,7 +201,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
return
|
return
|
||||||
|
|
||||||
_async_create_template_tracking_entities(
|
_async_create_template_tracking_entities(
|
||||||
async_add_entities, hass, discovery_info["entities"]
|
async_add_entities,
|
||||||
|
hass,
|
||||||
|
discovery_info["entities"],
|
||||||
|
discovery_info["unique_id"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -842,8 +842,16 @@ async def test_unique_id(hass):
|
|||||||
"""Test unique_id option only creates one binary sensor per id."""
|
"""Test unique_id option only creates one binary sensor per id."""
|
||||||
await setup.async_setup_component(
|
await setup.async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
binary_sensor.DOMAIN,
|
"template",
|
||||||
{
|
{
|
||||||
|
"template": {
|
||||||
|
"unique_id": "group-id",
|
||||||
|
"binary_sensor": {
|
||||||
|
"name": "top-level",
|
||||||
|
"unique_id": "sensor-id",
|
||||||
|
"state": "on",
|
||||||
|
},
|
||||||
|
},
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"platform": "template",
|
"platform": "template",
|
||||||
"sensors": {
|
"sensors": {
|
||||||
@ -864,7 +872,21 @@ async def test_unique_id(hass):
|
|||||||
await hass.async_start()
|
await hass.async_start()
|
||||||
await hass.async_block_till_done()
|
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):
|
async def test_template_validation_error(hass, caplog):
|
||||||
|
@ -636,8 +636,12 @@ async def test_unique_id(hass):
|
|||||||
"""Test unique_id option only creates one sensor per id."""
|
"""Test unique_id option only creates one sensor per id."""
|
||||||
await async_setup_component(
|
await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
"template",
|
||||||
{
|
{
|
||||||
|
"template": {
|
||||||
|
"unique_id": "group-id",
|
||||||
|
"sensor": {"name": "top-level", "unique_id": "sensor-id", "state": "5"},
|
||||||
|
},
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"platform": "template",
|
"platform": "template",
|
||||||
"sensors": {
|
"sensors": {
|
||||||
@ -650,7 +654,7 @@ async def test_unique_id(hass):
|
|||||||
"value_template": "{{ false }}",
|
"value_template": "{{ false }}",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -658,7 +662,19 @@ async def test_unique_id(hass):
|
|||||||
await hass.async_start()
|
await hass.async_start()
|
||||||
await hass.async_block_till_done()
|
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):
|
async def test_sun_renders_once_per_sensor(hass):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user