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.

189 lines
6.8 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. CONF_TRANSITION_LENGTH, CONF_BRIGHTNESS, CONF_EFFECT
  10. )
  11. from .. import bslamp2_ns, CODEOWNERS, CONF_LIGHT_HAL_ID, LightHAL
  12. AUTO_LOAD = ["xiaomi_bslamp2"]
  13. CONF_MASTER1 = "master1"
  14. CONF_MASTER2 = "master2"
  15. CONF_ON_BRIGHTNESS = "on_brightness"
  16. CONF_PRESET_ID = "preset_id"
  17. CONF_PRESETS_ID = "presets_id"
  18. CONF_PRESETS = "presets"
  19. CONF_NEXT = "next"
  20. CONF_GROUP = "group"
  21. CONF_PRESET = "preset"
  22. XiaomiBslamp2LightState = bslamp2_ns.class_("XiaomiBslamp2LightState", light.LightState)
  23. XiaomiBslamp2LightOutput = bslamp2_ns.class_("XiaomiBslamp2LightOutput", light.LightOutput)
  24. PresetsContainer = bslamp2_ns.class_("PresetsContainer", cg.Component)
  25. Preset = bslamp2_ns.class_("Preset", cg.Component)
  26. BrightnessTrigger = bslamp2_ns.class_("BrightnessTrigger", automation.Trigger.template())
  27. ActivatePresetAction = bslamp2_ns.class_("ActivatePresetAction", automation.Action)
  28. PRESETS_SCHEMA = cv.Schema({
  29. str.lower: cv.Schema({
  30. str.lower: light.automation.LIGHT_TURN_ON_ACTION_SCHEMA
  31. })
  32. })
  33. PRESET_SCHEMA_BASE = cv.Schema(
  34. {
  35. cv.GenerateID(CONF_ID): cv.use_id(XiaomiBslamp2LightState),
  36. cv.GenerateID(CONF_PRESET_ID): cv.declare_id(Preset),
  37. }
  38. )
  39. PRESET_SCHEMA = cv.Any(
  40. PRESET_SCHEMA_BASE.extend({
  41. cv.Required(CONF_EFFECT): cv.string
  42. }),
  43. PRESET_SCHEMA_BASE.extend({
  44. cv.Required(CONF_COLOR_TEMPERATURE): cv.color_temperature,
  45. cv.Optional(CONF_BRIGHTNESS): cv.percentage,
  46. cv.Optional(CONF_TRANSITION_LENGTH): cv.positive_time_period_milliseconds,
  47. }),
  48. PRESET_SCHEMA_BASE.extend({
  49. cv.Required(CONF_RED): cv.percentage,
  50. cv.Required(CONF_GREEN): cv.percentage,
  51. cv.Required(CONF_BLUE): cv.percentage,
  52. cv.Optional(CONF_BRIGHTNESS): cv.percentage,
  53. cv.Optional(CONF_TRANSITION_LENGTH): cv.positive_time_period_milliseconds,
  54. }),
  55. )
  56. CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
  57. {
  58. cv.GenerateID(CONF_ID): cv.declare_id(XiaomiBslamp2LightState),
  59. cv.GenerateID(CONF_LIGHT_HAL_ID): cv.use_id(LightHAL),
  60. cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(XiaomiBslamp2LightOutput),
  61. cv.Optional(CONF_ON_BRIGHTNESS): automation.validate_automation(
  62. {
  63. cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(BrightnessTrigger),
  64. }
  65. ),
  66. cv.GenerateID(CONF_PRESETS_ID): cv.declare_id(PresetsContainer),
  67. cv.Optional(CONF_PRESETS): cv.Schema({
  68. str.lower: cv.Schema({
  69. str.lower: PRESET_SCHEMA
  70. })
  71. }),
  72. }
  73. )
  74. def is_preset_group(value):
  75. return value
  76. def is_preset(value):
  77. return value
  78. def maybe_simple_preset_action(schema):
  79. def validator(value):
  80. if isinstance(value, dict):
  81. return schema(value)
  82. value = value.lower()
  83. conf = {}
  84. if value == "next_group":
  85. conf[CONF_NEXT] = CONF_GROUP
  86. elif value == "next_preset":
  87. conf[CONF_NEXT] = CONF_PRESET
  88. elif "." not in value:
  89. conf[CONF_GROUP] = value
  90. else:
  91. group, preset = value.split(".", 2)
  92. conf[CONF_GROUP] = group
  93. conf[CONF_PRESET] = preset
  94. return schema(conf)
  95. return validator
  96. @automation.register_action(
  97. "preset.activate",
  98. ActivatePresetAction,
  99. cv.Schema(
  100. maybe_simple_preset_action(cv.Any(
  101. cv.Schema({
  102. cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer),
  103. cv.Required(CONF_GROUP): is_preset_group,
  104. cv.Optional(CONF_PRESET): is_preset
  105. }),
  106. cv.Schema({
  107. cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer),
  108. cv.Required(CONF_NEXT): cv.one_of(CONF_GROUP, CONF_PRESET, lower=True)
  109. })
  110. ))
  111. )
  112. )
  113. def preset_activate_to_code(config, action_id, template_arg, args):
  114. presets_var = yield cg.get_variable(config[CONF_PRESETS_ID])
  115. action_var = cg.new_Pvariable(action_id, template_arg, presets_var)
  116. if CONF_NEXT in config:
  117. cg.add(action_var.set_operation(f"next_{config[CONF_NEXT]}"))
  118. elif CONF_PRESET in config:
  119. cg.add(action_var.set_operation("activate_preset"))
  120. cg.add(action_var.set_group(config[CONF_GROUP]))
  121. cg.add(action_var.set_preset(config[CONF_PRESET]))
  122. else:
  123. cg.add(action_var.set_operation("activate_group"))
  124. cg.add(action_var.set_group(config[CONF_GROUP]))
  125. yield action_var
  126. @coroutine
  127. def light_output_to_code(config):
  128. light_output_var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
  129. yield light.register_light(light_output_var, config)
  130. light_hal_var = yield cg.get_variable(config[CONF_LIGHT_HAL_ID])
  131. cg.add(light_output_var.set_parent(light_hal_var))
  132. @coroutine
  133. def on_brightness_to_code(config):
  134. light_output_var = yield cg.get_variable(config[CONF_OUTPUT_ID])
  135. for conf in config.get(CONF_ON_BRIGHTNESS, []):
  136. trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_output_var)
  137. yield automation.build_automation(trigger, [(float, "x")], conf)
  138. @coroutine
  139. def preset_to_code(config, preset_group, preset_name):
  140. light_var = yield cg.get_variable(config[CONF_ID])
  141. preset_var = cg.new_Pvariable(
  142. config[CONF_PRESET_ID], light_var, preset_group, preset_name)
  143. if CONF_TRANSITION_LENGTH in config:
  144. cg.add(preset_var.set_transition_length(config[CONF_TRANSITION_LENGTH]))
  145. if CONF_BRIGHTNESS in config:
  146. cg.add(preset_var.set_brightness(config[CONF_BRIGHTNESS]))
  147. if CONF_RED in config:
  148. cg.add(preset_var.set_red(config[CONF_RED]))
  149. if CONF_GREEN in config:
  150. cg.add(preset_var.set_green(config[CONF_GREEN]))
  151. if CONF_BLUE in config:
  152. cg.add(preset_var.set_blue(config[CONF_BLUE]))
  153. if CONF_COLOR_TEMPERATURE in config:
  154. cg.add(preset_var.set_color_temperature(config[CONF_COLOR_TEMPERATURE]))
  155. if CONF_EFFECT in config:
  156. cg.add(preset_var.set_effect(config[CONF_EFFECT]))
  157. else:
  158. cg.add(preset_var.set_effect("None"))
  159. yield cg.register_component(preset_var, config)
  160. @coroutine
  161. def presets_to_code(config):
  162. presets_var = cg.new_Pvariable(config[CONF_PRESETS_ID])
  163. yield cg.register_component(presets_var, config)
  164. for preset_group, presets in config.get(CONF_PRESETS, {}).items():
  165. for preset_name, preset_config in presets.items():
  166. preset = yield preset_to_code(preset_config, preset_group, preset_name)
  167. cg.add(presets_var.add_preset(preset))
  168. def to_code(config):
  169. yield light_output_to_code(config)
  170. yield on_brightness_to_code(config)
  171. yield presets_to_code(config)