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.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. "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. XiaomiBslamp2TouchBinarySensor = bslamp2_ns.class_(
  21. "XiaomiBslamp2TouchBinarySensor", binary_sensor.BinarySensor, cg.Component)
  22. CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
  23. {
  24. cv.GenerateID(): cv.declare_id(XiaomiBslamp2TouchBinarySensor),
  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_parent(front_panel_hal_var))
  35. cg.add(var.set_part(config[CONF_PART]))