Improved validation for Addressable Light Partition Segments (#2439)

Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
Paul Monigatti 2021-10-05 12:00:23 +13:00 committed by GitHub
parent 877367677b
commit 6ec546a6a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,11 +1,13 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
import esphome.final_validate as fv
from esphome.components import light from esphome.components import light
from esphome.const import ( from esphome.const import (
CONF_ADDRESSABLE_LIGHT_ID, CONF_ADDRESSABLE_LIGHT_ID,
CONF_FROM, CONF_FROM,
CONF_ID, CONF_ID,
CONF_LIGHT_ID, CONF_LIGHT_ID,
CONF_NUM_LEDS,
CONF_SEGMENTS, CONF_SEGMENTS,
CONF_SINGLE_LIGHT_ID, CONF_SINGLE_LIGHT_ID,
CONF_TO, CONF_TO,
@ -31,6 +33,27 @@ def validate_from_to(value):
return value return value
def validate_segment(config):
fconf = fv.full_config.get()
if CONF_ID in config: # only validate addressable segments
path = fconf.get_path_for_id(config[CONF_ID])[:-1]
segment_light_config = fconf.get_config_for_path(path)
if CONF_NUM_LEDS in segment_light_config:
segment_len = segment_light_config[CONF_NUM_LEDS]
if config[CONF_FROM] >= segment_len:
raise cv.Invalid(
f"FROM ({config[CONF_FROM]}) must be less than the number of LEDs in light '{config[CONF_ID]}' ({segment_len})",
[CONF_FROM],
)
if config[CONF_TO] >= segment_len:
raise cv.Invalid(
f"TO ({config[CONF_TO]}) must be less than the number of LEDs in light '{config[CONF_ID]}' ({segment_len})",
[CONF_TO],
)
ADDRESSABLE_SEGMENT_SCHEMA = cv.Schema( ADDRESSABLE_SEGMENT_SCHEMA = cv.Schema(
{ {
cv.Required(CONF_ID): cv.use_id(light.AddressableLightState), cv.Required(CONF_ID): cv.use_id(light.AddressableLightState),
@ -63,6 +86,13 @@ CONFIG_SCHEMA = light.ADDRESSABLE_LIGHT_SCHEMA.extend(
} }
) )
FINAL_VALIDATE_SCHEMA = cv.Schema(
{
cv.Required(CONF_SEGMENTS): [validate_segment],
},
extra=cv.ALLOW_EXTRA,
)
async def to_code(config): async def to_code(config):
segments = [] segments = []