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.

121 lines
4.5 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, CONF_LEVEL
  5. from esphome import automation
  6. from .. import (
  7. bslamp2_ns, CODEOWNERS,
  8. CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL, FRONT_PANEL_LED_OPTIONS
  9. )
  10. CONF_LEDS = "leds"
  11. DEPENDENCIES = ["xiaomi_bslamp2"]
  12. XiaomiBslamp2FrontPanelOutput = bslamp2_ns.class_(
  13. "XiaomiBslamp2FrontPanelOutput", output.FloatOutput, cg.Component)
  14. SetLEDsAction = bslamp2_ns.class_("SetLEDsAction", automation.Action)
  15. SetLevelAction = bslamp2_ns.class_("SetLevelAction", automation.Action)
  16. UpdateLEDsAction = bslamp2_ns.class_("UpdateLEDsAction", automation.Action)
  17. CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
  18. {
  19. cv.GenerateID(): cv.declare_id(XiaomiBslamp2FrontPanelOutput),
  20. cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
  21. }
  22. ).extend(cv.COMPONENT_SCHEMA)
  23. def to_code(config):
  24. var = cg.new_Pvariable(config[CONF_ID])
  25. yield cg.register_component(var, config)
  26. yield output.register_output(var, config)
  27. front_panel_hal_var = yield cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID])
  28. cg.add(var.set_parent(front_panel_hal_var))
  29. def maybe_simple_level_value(schema):
  30. def validator(value):
  31. if isinstance(value, dict):
  32. return schema(value)
  33. return schema({ "level": value })
  34. return validator
  35. def maybe_simple_leds_value(schema):
  36. def validator(value):
  37. if isinstance(value, dict):
  38. return schema(value)
  39. return schema({ "leds": value })
  40. return validator
  41. FRONT_PANEL_SCHEMA = cv.Schema({
  42. cv.GenerateID(CONF_ID): cv.use_id(XiaomiBslamp2FrontPanelOutput),
  43. })
  44. FRONT_PANEL_LEVEL_SCHEMA = cv.Schema(
  45. maybe_simple_level_value(FRONT_PANEL_SCHEMA.extend(
  46. {
  47. cv.Required(CONF_LEVEL): cv.templatable(cv.percentage),
  48. }
  49. ))
  50. )
  51. FRONT_PANEL_LED_SCHEMA = cv.Schema(
  52. maybe_simple_leds_value(FRONT_PANEL_SCHEMA.extend(
  53. {
  54. cv.Required(CONF_LEDS): cv.ensure_list(cv.enum(FRONT_PANEL_LED_OPTIONS, upper=True)),
  55. }
  56. ))
  57. )
  58. @automation.register_action("front_panel.set_level", SetLevelAction, FRONT_PANEL_LEVEL_SCHEMA)
  59. async def set_level_to_code(config, action_id, template_arg, args):
  60. output_var = await cg.get_variable(config[CONF_ID])
  61. action_var = cg.new_Pvariable(action_id, template_arg, output_var)
  62. template_ = await cg.templatable(config[CONF_LEVEL], args, float)
  63. cg.add(action_var.set_level(template_))
  64. return action_var
  65. @automation.register_action("front_panel.set_leds", SetLEDsAction, FRONT_PANEL_LED_SCHEMA)
  66. async def set_leds_to_code(config, action_id, template_arg, args):
  67. output_var = await cg.get_variable(config[CONF_ID])
  68. action_var = cg.new_Pvariable(action_id, template_arg, output_var)
  69. bits = (
  70. [FRONT_PANEL_LED_OPTIONS['NONE']] +
  71. [FRONT_PANEL_LED_OPTIONS[led] for led in config[CONF_LEDS]]
  72. )
  73. value = cg.RawExpression("|".join(map(str, bits)))
  74. cg.add(action_var.set_mode(2))
  75. cg.add(action_var.set_leds(value))
  76. return action_var
  77. @automation.register_action("front_panel.turn_on_leds", SetLEDsAction, FRONT_PANEL_LED_SCHEMA)
  78. async def turn_on_leds_to_code(config, action_id, template_arg, args):
  79. output_var = await cg.get_variable(config[CONF_ID])
  80. action_var = cg.new_Pvariable(action_id, template_arg, output_var)
  81. bits = (
  82. [FRONT_PANEL_LED_OPTIONS['NONE']] +
  83. [FRONT_PANEL_LED_OPTIONS[led] for led in config[CONF_LEDS]]
  84. )
  85. value = cg.RawExpression("|".join(map(str, bits)))
  86. cg.add(action_var.set_mode(1))
  87. cg.add(action_var.set_leds(value))
  88. return action_var
  89. @automation.register_action("front_panel.turn_off_leds", SetLEDsAction, FRONT_PANEL_LED_SCHEMA)
  90. async def turn_off_leds_to_code(config, action_id, template_arg, args):
  91. output_var = await cg.get_variable(config[CONF_ID])
  92. action_var = cg.new_Pvariable(action_id, template_arg, output_var)
  93. bits = (
  94. [FRONT_PANEL_LED_OPTIONS['NONE']] +
  95. [FRONT_PANEL_LED_OPTIONS[led] for led in config[CONF_LEDS]]
  96. )
  97. value = cg.RawExpression("|".join(map(str, bits)))
  98. cg.add(action_var.set_mode(0))
  99. cg.add(action_var.set_leds(value))
  100. return action_var
  101. @automation.register_action("front_panel.update_leds", UpdateLEDsAction, FRONT_PANEL_SCHEMA)
  102. async def update_leds_to_code(config, action_id, template_arg, args):
  103. output_var = await cg.get_variable(config[CONF_ID])
  104. action_var = cg.new_Pvariable(action_id, template_arg, output_var)
  105. return action_var