@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.codegen as cg
import esphome.config_validation as cv
import esphome.config_validation as cv
from esphome.components import output
from esphome.components import output
from esphome.const import CONF_ID
from esphome.const import CONF_ID , CONF_LEVEL
from esphome import automation
from esphome import automation
from .. import (
from .. import (
bslamp2_ns , CODEOWNERS ,
bslamp2_ns , CODEOWNERS ,
@ -14,6 +14,7 @@ AUTO_LOAD = ["xiaomi_bslamp2"]
XiaomiBslamp2FrontPanelOutput = bslamp2_ns . class_ (
XiaomiBslamp2FrontPanelOutput = bslamp2_ns . class_ (
" XiaomiBslamp2FrontPanelOutput " , output . FloatOutput , cg . Component )
" XiaomiBslamp2FrontPanelOutput " , output . FloatOutput , cg . Component )
SetLEDsAction = bslamp2_ns . class_ ( " SetLEDsAction " , automation . Action )
SetLEDsAction = bslamp2_ns . class_ ( " SetLEDsAction " , automation . Action )
SetLevelAction = bslamp2_ns . class_ ( " SetLevelAction " , automation . Action )
UpdateLEDsAction = bslamp2_ns . class_ ( " UpdateLEDsAction " , automation . Action )
UpdateLEDsAction = bslamp2_ns . class_ ( " UpdateLEDsAction " , automation . Action )
CONFIG_SCHEMA = output . FLOAT_OUTPUT_SCHEMA . extend (
CONFIG_SCHEMA = output . FLOAT_OUTPUT_SCHEMA . extend (
@ -31,6 +32,13 @@ def to_code(config):
front_panel_hal_var = yield cg . get_variable ( config [ CONF_FRONT_PANEL_HAL_ID ] )
front_panel_hal_var = yield cg . get_variable ( config [ CONF_FRONT_PANEL_HAL_ID ] )
cg . add ( var . set_parent ( front_panel_hal_var ) )
cg . add ( var . set_parent ( front_panel_hal_var ) )
def maybe_simple_level_value ( schema ) :
def validator ( value ) :
if isinstance ( value , dict ) :
return schema ( value )
return schema ( { " level " : value } )
return validator
def maybe_simple_leds_value ( schema ) :
def maybe_simple_leds_value ( schema ) :
def validator ( value ) :
def validator ( value ) :
if isinstance ( value , dict ) :
if isinstance ( value , dict ) :
@ -42,13 +50,30 @@ FRONT_PANEL_SCHEMA = cv.Schema({
cv . GenerateID ( CONF_ID ) : cv . use_id ( XiaomiBslamp2FrontPanelOutput ) ,
cv . GenerateID ( CONF_ID ) : cv . use_id ( XiaomiBslamp2FrontPanelOutput ) ,
} )
} )
FRONT_PANEL_LEVEL_SCHEMA = cv . Schema (
maybe_simple_level_value ( FRONT_PANEL_SCHEMA . extend (
{
cv . Required ( CONF_LEVEL ) : cv . templatable ( cv . percentage ) ,
}
) )
)
FRONT_PANEL_LED_SCHEMA = cv . Schema (
FRONT_PANEL_LED_SCHEMA = cv . Schema (
maybe_simple_leds_value ( cv . Schema ( {
cv . GenerateID ( CONF_ID ) : cv . use_id ( XiaomiBslamp2FrontPanelOutput ) ,
cv . Required ( CONF_LEDS ) : cv . ensure_list ( cv . enum ( FRONT_PANEL_LED_OPTIONS , upper = True ) ) ,
} ) )
maybe_simple_leds_value ( FRONT_PANEL_SCHEMA . extend (
{
cv . Required ( CONF_LEDS ) : cv . ensure_list ( cv . enum ( FRONT_PANEL_LED_OPTIONS , upper = True ) ) ,
}
) )
)
)
@automation.register_action ( " front_panel.set_level " , SetLevelAction , FRONT_PANEL_LEVEL_SCHEMA )
async def set_level_to_code ( config , action_id , template_arg , args ) :
output_var = await cg . get_variable ( config [ CONF_ID ] )
action_var = cg . new_Pvariable ( action_id , template_arg , output_var )
template_ = await cg . templatable ( config [ CONF_LEVEL ] , args , float )
cg . add ( action_var . set_level ( template_ ) )
return action_var
@automation.register_action ( " front_panel.set_leds " , SetLEDsAction , FRONT_PANEL_LED_SCHEMA )
@automation.register_action ( " front_panel.set_leds " , SetLEDsAction , FRONT_PANEL_LED_SCHEMA )
async def set_leds_to_code ( config , action_id , template_arg , args ) :
async def set_leds_to_code ( config , action_id , template_arg , args ) :
output_var = await cg . get_variable ( config [ CONF_ID ] )
output_var = await cg . get_variable ( config [ CONF_ID ] )