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.

43 lines
1.3 KiB

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