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.

63 lines
2.1 KiB

  1. import re
  2. import esphome.codegen as cg
  3. import esphome.config_validation as cv
  4. from esphome.components import binary_sensor
  5. from esphome.const import CONF_ID, CONF_FOR
  6. from .. import (
  7. bslamp2_ns, CODEOWNERS,
  8. CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL
  9. )
  10. AUTO_LOAD = ["xiaomi_bslamp2"]
  11. CONF_PART = "part"
  12. # The identifier values match the bit values of the events as defined
  13. # in ../front_panel_hal.h.
  14. PARTS = {
  15. "POWER_BUTTON" : 0b001 << 1,
  16. "POWER" : 0b001 << 1,
  17. "COLOR_BUTTON" : 0b010 << 1,
  18. "COLOR" : 0b010 << 1,
  19. "SLIDER" : 0b100 << 1,
  20. }
  21. XiaomiBslamp2TouchBinarySensor = bslamp2_ns.class_(
  22. "XiaomiBslamp2TouchBinarySensor", binary_sensor.BinarySensor, cg.Component)
  23. def validate_for(value):
  24. value = cv.string(value)
  25. return cv.enum(PARTS, upper=True, space='_')(value)
  26. def validate_binary_sensor(conf):
  27. if CONF_PART in conf and CONF_FOR in conf:
  28. raise cv.Invalid("Specify only one of [part] or [for]")
  29. if CONF_PART in conf and not CONF_FOR in conf:
  30. conf[CONF_FOR] = conf[CONF_PART]
  31. if CONF_FOR not in conf:
  32. raise cv.Invalid("'for' is a required option for [binary_sensor.xiaomi_bslamp2]")
  33. return conf
  34. CONFIG_SCHEMA = cv.All(
  35. binary_sensor.BINARY_SENSOR_SCHEMA.extend(
  36. {
  37. cv.GenerateID(): cv.declare_id(XiaomiBslamp2TouchBinarySensor),
  38. cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
  39. # This option is not advertised in the documentation. It must be
  40. # considered deprecated. I'm not announcing it as such yet. Not sure
  41. # if it's useful to do so.
  42. cv.Optional(CONF_PART): validate_for,
  43. cv.Optional(CONF_FOR): validate_for,
  44. }
  45. ).extend(cv.COMPONENT_SCHEMA),
  46. validate_binary_sensor,
  47. )
  48. def to_code(config):
  49. var = cg.new_Pvariable(config[CONF_ID])
  50. yield cg.register_component(var, config)
  51. yield binary_sensor.register_binary_sensor(var, config)
  52. front_panel_hal_var = yield cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID])
  53. cg.add(var.set_parent(front_panel_hal_var))
  54. cg.add(var.set_for(config[CONF_FOR]))