You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB

  1. import esphome.codegen as cg
  2. import esphome.config_validation as cv
  3. from esphome.components import output
  4. from esphome.const import CONF_ID
  5. from esphome import automation
  6. from .. import (
  7. bslamp2_ns, CODEOWNERS,
  8. CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL, CONF_LEDS
  9. )
  10. AUTO_LOAD = ["xiaomi_bslamp2"]
  11. XiaomiBslamp2FrontPanelOutput = bslamp2_ns.class_(
  12. "XiaomiBslamp2FrontPanelOutput", output.FloatOutput, cg.Component)
  13. SetLEDsAction = bslamp2_ns.class_("SetLEDsAction", automation.Action)
  14. CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
  15. {
  16. cv.GenerateID(): cv.declare_id(XiaomiBslamp2FrontPanelOutput),
  17. cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
  18. }
  19. ).extend(cv.COMPONENT_SCHEMA)
  20. def to_code(config):
  21. var = cg.new_Pvariable(config[CONF_ID])
  22. yield cg.register_component(var, config)
  23. yield output.register_output(var, config)
  24. front_panel_hal_var = yield cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID])
  25. cg.add(var.set_parent(front_panel_hal_var))
  26. def maybe_simple_leds_value(schema):
  27. def validator(value):
  28. if isinstance(value, dict):
  29. return schema(value)
  30. return schema({ "leds": value })
  31. return validator
  32. @automation.register_action(
  33. "output.set_leds",
  34. SetLEDsAction,
  35. cv.Schema(
  36. maybe_simple_leds_value(cv.Schema({
  37. cv.GenerateID(CONF_ID): cv.use_id(XiaomiBslamp2FrontPanelOutput),
  38. cv.Required(CONF_LEDS): cv.templatable(cv.uint16_t),
  39. }))
  40. )
  41. )
  42. async def set_leds_to_code(config, action_id, template_arg, args):
  43. output_var = await cg.get_variable(config[CONF_ID])
  44. var = cg.new_Pvariable(action_id, template_arg, output_var)
  45. template_ = await cg.templatable(config[CONF_LEDS], args, cg.uint16)
  46. cg.add(var.set_leds(template_))
  47. return var