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.

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