2020-08-20 13:06:41 +00:00
|
|
|
"""TemplateEntity utility class."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
2020-08-20 13:06:41 +00:00
|
|
|
|
2021-12-02 18:26:10 +00:00
|
|
|
import itertools
|
2021-09-29 14:19:06 +00:00
|
|
|
from typing import Any
|
2020-08-20 13:06:41 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-12-02 18:26:10 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_ENTITY_PICTURE_TEMPLATE,
|
2022-01-24 14:44:05 +00:00
|
|
|
CONF_FRIENDLY_NAME,
|
2021-12-02 18:26:10 +00:00
|
|
|
CONF_ICON,
|
|
|
|
CONF_ICON_TEMPLATE,
|
2022-01-24 14:44:05 +00:00
|
|
|
CONF_NAME,
|
2021-12-02 18:26:10 +00:00
|
|
|
)
|
2020-08-20 13:51:27 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-06-08 13:55:49 +00:00
|
|
|
from homeassistant.helpers.template import Template
|
|
|
|
from homeassistant.helpers.template_entity import ( # noqa: F401 pylint: disable=unused-import
|
|
|
|
TEMPLATE_ENTITY_BASE_SCHEMA,
|
|
|
|
TemplateEntity,
|
2022-04-20 13:30:17 +00:00
|
|
|
)
|
2020-08-20 13:06:41 +00:00
|
|
|
|
2021-12-02 18:26:10 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_ATTRIBUTE_TEMPLATES,
|
|
|
|
CONF_ATTRIBUTES,
|
|
|
|
CONF_AVAILABILITY,
|
|
|
|
CONF_AVAILABILITY_TEMPLATE,
|
|
|
|
CONF_PICTURE,
|
|
|
|
)
|
2021-11-10 06:58:22 +00:00
|
|
|
|
2021-12-16 15:22:17 +00:00
|
|
|
TEMPLATE_ENTITY_AVAILABILITY_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_AVAILABILITY): cv.template,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
TEMPLATE_ENTITY_ICON_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ICON): cv.template,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-11-10 06:58:22 +00:00
|
|
|
TEMPLATE_ENTITY_COMMON_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ATTRIBUTES): vol.Schema({cv.string: cv.template}),
|
|
|
|
vol.Optional(CONF_AVAILABILITY): cv.template,
|
|
|
|
}
|
2022-06-08 13:55:49 +00:00
|
|
|
).extend(TEMPLATE_ENTITY_BASE_SCHEMA.schema)
|
2021-11-10 06:58:22 +00:00
|
|
|
|
2021-12-16 19:53:19 +00:00
|
|
|
TEMPLATE_ENTITY_ATTRIBUTES_SCHEMA_LEGACY = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ATTRIBUTE_TEMPLATES, default={}): vol.Schema(
|
|
|
|
{cv.string: cv.template}
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-12-02 18:26:10 +00:00
|
|
|
TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
|
|
|
|
}
|
|
|
|
).extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY.schema)
|
|
|
|
|
|
|
|
|
|
|
|
LEGACY_FIELDS = {
|
|
|
|
CONF_ICON_TEMPLATE: CONF_ICON,
|
|
|
|
CONF_ENTITY_PICTURE_TEMPLATE: CONF_PICTURE,
|
|
|
|
CONF_AVAILABILITY_TEMPLATE: CONF_AVAILABILITY,
|
|
|
|
CONF_ATTRIBUTE_TEMPLATES: CONF_ATTRIBUTES,
|
2022-01-24 14:44:05 +00:00
|
|
|
CONF_FRIENDLY_NAME: CONF_NAME,
|
2021-12-02 18:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def rewrite_common_legacy_to_modern_conf(
|
|
|
|
entity_cfg: dict[str, Any], extra_legacy_fields: dict[str, str] = None
|
2022-02-10 09:59:54 +00:00
|
|
|
) -> dict[str, Any]:
|
2021-12-02 18:26:10 +00:00
|
|
|
"""Rewrite legacy config."""
|
|
|
|
entity_cfg = {**entity_cfg}
|
|
|
|
if extra_legacy_fields is None:
|
|
|
|
extra_legacy_fields = {}
|
|
|
|
|
|
|
|
for from_key, to_key in itertools.chain(
|
|
|
|
LEGACY_FIELDS.items(), extra_legacy_fields.items()
|
|
|
|
):
|
|
|
|
if from_key not in entity_cfg or to_key in entity_cfg:
|
|
|
|
continue
|
|
|
|
|
|
|
|
val = entity_cfg.pop(from_key)
|
|
|
|
if isinstance(val, str):
|
|
|
|
val = Template(val)
|
|
|
|
entity_cfg[to_key] = val
|
|
|
|
|
2022-01-24 14:44:05 +00:00
|
|
|
if CONF_NAME in entity_cfg and isinstance(entity_cfg[CONF_NAME], str):
|
|
|
|
entity_cfg[CONF_NAME] = Template(entity_cfg[CONF_NAME])
|
|
|
|
|
2021-12-02 18:26:10 +00:00
|
|
|
return entity_cfg
|