1
0
mirror of https://github.com/home-assistant/core.git synced 2025-05-03 21:49:17 +00:00
chammp 8ff4d5dcbf
Adapt template sensors to use the same plural trigger/condition/action definitions as automations ()
* Add plurals to template entities

* Ruff

* Ruffy ruff

* Fix linters

* Fix bug introduced after merging dev

* Fix merge mistake

* Revert adding automation helper

* Revert "Fix bug introduced after merging dev"

This reverts commit 098d478f150a06546fb9ec3668865fa5d763c6b2.

* Fix blueprint validation

* Apply suggestions from code review

---------

Co-authored-by: Erik <erik@montnemery.com>
2025-04-29 11:52:58 +02:00

68 lines
2.2 KiB
Python

"""Helpers for template integration."""
import logging
from homeassistant.components import blueprint
from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import async_get_platforms
from homeassistant.helpers.singleton import singleton
from .const import DOMAIN
from .entity import AbstractTemplateEntity
DATA_BLUEPRINTS = "template_blueprints"
LOGGER = logging.getLogger(__name__)
@callback
def templates_with_blueprint(hass: HomeAssistant, blueprint_path: str) -> list[str]:
"""Return all template entity ids that reference the blueprint."""
return [
entity_id
for platform in async_get_platforms(hass, DOMAIN)
for entity_id, template_entity in platform.entities.items()
if isinstance(template_entity, AbstractTemplateEntity)
and template_entity.referenced_blueprint == blueprint_path
]
@callback
def blueprint_in_template(hass: HomeAssistant, entity_id: str) -> str | None:
"""Return the blueprint the template entity is based on or None."""
for platform in async_get_platforms(hass, DOMAIN):
if isinstance(
(template_entity := platform.entities.get(entity_id)),
AbstractTemplateEntity,
):
return template_entity.referenced_blueprint
return None
def _blueprint_in_use(hass: HomeAssistant, blueprint_path: str) -> bool:
"""Return True if any template references the blueprint."""
return len(templates_with_blueprint(hass, blueprint_path)) > 0
async def _reload_blueprint_templates(hass: HomeAssistant, blueprint_path: str) -> None:
"""Reload all templates that rely on a specific blueprint."""
await hass.services.async_call(DOMAIN, SERVICE_RELOAD)
@singleton(DATA_BLUEPRINTS)
@callback
def async_get_blueprints(hass: HomeAssistant) -> blueprint.DomainBlueprints:
"""Get template blueprints."""
# pylint: disable-next=import-outside-toplevel
from .config import TEMPLATE_BLUEPRINT_SCHEMA
return blueprint.DomainBlueprints(
hass,
DOMAIN,
LOGGER,
_blueprint_in_use,
_reload_blueprint_templates,
TEMPLATE_BLUEPRINT_SCHEMA,
)