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.

45 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.const import CONF_ID
  5. from .. import (
  6. bslamp2_ns, CODEOWNERS,
  7. CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL
  8. )
  9. AUTO_LOAD = ["xiaomi_bslamp2"]
  10. CONF_PART = "part"
  11. PARTS = {
  12. "ANY" : 0,
  13. "POWER_BUTTON" : 1,
  14. "POWER" : 1,
  15. "COLOR_BUTTON" : 2,
  16. "COLOR" : 2,
  17. "SLIDER" : 3,
  18. }
  19. def validate_part(value):
  20. value = cv.string(value)
  21. return cv.enum(PARTS, upper=True, space='_')(value)
  22. XiaomiBslamp2TouchBinarySensor = bslamp2_ns.class_(
  23. "XiaomiBslamp2TouchBinarySensor", binary_sensor.BinarySensor, cg.Component)
  24. CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
  25. {
  26. cv.GenerateID(): cv.declare_id(XiaomiBslamp2TouchBinarySensor),
  27. cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
  28. cv.Optional(CONF_PART, default="ANY"): validate_part,
  29. }
  30. ).extend(cv.COMPONENT_SCHEMA)
  31. def to_code(config):
  32. var = cg.new_Pvariable(config[CONF_ID])
  33. yield cg.register_component(var, config)
  34. yield binary_sensor.register_binary_sensor(var, config)
  35. front_panel_hal_var = yield cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID])
  36. cg.add(var.set_parent(front_panel_hal_var))
  37. cg.add(var.set_part(config[CONF_PART]))