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.

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