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.

148 lines
5.3 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. def maybe_simple_preset_action(schema):
  58. def validator(value):
  59. if isinstance(value, dict):
  60. return schema(value)
  61. value = value.lower()
  62. conf = {}
  63. if value == "next_group":
  64. conf[CONF_NEXT] = CONF_GROUP
  65. elif value == "next_preset":
  66. conf[CONF_NEXT] = CONF_PRESET
  67. elif "." not in value:
  68. conf[CONF_GROUP] = value
  69. else:
  70. group, preset = value.split(".", 2)
  71. conf[CONF_GROUP] = group
  72. conf[CONF_PRESET] = preset
  73. return schema(conf)
  74. return validator
  75. @automation.register_action(
  76. "preset.activate",
  77. ActivatePresetAction,
  78. cv.Schema(
  79. maybe_simple_preset_action(cv.Any(
  80. cv.Schema({
  81. cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer),
  82. cv.Required(CONF_GROUP): is_preset_group,
  83. cv.Optional(CONF_PRESET): is_preset
  84. }),
  85. cv.Schema({
  86. cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer),
  87. cv.Required(CONF_NEXT): cv.one_of(CONF_GROUP, CONF_PRESET, lower=True)
  88. })
  89. ))
  90. )
  91. )
  92. def preset_activate_to_code(config, action_id, template_arg, args):
  93. presets_var = yield cg.get_variable(config[CONF_PRESETS_ID])
  94. action_var = cg.new_Pvariable(action_id, template_arg, presets_var)
  95. if CONF_NEXT in config:
  96. cg.add(action_var.set_operation(f"next_{config[CONF_NEXT]}"))
  97. elif CONF_PRESET in config:
  98. cg.add(action_var.set_operation("activate_preset"))
  99. cg.add(action_var.set_group(config[CONF_GROUP]))
  100. cg.add(action_var.set_preset(config[CONF_PRESET]))
  101. else:
  102. cg.add(action_var.set_operation("activate_group"))
  103. cg.add(action_var.set_group(config[CONF_GROUP]))
  104. yield action_var
  105. @coroutine
  106. def light_output_to_code(config):
  107. light_output_var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
  108. yield light.register_light(light_output_var, config)
  109. light_hal_var = yield cg.get_variable(config[CONF_LIGHT_HAL_ID])
  110. cg.add(light_output_var.set_parent(light_hal_var))
  111. @coroutine
  112. def on_brightness_to_code(config):
  113. light_output_var = yield cg.get_variable(config[CONF_OUTPUT_ID])
  114. for conf in config.get(CONF_ON_BRIGHTNESS, []):
  115. trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_output_var)
  116. yield automation.build_automation(trigger, [(float, "x")], conf)
  117. @coroutine
  118. def presets_to_code(config):
  119. light_state_var = yield cg.get_variable(config[CONF_ID])
  120. presets_var = cg.new_Pvariable(config[CONF_PRESETS_ID], light_state_var)
  121. yield cg.register_component(presets_var, config)
  122. for preset_group, presets in config.get(CONF_PRESETS, {}).items():
  123. for name, preset in presets.items():
  124. if CONF_COLOR_TEMPERATURE in preset:
  125. cg.add(presets_var.add(
  126. preset_group, name, preset[CONF_COLOR_TEMPERATURE]))
  127. else:
  128. cg.add(presets_var.add(
  129. preset_group, name, preset[CONF_RED], preset[CONF_GREEN], preset[CONF_BLUE]))
  130. def to_code(config):
  131. yield light_output_to_code(config)
  132. yield on_brightness_to_code(config)
  133. yield presets_to_code(config)