1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 22:24:26 +00:00

[config] Deprecate more *_SCHEMA constants (#8763)

This commit is contained in:
Jesse Hills
2025-05-13 13:24:13 +12:00
committed by GitHub
parent 49f631d6c5
commit bc0956019b
5 changed files with 202 additions and 11 deletions

View File

@@ -1,3 +1,5 @@
import enum
import esphome.automation as auto
import esphome.codegen as cg
from esphome.components import mqtt, power_supply, web_server
@@ -13,15 +15,18 @@ from esphome.const import (
CONF_COLOR_TEMPERATURE,
CONF_DEFAULT_TRANSITION_LENGTH,
CONF_EFFECTS,
CONF_ENTITY_CATEGORY,
CONF_FLASH_TRANSITION_LENGTH,
CONF_GAMMA_CORRECT,
CONF_GREEN,
CONF_ICON,
CONF_ID,
CONF_INITIAL_STATE,
CONF_MQTT_ID,
CONF_ON_STATE,
CONF_ON_TURN_OFF,
CONF_ON_TURN_ON,
CONF_OUTPUT_ID,
CONF_POWER_SUPPLY,
CONF_RED,
CONF_RESTORE_MODE,
@@ -33,6 +38,7 @@ from esphome.const import (
CONF_WHITE,
)
from esphome.core import coroutine_with_priority
from esphome.cpp_generator import MockObjClass
from esphome.cpp_helpers import setup_entity
from .automation import LIGHT_STATE_SCHEMA
@@ -141,6 +147,51 @@ ADDRESSABLE_LIGHT_SCHEMA = RGB_LIGHT_SCHEMA.extend(
)
class LightType(enum.IntEnum):
"""Light type enum."""
BINARY = 0
BRIGHTNESS_ONLY = 1
RGB = 2
ADDRESSABLE = 3
def light_schema(
class_: MockObjClass,
type_: LightType,
*,
entity_category: str = cv.UNDEFINED,
icon: str = cv.UNDEFINED,
default_restore_mode: str = cv.UNDEFINED,
) -> cv.Schema:
schema = {
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(class_),
}
for key, default, validator in [
(CONF_ENTITY_CATEGORY, entity_category, cv.entity_category),
(CONF_ICON, icon, cv.icon),
(
CONF_RESTORE_MODE,
default_restore_mode,
cv.enum(RESTORE_MODES, upper=True, space="_"),
),
]:
if default is not cv.UNDEFINED:
schema[cv.Optional(key, default=default)] = validator
if type_ == LightType.BINARY:
return BINARY_LIGHT_SCHEMA.extend(schema)
if type_ == LightType.BRIGHTNESS_ONLY:
return BRIGHTNESS_ONLY_LIGHT_SCHEMA.extend(schema)
if type_ == LightType.RGB:
return RGB_LIGHT_SCHEMA.extend(schema)
if type_ == LightType.ADDRESSABLE:
return ADDRESSABLE_LIGHT_SCHEMA.extend(schema)
raise ValueError(f"Invalid light type: {type_}")
def validate_color_temperature_channels(value):
if (
CONF_COLD_WHITE_COLOR_TEMPERATURE in value
@@ -223,6 +274,12 @@ async def register_light(output_var, config):
await setup_light_core_(light_var, output_var, config)
async def new_light(config, *args):
output_var = cg.new_Pvariable(config[CONF_OUTPUT_ID], *args)
await register_light(output_var, config)
return output_var
@coroutine_with_priority(100.0)
async def to_code(config):
cg.add_define("USE_LIGHT")