mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Update template cover to modern style config (#141878)
This commit is contained in:
parent
59af3a396c
commit
4a1905a2a2
@ -12,6 +12,7 @@ from homeassistant.components.blueprint import (
|
|||||||
is_blueprint_instance_config,
|
is_blueprint_instance_config,
|
||||||
)
|
)
|
||||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
|
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
|
||||||
|
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
||||||
from homeassistant.components.image import DOMAIN as IMAGE_DOMAIN
|
from homeassistant.components.image import DOMAIN as IMAGE_DOMAIN
|
||||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||||
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
|
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
|
||||||
@ -37,6 +38,7 @@ from homeassistant.setup import async_notify_setup_error
|
|||||||
from . import (
|
from . import (
|
||||||
binary_sensor as binary_sensor_platform,
|
binary_sensor as binary_sensor_platform,
|
||||||
button as button_platform,
|
button as button_platform,
|
||||||
|
cover as cover_platform,
|
||||||
image as image_platform,
|
image as image_platform,
|
||||||
light as light_platform,
|
light as light_platform,
|
||||||
number as number_platform,
|
number as number_platform,
|
||||||
@ -117,9 +119,12 @@ CONFIG_SECTION_SCHEMA = vol.Schema(
|
|||||||
vol.Optional(SWITCH_DOMAIN): vol.All(
|
vol.Optional(SWITCH_DOMAIN): vol.All(
|
||||||
cv.ensure_list, [switch_platform.SWITCH_SCHEMA]
|
cv.ensure_list, [switch_platform.SWITCH_SCHEMA]
|
||||||
),
|
),
|
||||||
|
vol.Optional(COVER_DOMAIN): vol.All(
|
||||||
|
cv.ensure_list, [cover_platform.COVER_SCHEMA]
|
||||||
|
),
|
||||||
},
|
},
|
||||||
ensure_domains_do_not_have_trigger_or_action(
|
ensure_domains_do_not_have_trigger_or_action(
|
||||||
BUTTON_DOMAIN, LIGHT_DOMAIN, SWITCH_DOMAIN
|
BUTTON_DOMAIN, COVER_DOMAIN, LIGHT_DOMAIN, SWITCH_DOMAIN
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -21,20 +21,25 @@ from homeassistant.const import (
|
|||||||
CONF_DEVICE_CLASS,
|
CONF_DEVICE_CLASS,
|
||||||
CONF_ENTITY_ID,
|
CONF_ENTITY_ID,
|
||||||
CONF_FRIENDLY_NAME,
|
CONF_FRIENDLY_NAME,
|
||||||
|
CONF_NAME,
|
||||||
CONF_OPTIMISTIC,
|
CONF_OPTIMISTIC,
|
||||||
|
CONF_STATE,
|
||||||
CONF_UNIQUE_ID,
|
CONF_UNIQUE_ID,
|
||||||
CONF_VALUE_TEMPLATE,
|
CONF_VALUE_TEMPLATE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv, template
|
||||||
from homeassistant.helpers.entity import async_generate_entity_id
|
from homeassistant.helpers.entity import async_generate_entity_id
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import CONF_OBJECT_ID, CONF_PICTURE, DOMAIN
|
||||||
from .template_entity import (
|
from .template_entity import (
|
||||||
|
LEGACY_FIELDS as TEMPLATE_ENTITY_LEGACY_FIELDS,
|
||||||
|
TEMPLATE_ENTITY_AVAILABILITY_SCHEMA,
|
||||||
TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY,
|
TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY,
|
||||||
|
TEMPLATE_ENTITY_ICON_SCHEMA,
|
||||||
TemplateEntity,
|
TemplateEntity,
|
||||||
rewrite_common_legacy_to_modern_conf,
|
rewrite_common_legacy_to_modern_conf,
|
||||||
)
|
)
|
||||||
@ -56,7 +61,9 @@ _VALID_STATES = [
|
|||||||
"none",
|
"none",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
CONF_POSITION = "position"
|
||||||
CONF_POSITION_TEMPLATE = "position_template"
|
CONF_POSITION_TEMPLATE = "position_template"
|
||||||
|
CONF_TILT = "tilt"
|
||||||
CONF_TILT_TEMPLATE = "tilt_template"
|
CONF_TILT_TEMPLATE = "tilt_template"
|
||||||
OPEN_ACTION = "open_cover"
|
OPEN_ACTION = "open_cover"
|
||||||
CLOSE_ACTION = "close_cover"
|
CLOSE_ACTION = "close_cover"
|
||||||
@ -74,7 +81,39 @@ TILT_FEATURES = (
|
|||||||
| CoverEntityFeature.SET_TILT_POSITION
|
| CoverEntityFeature.SET_TILT_POSITION
|
||||||
)
|
)
|
||||||
|
|
||||||
|
LEGACY_FIELDS = TEMPLATE_ENTITY_LEGACY_FIELDS | {
|
||||||
|
CONF_VALUE_TEMPLATE: CONF_STATE,
|
||||||
|
CONF_POSITION_TEMPLATE: CONF_POSITION,
|
||||||
|
CONF_TILT_TEMPLATE: CONF_TILT,
|
||||||
|
}
|
||||||
|
|
||||||
|
DEFAULT_NAME = "Template Cover"
|
||||||
|
|
||||||
COVER_SCHEMA = vol.All(
|
COVER_SCHEMA = vol.All(
|
||||||
|
vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Inclusive(CLOSE_ACTION, CONF_OPEN_AND_CLOSE): cv.SCRIPT_SCHEMA,
|
||||||
|
vol.Inclusive(OPEN_ACTION, CONF_OPEN_AND_CLOSE): cv.SCRIPT_SCHEMA,
|
||||||
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
||||||
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.template,
|
||||||
|
vol.Optional(CONF_OPTIMISTIC): cv.boolean,
|
||||||
|
vol.Optional(CONF_PICTURE): cv.template,
|
||||||
|
vol.Optional(CONF_POSITION): cv.template,
|
||||||
|
vol.Optional(CONF_STATE): cv.template,
|
||||||
|
vol.Optional(CONF_TILT_OPTIMISTIC): cv.boolean,
|
||||||
|
vol.Optional(CONF_TILT): cv.template,
|
||||||
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||||
|
vol.Optional(POSITION_ACTION): cv.SCRIPT_SCHEMA,
|
||||||
|
vol.Optional(STOP_ACTION): cv.SCRIPT_SCHEMA,
|
||||||
|
vol.Optional(TILT_ACTION): cv.SCRIPT_SCHEMA,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA.schema)
|
||||||
|
.extend(TEMPLATE_ENTITY_ICON_SCHEMA.schema),
|
||||||
|
cv.has_at_least_one_key(OPEN_ACTION, POSITION_ACTION),
|
||||||
|
)
|
||||||
|
|
||||||
|
LEGACY_COVER_SCHEMA = vol.All(
|
||||||
cv.deprecated(CONF_ENTITY_ID),
|
cv.deprecated(CONF_ENTITY_ID),
|
||||||
vol.Schema(
|
vol.Schema(
|
||||||
{
|
{
|
||||||
@ -98,29 +137,56 @@ COVER_SCHEMA = vol.All(
|
|||||||
)
|
)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = COVER_PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = COVER_PLATFORM_SCHEMA.extend(
|
||||||
{vol.Required(CONF_COVERS): cv.schema_with_slug_keys(COVER_SCHEMA)}
|
{vol.Required(CONF_COVERS): cv.schema_with_slug_keys(LEGACY_COVER_SCHEMA)}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _async_create_entities(hass: HomeAssistant, config):
|
def rewrite_legacy_to_modern_conf(
|
||||||
"""Create the Template cover."""
|
hass: HomeAssistant, config: dict[str, dict]
|
||||||
|
) -> list[dict]:
|
||||||
|
"""Rewrite legacy switch configuration definitions to modern ones."""
|
||||||
covers = []
|
covers = []
|
||||||
|
|
||||||
for object_id, entity_config in config[CONF_COVERS].items():
|
for object_id, entity_conf in config.items():
|
||||||
entity_config = rewrite_common_legacy_to_modern_conf(hass, entity_config)
|
entity_conf = {**entity_conf, CONF_OBJECT_ID: object_id}
|
||||||
|
|
||||||
unique_id = entity_config.get(CONF_UNIQUE_ID)
|
entity_conf = rewrite_common_legacy_to_modern_conf(
|
||||||
|
hass, entity_conf, LEGACY_FIELDS
|
||||||
|
)
|
||||||
|
|
||||||
|
if CONF_NAME not in entity_conf:
|
||||||
|
entity_conf[CONF_NAME] = template.Template(object_id, hass)
|
||||||
|
|
||||||
|
covers.append(entity_conf)
|
||||||
|
|
||||||
|
return covers
|
||||||
|
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _async_create_template_tracking_entities(
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
definitions: list[dict],
|
||||||
|
unique_id_prefix: str | None,
|
||||||
|
) -> None:
|
||||||
|
"""Create the template switches."""
|
||||||
|
covers = []
|
||||||
|
|
||||||
|
for entity_conf in definitions:
|
||||||
|
unique_id = entity_conf.get(CONF_UNIQUE_ID)
|
||||||
|
|
||||||
|
if unique_id and unique_id_prefix:
|
||||||
|
unique_id = f"{unique_id_prefix}-{unique_id}"
|
||||||
|
|
||||||
covers.append(
|
covers.append(
|
||||||
CoverTemplate(
|
CoverTemplate(
|
||||||
hass,
|
hass,
|
||||||
object_id,
|
entity_conf,
|
||||||
entity_config,
|
|
||||||
unique_id,
|
unique_id,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return covers
|
async_add_entities(covers)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
@ -130,7 +196,21 @@ async def async_setup_platform(
|
|||||||
discovery_info: DiscoveryInfoType | None = None,
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Template cover."""
|
"""Set up the Template cover."""
|
||||||
async_add_entities(await _async_create_entities(hass, config))
|
if discovery_info is None:
|
||||||
|
_async_create_template_tracking_entities(
|
||||||
|
async_add_entities,
|
||||||
|
hass,
|
||||||
|
rewrite_legacy_to_modern_conf(hass, config[CONF_COVERS]),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
_async_create_template_tracking_entities(
|
||||||
|
async_add_entities,
|
||||||
|
hass,
|
||||||
|
discovery_info["entities"],
|
||||||
|
discovery_info["unique_id"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class CoverTemplate(TemplateEntity, CoverEntity):
|
class CoverTemplate(TemplateEntity, CoverEntity):
|
||||||
@ -141,23 +221,22 @@ class CoverTemplate(TemplateEntity, CoverEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
object_id,
|
|
||||||
config: dict[str, Any],
|
config: dict[str, Any],
|
||||||
unique_id,
|
unique_id,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Template cover."""
|
"""Initialize the Template cover."""
|
||||||
super().__init__(
|
super().__init__(hass, config=config, fallback_name=None, unique_id=unique_id)
|
||||||
hass, config=config, fallback_name=object_id, unique_id=unique_id
|
if (object_id := config.get(CONF_OBJECT_ID)) is not None:
|
||||||
)
|
|
||||||
self.entity_id = async_generate_entity_id(
|
self.entity_id = async_generate_entity_id(
|
||||||
ENTITY_ID_FORMAT, object_id, hass=hass
|
ENTITY_ID_FORMAT, object_id, hass=hass
|
||||||
)
|
)
|
||||||
name = self._attr_name
|
name = self._attr_name
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
assert name is not None
|
assert name is not None
|
||||||
self._template = config.get(CONF_VALUE_TEMPLATE)
|
self._template = config.get(CONF_STATE)
|
||||||
self._position_template = config.get(CONF_POSITION_TEMPLATE)
|
|
||||||
self._tilt_template = config.get(CONF_TILT_TEMPLATE)
|
self._position_template = config.get(CONF_POSITION)
|
||||||
|
self._tilt_template = config.get(CONF_TILT)
|
||||||
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
||||||
|
|
||||||
# The config requires (open and close scripts) or a set position script,
|
# The config requires (open and close scripts) or a set position script,
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user