@ -6,6 +6,7 @@ from esphome.core import coroutine
from esphome.const import (
from esphome.const import (
CONF_RED , CONF_GREEN , CONF_BLUE , CONF_WHITE , CONF_COLOR_TEMPERATURE ,
CONF_RED , CONF_GREEN , CONF_BLUE , CONF_WHITE , CONF_COLOR_TEMPERATURE ,
CONF_OUTPUT_ID , CONF_TRIGGER_ID , CONF_ID ,
CONF_OUTPUT_ID , CONF_TRIGGER_ID , CONF_ID ,
CONF_TRANSITION_LENGTH , CONF_BRIGHTNESS , CONF_EFFECT
)
)
from .. import bslamp2_ns , CODEOWNERS , CONF_LIGHT_HAL_ID , LightHAL
from .. import bslamp2_ns , CODEOWNERS , CONF_LIGHT_HAL_ID , LightHAL
@ -14,6 +15,7 @@ AUTO_LOAD = ["xiaomi_bslamp2"]
CONF_MASTER1 = " master1 "
CONF_MASTER1 = " master1 "
CONF_MASTER2 = " master2 "
CONF_MASTER2 = " master2 "
CONF_ON_BRIGHTNESS = " on_brightness "
CONF_ON_BRIGHTNESS = " on_brightness "
CONF_PRESET_ID = " preset_id "
CONF_PRESETS_ID = " presets_id "
CONF_PRESETS_ID = " presets_id "
CONF_PRESETS = " presets "
CONF_PRESETS = " presets "
CONF_NEXT = " next "
CONF_NEXT = " next "
@ -23,27 +25,44 @@ CONF_PRESET = "preset"
XiaomiBslamp2LightState = bslamp2_ns . class_ ( " XiaomiBslamp2LightState " , light . LightState )
XiaomiBslamp2LightState = bslamp2_ns . class_ ( " XiaomiBslamp2LightState " , light . LightState )
XiaomiBslamp2LightOutput = bslamp2_ns . class_ ( " XiaomiBslamp2LightOutput " , light . LightOutput )
XiaomiBslamp2LightOutput = bslamp2_ns . class_ ( " XiaomiBslamp2LightOutput " , light . LightOutput )
PresetsContainer = bslamp2_ns . class_ ( " PresetsContainer " , cg . Component )
PresetsContainer = bslamp2_ns . class_ ( " PresetsContainer " , cg . Component )
Preset = bslamp2_ns . class_ ( " Preset " , cg . Component )
BrightnessTrigger = bslamp2_ns . class_ ( " BrightnessTrigger " , automation . Trigger . template ( ) )
BrightnessTrigger = bslamp2_ns . class_ ( " BrightnessTrigger " , automation . Trigger . template ( ) )
ActivatePresetAction = bslamp2_ns . class_ ( " ActivatePresetAction " , automation . Action )
ActivatePresetAction = bslamp2_ns . class_ ( " ActivatePresetAction " , automation . Action )
PRESETS_SCHEMA = cv . Schema ( {
PRESETS_SCHEMA = cv . Schema ( {
str . lower : cv . Schema ( {
str . lower : cv . Schema ( {
str . lower : cv . Any (
cv . Schema ( {
cv . Optional ( CONF_RED , default = 0 ) : cv . percentage ,
cv . Optional ( CONF_GREEN , default = 0 ) : cv . percentage ,
cv . Optional ( CONF_BLUE , default = 0 ) : cv . percentage ,
} ) ,
cv . Schema ( {
cv . Required ( CONF_COLOR_TEMPERATURE ) : cv . int_range ( min = 153 , max = 588 ) ,
} ) ,
)
str . lower : light . automation . LIGHT_TURN_ON_ACTION_SCHEMA
} )
} )
} )
} )
PRESET_SCHEMA_BASE = cv . Schema (
{
cv . GenerateID ( CONF_ID ) : cv . use_id ( XiaomiBslamp2LightState ) ,
cv . GenerateID ( CONF_PRESET_ID ) : cv . declare_id ( Preset ) ,
}
)
PRESET_SCHEMA = cv . Any (
PRESET_SCHEMA_BASE . extend ( {
cv . Required ( CONF_EFFECT ) : cv . string
} ) ,
PRESET_SCHEMA_BASE . extend ( {
cv . Required ( CONF_COLOR_TEMPERATURE ) : cv . color_temperature ,
cv . Optional ( CONF_BRIGHTNESS ) : cv . percentage ,
cv . Optional ( CONF_TRANSITION_LENGTH ) : cv . positive_time_period_milliseconds ,
} ) ,
PRESET_SCHEMA_BASE . extend ( {
cv . Required ( CONF_RED ) : cv . percentage ,
cv . Required ( CONF_GREEN ) : cv . percentage ,
cv . Required ( CONF_BLUE ) : cv . percentage ,
cv . Optional ( CONF_BRIGHTNESS ) : cv . percentage ,
cv . Optional ( CONF_TRANSITION_LENGTH ) : cv . positive_time_period_milliseconds ,
} ) ,
)
CONFIG_SCHEMA = light . RGB_LIGHT_SCHEMA . extend (
CONFIG_SCHEMA = light . RGB_LIGHT_SCHEMA . extend (
{
{
cv . GenerateID ( ) : cv . declare_id ( XiaomiBslamp2LightState ) ,
cv . GenerateID ( CONF_ID ) : cv . declare_id ( XiaomiBslamp2LightState ) ,
cv . GenerateID ( CONF_LIGHT_HAL_ID ) : cv . use_id ( LightHAL ) ,
cv . GenerateID ( CONF_LIGHT_HAL_ID ) : cv . use_id ( LightHAL ) ,
cv . GenerateID ( CONF_OUTPUT_ID ) : cv . declare_id ( XiaomiBslamp2LightOutput ) ,
cv . GenerateID ( CONF_OUTPUT_ID ) : cv . declare_id ( XiaomiBslamp2LightOutput ) ,
cv . Optional ( CONF_ON_BRIGHTNESS ) : automation . validate_automation (
cv . Optional ( CONF_ON_BRIGHTNESS ) : automation . validate_automation (
@ -52,7 +71,11 @@ CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
}
}
) ,
) ,
cv . GenerateID ( CONF_PRESETS_ID ) : cv . declare_id ( PresetsContainer ) ,
cv . GenerateID ( CONF_PRESETS_ID ) : cv . declare_id ( PresetsContainer ) ,
cv . Optional ( CONF_PRESETS ) : PRESETS_SCHEMA ,
cv . Optional ( CONF_PRESETS ) : cv . Schema ( {
str . lower : cv . Schema ( {
str . lower : PRESET_SCHEMA
} )
} ) ,
}
}
)
)
@ -127,20 +150,38 @@ def on_brightness_to_code(config):
trigger = cg . new_Pvariable ( conf [ CONF_TRIGGER_ID ] , light_output_var )
trigger = cg . new_Pvariable ( conf [ CONF_TRIGGER_ID ] , light_output_var )
yield automation . build_automation ( trigger , [ ( float , " x " ) ] , conf )
yield automation . build_automation ( trigger , [ ( float , " x " ) ] , conf )
@coroutine
def preset_to_code ( config , preset_group , preset_name ) :
light_var = yield cg . get_variable ( config [ CONF_ID ] )
preset_var = cg . new_Pvariable (
config [ CONF_PRESET_ID ] , light_var , preset_group , preset_name )
if CONF_TRANSITION_LENGTH in config :
cg . add ( preset_var . set_transition_length ( config [ CONF_TRANSITION_LENGTH ] ) )
if CONF_BRIGHTNESS in config :
cg . add ( preset_var . set_brightness ( config [ CONF_BRIGHTNESS ] ) )
if CONF_RED in config :
cg . add ( preset_var . set_red ( config [ CONF_RED ] ) )
if CONF_GREEN in config :
cg . add ( preset_var . set_green ( config [ CONF_GREEN ] ) )
if CONF_BLUE in config :
cg . add ( preset_var . set_blue ( config [ CONF_BLUE ] ) )
if CONF_COLOR_TEMPERATURE in config :
cg . add ( preset_var . set_color_temperature ( config [ CONF_COLOR_TEMPERATURE ] ) )
if CONF_EFFECT in config :
cg . add ( preset_var . set_effect ( config [ CONF_EFFECT ] ) )
else :
cg . add ( preset_var . set_effect ( " None " ) )
yield cg . register_component ( preset_var , config )
@coroutine
@coroutine
def presets_to_code ( config ) :
def presets_to_code ( config ) :
light_state_var = yield cg . get_variable ( config [ CONF_ID ] )
presets_var = cg . new_Pvariable ( config [ CONF_PRESETS_ID ] , light_state_var )
presets_var = cg . new_Pvariable ( config [ CONF_PRESETS_ID ] )
yield cg . register_component ( presets_var , config )
yield cg . register_component ( presets_var , config )
for preset_group , presets in config . get ( CONF_PRESETS , { } ) . items ( ) :
for preset_group , presets in config . get ( CONF_PRESETS , { } ) . items ( ) :
for name , preset in presets . items ( ) :
if CONF_COLOR_TEMPERATURE in preset :
cg . add ( presets_var . add (
preset_group , name , preset [ CONF_COLOR_TEMPERATURE ] ) )
else :
cg . add ( presets_var . add (
preset_group , name , preset [ CONF_RED ] , preset [ CONF_GREEN ] , preset [ CONF_BLUE ] ) )
for preset_name , preset_config in presets . items ( ) :
preset = yield preset_to_code ( preset_config , preset_group , preset_name )
cg . add ( presets_var . add_preset ( preset ) )
def to_code ( config ) :
def to_code ( config ) :
yield light_output_to_code ( config )
yield light_output_to_code ( config )