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.

42 lines
1.2 KiB

  1. import esphome.codegen as cg
  2. import esphome.config_validation as cv
  3. from esphome.components import binary_sensor
  4. from esphome.const import CONF_ID
  5. from .. import (
  6. bs2_ns, CODEOWNERS,
  7. CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL
  8. )
  9. AUTO_LOAD = ["yeelight_bs2"]
  10. CONF_PART = "part"
  11. PARTS = {
  12. "ANY" : 0,
  13. "POWER_BUTTON" : 1,
  14. "COLOR_BUTTON" : 2,
  15. "SLIDER" : 3.
  16. }
  17. def validate_part(value):
  18. value = cv.string(value)
  19. return cv.enum(PARTS, upper=True, space='_')(value)
  20. YeelightBS2Button = bs2_ns.class_("YeelightBS2Button", binary_sensor.BinarySensor, cg.Component)
  21. CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
  22. {
  23. cv.GenerateID(): cv.declare_id(YeelightBS2Button),
  24. cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
  25. cv.Optional(CONF_PART, default="ANY"): validate_part,
  26. }
  27. ).extend(cv.COMPONENT_SCHEMA)
  28. def to_code(config):
  29. var = cg.new_Pvariable(config[CONF_ID])
  30. yield cg.register_component(var, config)
  31. yield binary_sensor.register_binary_sensor(var, config)
  32. front_panel_hal_var = yield cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID])
  33. cg.add(var.set_parent(front_panel_hal_var))
  34. cg.add(var.set_part(config[CONF_PART]))