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.

127 lines
4.7 KiB

Introduced a HUB component + front panel IRQ handling A HUB component was introduced. This HUB component has all the knowledge about the Yeelight Bedside Lamp 2 hardware. It known what pins are used, that PWM frequencies to use, what pins to switch in binary mode, etc. etc. No configuration is required for this HUB component. It's automatically loaded when the light component is loaded. The light component will use the HUB component to access the pins that are required for driving the LED circuitry. Note that this simplifies the configuration by A LOT. There's no need anymore to configure the pinouts in the YAML file. This is a logical route to take, since we're talking about a factory-produced PCB with a soldered on ESP32 chip, which uses the same GPIO's and settings on all produced devices (I presume). It would be quite redundant to force every user into configuring these pinouts themselves. ** Beware to update your device yaml configuration ** There are a few pinouts left to move into the HUB. I will do that in the next commit. Your device yaml configuration can be simplified along with these changes. Some of the keys in the existing light configuration block will no longer work and will have to be removed (red, green, blue, white). ** Further development ** The HUB will be extended make it the central component that also handles the I2C communication. This way, there is a central place to regulate the traffic to and from the front panel. We will be able to build upon this by implementing extra, fully separated components that handle for example the front panel light level, the power button, the color button and the slider. ** Interrupt handler for the I2C IRQ trigger pin ** One requirement for the I2C communication has already been implemented: an interrupt handler for the GPIO that is used by the front panel to signal the ESP that a new touch or release event is avilable to be read. It doens't do anything functionally right now, but if you watch the log file, you will see that touch events are detected and that they trigger some log messages.
3 years ago
  1. import esphome.codegen as cg
  2. import esphome.config_validation as cv
  3. from esphome.components import light
  4. from esphome import automation
  5. from esphome.core import coroutine
  6. from esphome.const import (
  7. CONF_RED, CONF_GREEN, CONF_BLUE, CONF_WHITE, CONF_COLOR_TEMPERATURE,
  8. CONF_OUTPUT_ID, CONF_TRIGGER_ID, CONF_ID,
  9. )
  10. from .. import bslamp2_ns, CODEOWNERS, CONF_LIGHT_HAL_ID, LightHAL
  11. AUTO_LOAD = ["xiaomi_bslamp2"]
  12. CONF_MASTER1 = "master1"
  13. CONF_MASTER2 = "master2"
  14. CONF_ON_BRIGHTNESS = "on_brightness"
  15. CONF_PRESETS_ID = "presets_id"
  16. CONF_PRESETS = "presets"
  17. CONF_NEXT = "next"
  18. CONF_GROUP = "group"
  19. CONF_PRESET = "preset"
  20. XiaomiBslamp2LightState = bslamp2_ns.class_("XiaomiBslamp2LightState", light.LightState)
  21. XiaomiBslamp2LightOutput = bslamp2_ns.class_("XiaomiBslamp2LightOutput", light.LightOutput)
  22. PresetsContainer = bslamp2_ns.class_("PresetsContainer", cg.Component)
  23. BrightnessTrigger = bslamp2_ns.class_("BrightnessTrigger", automation.Trigger.template())
  24. ActivatePresetAction = bslamp2_ns.class_("ActivatePresetAction", automation.Action)
  25. PRESETS_SCHEMA = cv.Schema({
  26. str.lower: cv.Schema({
  27. str.lower: cv.Any(
  28. cv.Schema({
  29. cv.Optional(CONF_RED, default=0): cv.percentage,
  30. cv.Optional(CONF_GREEN, default=0): cv.percentage,
  31. cv.Optional(CONF_BLUE, default=0): cv.percentage,
  32. }),
  33. cv.Schema({
  34. cv.Required(CONF_COLOR_TEMPERATURE): cv.int_range(min=153, max=588),
  35. }),
  36. )
  37. })
  38. })
  39. CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
  40. {
  41. cv.GenerateID(): cv.declare_id(XiaomiBslamp2LightState),
  42. cv.GenerateID(CONF_LIGHT_HAL_ID): cv.use_id(LightHAL),
  43. cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(XiaomiBslamp2LightOutput),
  44. cv.Optional(CONF_ON_BRIGHTNESS): automation.validate_automation(
  45. {
  46. cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(BrightnessTrigger),
  47. }
  48. ),
  49. cv.GenerateID(CONF_PRESETS_ID): cv.declare_id(PresetsContainer),
  50. cv.Optional(CONF_PRESETS): PRESETS_SCHEMA,
  51. }
  52. )
  53. def is_preset_group(value):
  54. return value
  55. def is_preset(value):
  56. return value
  57. @automation.register_action(
  58. "preset.activate",
  59. ActivatePresetAction,
  60. cv.Schema(cv.Any(
  61. cv.Schema({
  62. cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer),
  63. cv.Required(CONF_GROUP): is_preset_group,
  64. cv.Optional(CONF_PRESET): is_preset
  65. }),
  66. cv.Schema({
  67. cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer),
  68. cv.Required(CONF_NEXT): cv.one_of(CONF_GROUP, CONF_PRESET, lower=True)
  69. })
  70. ))
  71. )
  72. def preset_activate_to_code(config, action_id, template_arg, args):
  73. presets_var = yield cg.get_variable(config[CONF_PRESETS_ID])
  74. action_var = cg.new_Pvariable(action_id, template_arg, presets_var)
  75. if CONF_NEXT in config:
  76. cg.add(action_var.set_operation(f"next_{config[CONF_NEXT]}"))
  77. elif CONF_PRESET in config:
  78. cg.add(action_var.set_operation("activate_preset"))
  79. cg.add(action_var.set_group(config[CONF_GROUP]))
  80. cg.add(action_var.set_preset(config[CONF_PRESET]))
  81. else:
  82. cg.add(action_var.set_operation("activate_group"))
  83. cg.add(action_var.set_group(config[CONF_GROUP]))
  84. yield action_var
  85. @coroutine
  86. def light_output_to_code(config):
  87. light_output_var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
  88. yield light.register_light(light_output_var, config)
  89. light_hal_var = yield cg.get_variable(config[CONF_LIGHT_HAL_ID])
  90. cg.add(light_output_var.set_parent(light_hal_var))
  91. @coroutine
  92. def on_brightness_to_code(config):
  93. light_output_var = yield cg.get_variable(config[CONF_OUTPUT_ID])
  94. for conf in config.get(CONF_ON_BRIGHTNESS, []):
  95. trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_output_var)
  96. yield automation.build_automation(trigger, [(float, "x")], conf)
  97. @coroutine
  98. def presets_to_code(config):
  99. light_state_var = yield cg.get_variable(config[CONF_ID])
  100. presets_var = cg.new_Pvariable(config[CONF_PRESETS_ID], light_state_var)
  101. yield cg.register_component(presets_var, config)
  102. for preset_group, presets in config.get(CONF_PRESETS, {}).items():
  103. for name, preset in presets.items():
  104. if CONF_COLOR_TEMPERATURE in preset:
  105. cg.add(presets_var.add(
  106. preset_group, name, preset[CONF_COLOR_TEMPERATURE]))
  107. else:
  108. cg.add(presets_var.add(
  109. preset_group, name, preset[CONF_RED], preset[CONF_GREEN], preset[CONF_BLUE]))
  110. def to_code(config):
  111. yield light_output_to_code(config)
  112. yield on_brightness_to_code(config)
  113. yield presets_to_code(config)