mirror of
https://github.com/esphome/esphome.git
synced 2026-09-07 21:46:09 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7aad659691 | ||
|
|
c54f05869f |
@@ -2,7 +2,13 @@ from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import binary_sensor
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_CONDITION, CONF_ID, CONF_LAMBDA, CONF_STATE
|
||||
from esphome.const import (
|
||||
CONF_CONDITION,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_STATE,
|
||||
)
|
||||
from esphome.cpp_generator import LambdaExpression
|
||||
|
||||
from .. import template_ns
|
||||
@@ -12,7 +18,11 @@ TemplateBinarySensor = template_ns.class_(
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
binary_sensor.binary_sensor_schema(TemplateBinarySensor)
|
||||
cv.with_visibility(
|
||||
binary_sensor.binary_sensor_schema(TemplateBinarySensor),
|
||||
cv.Visibility.UI,
|
||||
CONF_DEVICE_CLASS,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.Exclusive(CONF_LAMBDA, CONF_CONDITION): cv.returning_lambda,
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
from esphome.components import button
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_DEVICE_CLASS
|
||||
|
||||
from .. import template_ns
|
||||
|
||||
TemplateButton = template_ns.class_("TemplateButton", button.Button)
|
||||
|
||||
CONFIG_SCHEMA = button.button_schema(TemplateButton)
|
||||
CONFIG_SCHEMA = cv.with_visibility(
|
||||
button.button_schema(TemplateButton), cv.Visibility.UI, CONF_DEVICE_CLASS
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
|
||||
@@ -6,6 +6,7 @@ from esphome.const import (
|
||||
CONF_ASSUMED_STATE,
|
||||
CONF_CLOSE_ACTION,
|
||||
CONF_CURRENT_OPERATION,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_OPEN_ACTION,
|
||||
@@ -38,7 +39,11 @@ CONF_HAS_POSITION = "has_position"
|
||||
CONF_TOGGLE_ACTION = "toggle_action"
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cover.cover_schema(TemplateCover)
|
||||
cv.with_visibility(
|
||||
cover.cover_schema(TemplateCover),
|
||||
cv.Visibility.UI,
|
||||
CONF_DEVICE_CLASS,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import event
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_EVENT_TYPES
|
||||
from esphome.const import CONF_DEVICE_CLASS, CONF_EVENT_TYPES
|
||||
|
||||
from .. import template_ns
|
||||
|
||||
@@ -9,7 +9,9 @@ CODEOWNERS = ["@nohat"]
|
||||
|
||||
TemplateEvent = template_ns.class_("TemplateEvent", event.Event, cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = event.event_schema(TemplateEvent).extend(
|
||||
CONFIG_SCHEMA = cv.with_visibility(
|
||||
event.event_schema(TemplateEvent), cv.Visibility.UI, CONF_DEVICE_CLASS
|
||||
).extend(
|
||||
{
|
||||
cv.Required(CONF_EVENT_TYPES): cv.ensure_list(cv.string_strict),
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import esphome.codegen as cg
|
||||
from esphome.components import number
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_ID,
|
||||
CONF_INITIAL_VALUE,
|
||||
CONF_LAMBDA,
|
||||
@@ -12,6 +13,7 @@ from esphome.const import (
|
||||
CONF_RESTORE_VALUE,
|
||||
CONF_SET_ACTION,
|
||||
CONF_STEP,
|
||||
CONF_UNIT_OF_MEASUREMENT,
|
||||
)
|
||||
|
||||
from .. import template_ns
|
||||
@@ -46,7 +48,12 @@ def validate(config):
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
number.number_schema(TemplateNumber)
|
||||
cv.with_visibility(
|
||||
number.number_schema(TemplateNumber),
|
||||
cv.Visibility.UI,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_UNIT_OF_MEASUREMENT,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.Required(CONF_MAX_VALUE): cv.float_,
|
||||
|
||||
@@ -2,7 +2,16 @@ from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import sensor
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_LAMBDA, CONF_STATE
|
||||
from esphome.const import (
|
||||
CONF_ACCURACY_DECIMALS,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_FORCE_UPDATE,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_STATE,
|
||||
CONF_STATE_CLASS,
|
||||
CONF_UNIT_OF_MEASUREMENT,
|
||||
)
|
||||
|
||||
from .. import template_ns
|
||||
|
||||
@@ -11,9 +20,14 @@ TemplateSensor = template_ns.class_(
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
sensor.sensor_schema(
|
||||
TemplateSensor,
|
||||
accuracy_decimals=1,
|
||||
cv.with_visibility(
|
||||
sensor.sensor_schema(TemplateSensor, accuracy_decimals=1),
|
||||
cv.Visibility.UI,
|
||||
CONF_UNIT_OF_MEASUREMENT,
|
||||
CONF_ACCURACY_DECIMALS,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_STATE_CLASS,
|
||||
CONF_FORCE_UPDATE,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ from esphome.components import switch
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ASSUMED_STATE,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_OPTIMISTIC,
|
||||
@@ -31,7 +32,11 @@ def validate(config):
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
switch.switch_schema(TemplateSwitch)
|
||||
cv.with_visibility(
|
||||
switch.switch_schema(TemplateSwitch),
|
||||
cv.Visibility.UI,
|
||||
CONF_DEVICE_CLASS,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
|
||||
@@ -3,7 +3,7 @@ import esphome.codegen as cg
|
||||
from esphome.components import text_sensor
|
||||
from esphome.components.text_sensor import TextSensorPublishAction
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_LAMBDA, CONF_STATE
|
||||
from esphome.const import CONF_DEVICE_CLASS, CONF_ID, CONF_LAMBDA, CONF_STATE
|
||||
|
||||
from .. import template_ns
|
||||
|
||||
@@ -12,7 +12,11 @@ TemplateTextSensor = template_ns.class_(
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
text_sensor.text_sensor_schema()
|
||||
cv.with_visibility(
|
||||
text_sensor.text_sensor_schema(),
|
||||
cv.Visibility.UI,
|
||||
CONF_DEVICE_CLASS,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(TemplateTextSensor),
|
||||
|
||||
@@ -6,6 +6,7 @@ from esphome.const import (
|
||||
CONF_ASSUMED_STATE,
|
||||
CONF_CLOSE_ACTION,
|
||||
CONF_CURRENT_OPERATION,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_OPEN_ACTION,
|
||||
@@ -36,7 +37,11 @@ CONF_HAS_POSITION = "has_position"
|
||||
CONF_TOGGLE_ACTION = "toggle_action"
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
valve.valve_schema(TemplateValve)
|
||||
cv.with_visibility(
|
||||
valve.valve_schema(TemplateValve),
|
||||
cv.Visibility.UI,
|
||||
CONF_DEVICE_CLASS,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
|
||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from contextlib import contextmanager, suppress
|
||||
import copy
|
||||
from datetime import datetime
|
||||
from ipaddress import (
|
||||
AddressValueError,
|
||||
@@ -418,6 +419,37 @@ class Required(vol.Required):
|
||||
self.visibility: Visibility | None = visibility
|
||||
|
||||
|
||||
def with_visibility(schema: Schema, visibility: Visibility, *keys: str) -> Schema:
|
||||
"""Return a copy of ``schema`` with the given ``keys`` re-marked at ``visibility``.
|
||||
|
||||
Lets a platform override the editor :class:`Visibility` of fields it
|
||||
inherits from a shared schema builder — without that builder needing a
|
||||
visibility parameter of its own. The canonical use is a ``template``
|
||||
platform promoting the value metadata its user is expected to define
|
||||
(``device_class``, ``unit_of_measurement``, …) onto the main form:
|
||||
|
||||
CONFIG_SCHEMA = cv.with_visibility(
|
||||
sensor.sensor_schema(TemplateSensor),
|
||||
cv.Visibility.UI,
|
||||
CONF_DEVICE_CLASS, CONF_UNIT_OF_MEASUREMENT,
|
||||
)
|
||||
|
||||
The original marker's key, default and validator are preserved; only the
|
||||
visibility changes, and the input ``schema`` is left untouched. Raises if
|
||||
a requested key is not present so typos fail at schema-build time.
|
||||
"""
|
||||
wanted = {str(k) for k in keys}
|
||||
overrides = {}
|
||||
for marker, validator in schema.schema.items():
|
||||
if str(marker) in wanted:
|
||||
marker = copy.copy(marker)
|
||||
marker.visibility = visibility
|
||||
overrides[marker] = validator
|
||||
if missing := wanted - {str(m) for m in overrides}:
|
||||
raise ValueError(f"with_visibility: keys not in schema: {sorted(missing)}")
|
||||
return schema.extend(overrides)
|
||||
|
||||
|
||||
class FinalExternalInvalid(Invalid):
|
||||
"""Represents an invalid value in the final validation phase where the path should not be prepended."""
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
"""The template platforms surface value-describing metadata on the main form.
|
||||
|
||||
Hardware platforms get sensible defaults for unit/device_class/etc., so those
|
||||
fields fall through to the editor's advanced disclosure. A ``template`` entity
|
||||
has no such defaults -- the user is expected to define them -- so the template
|
||||
platforms pass ``visibility=cv.Visibility.UI`` to promote them onto the form.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
|
||||
import pytest
|
||||
|
||||
import esphome.config_validation as cv
|
||||
|
||||
|
||||
def _markers(schema: cv.Schema) -> dict[str, object]:
|
||||
s = schema
|
||||
if hasattr(s, "validators"):
|
||||
# cv.All -> the schema is the first validator.
|
||||
s = s.validators[0]
|
||||
return {str(k): k for k in s.schema}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("platform", "fields"),
|
||||
[
|
||||
(
|
||||
"sensor",
|
||||
[
|
||||
"unit_of_measurement",
|
||||
"accuracy_decimals",
|
||||
"device_class",
|
||||
"state_class",
|
||||
"force_update",
|
||||
],
|
||||
),
|
||||
("binary_sensor", ["device_class"]),
|
||||
("switch", ["device_class"]),
|
||||
("cover", ["device_class"]),
|
||||
("button", ["device_class"]),
|
||||
("valve", ["device_class"]),
|
||||
("event", ["device_class"]),
|
||||
("text_sensor", ["device_class"]),
|
||||
("number", ["device_class", "unit_of_measurement"]),
|
||||
],
|
||||
)
|
||||
def test_template_metadata_is_ui(platform: str, fields: list[str]) -> None:
|
||||
mod = importlib.import_module(f"esphome.components.template.{platform}")
|
||||
markers = _markers(mod.CONFIG_SCHEMA)
|
||||
for field in fields:
|
||||
assert markers[field].visibility is cv.Visibility.UI, f"{platform}.{field}"
|
||||
|
||||
|
||||
def test_template_sensor_promotion_preserves_defaults() -> None:
|
||||
"""Promoting to UI must not drop the fields' defaults."""
|
||||
from esphome.components.template.sensor import CONFIG_SCHEMA
|
||||
|
||||
markers = _markers(CONFIG_SCHEMA)
|
||||
assert markers["accuracy_decimals"].default() == 1
|
||||
assert markers["force_update"].default() is False
|
||||
|
||||
|
||||
def test_hardware_platform_metadata_not_promoted() -> None:
|
||||
"""Without ``visibility=`` the builders leave metadata unset.
|
||||
|
||||
Unset markers fall through to the consumer's ``Optional`` default of
|
||||
advanced, so hardware platforms are unaffected by the template promotion.
|
||||
"""
|
||||
from esphome.components import binary_sensor, sensor
|
||||
|
||||
hw_sensor = _markers(sensor.sensor_schema(device_class="temperature"))
|
||||
assert hw_sensor["device_class"].visibility is None
|
||||
hw_bs = _markers(binary_sensor.binary_sensor_schema(device_class="motion"))
|
||||
assert hw_bs["device_class"].visibility is None
|
||||
@@ -1394,6 +1394,35 @@ def test_entity_metadata_visibility_hints() -> None:
|
||||
assert web["web_server"].visibility is advanced
|
||||
|
||||
|
||||
def test_with_visibility_remarks_keys() -> None:
|
||||
"""``with_visibility`` re-marks the named keys, preserving each field's
|
||||
default and validator, without touching the other keys or the input schema.
|
||||
"""
|
||||
base = cv.Schema(
|
||||
{
|
||||
cv.Optional("a", default=7): cv.int_,
|
||||
cv.Optional("b", visibility=cv.Visibility.ADVANCED): cv.string,
|
||||
}
|
||||
)
|
||||
promoted = cv.with_visibility(base, cv.Visibility.UI, "a")
|
||||
|
||||
pm = {str(k): k for k in promoted.schema}
|
||||
assert pm["a"].visibility is cv.Visibility.UI # re-marked
|
||||
assert pm["a"].default() == 7 # default preserved
|
||||
assert pm["b"].visibility is cv.Visibility.ADVANCED # sibling untouched
|
||||
assert promoted({}) == {"a": 7} # validator/default still applied
|
||||
|
||||
# The input schema is left untouched (no shared-marker mutation).
|
||||
assert {str(k): k for k in base.schema}["a"].visibility is None
|
||||
|
||||
|
||||
def test_with_visibility_unknown_key_raises() -> None:
|
||||
"""A key not present in the schema is a typo — fail at build time."""
|
||||
base = cv.Schema({cv.Optional("a"): cv.int_})
|
||||
with pytest.raises(ValueError, match="not in schema"):
|
||||
cv.with_visibility(base, cv.Visibility.UI, "nope")
|
||||
|
||||
|
||||
def _wrap_str(value: str) -> ESPHomeDataBase:
|
||||
"""Wrap a raw string as an ESPHomeDataBase, mimicking a YAML-loaded value."""
|
||||
return make_data_base(value)
|
||||
|
||||
Reference in New Issue
Block a user