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.

257 lines
9.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_STATE, CONF_OUTPUT_ID, CONF_TRIGGER_ID, CONF_ID,
  9. CONF_TRANSITION_LENGTH, CONF_BRIGHTNESS, CONF_EFFECT, CONF_FLASH_LENGTH
  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. MIRED_MIN = 153
  23. MIRED_MAX = 588
  24. XiaomiBslamp2LightState = bslamp2_ns.class_("XiaomiBslamp2LightState", light.LightState)
  25. XiaomiBslamp2LightOutput = bslamp2_ns.class_("XiaomiBslamp2LightOutput", light.LightOutput)
  26. PresetsContainer = bslamp2_ns.class_("PresetsContainer", cg.Component)
  27. Preset = bslamp2_ns.class_("Preset", cg.Component)
  28. BrightnessTrigger = bslamp2_ns.class_("BrightnessTrigger", automation.Trigger.template())
  29. ActivatePresetAction = bslamp2_ns.class_("ActivatePresetAction", automation.Action)
  30. DiscoAction = bslamp2_ns.class_("DiscoAction", automation.Action)
  31. PRESETS_SCHEMA = cv.Schema({
  32. str.lower: cv.Schema({
  33. str.lower: light.automation.LIGHT_TURN_ON_ACTION_SCHEMA
  34. })
  35. })
  36. def validate_preset(conf):
  37. has_rgb = CONF_RED in conf or CONF_GREEN in conf or CONF_BLUE in conf
  38. has_white = CONF_COLOR_TEMPERATURE in conf
  39. has_effect = CONF_EFFECT in conf
  40. # Check mutual exclusivity of preset options.
  41. if (has_rgb + has_white + has_effect) > 1:
  42. raise cv.Invalid("Use only one of RGB light, white (color temperature) light or an effect")
  43. # Check the color temperature value range.
  44. if has_white:
  45. if conf[CONF_COLOR_TEMPERATURE] < MIRED_MIN or conf[CONF_COLOR_TEMPERATURE] > MIRED_MAX:
  46. raise cv.Invalid(f"The color temperature must be in the range {MIRED_MIN} - {MIRED_MAX}")
  47. # When defining an RGB color, it is allowed to omit RGB components that have value 0.
  48. if has_rgb:
  49. if CONF_RED not in conf:
  50. conf[CONF_RED] = 0
  51. if CONF_GREEN not in conf:
  52. conf[CONF_GREEN] = 0
  53. if CONF_BLUE not in conf:
  54. conf[CONF_BLUE] = 0
  55. return conf
  56. PRESET_SCHEMA = cv.All(
  57. cv.Schema(
  58. {
  59. cv.GenerateID(CONF_ID): cv.use_id(XiaomiBslamp2LightState),
  60. cv.GenerateID(CONF_PRESET_ID): cv.declare_id(Preset),
  61. cv.Optional(CONF_EFFECT): cv.string,
  62. cv.Optional(CONF_COLOR_TEMPERATURE): cv.color_temperature,
  63. cv.Optional(CONF_RED): cv.percentage,
  64. cv.Optional(CONF_GREEN): cv.percentage,
  65. cv.Optional(CONF_BLUE): cv.percentage,
  66. cv.Optional(CONF_BRIGHTNESS): cv.percentage,
  67. cv.Optional(CONF_TRANSITION_LENGTH): cv.positive_time_period_milliseconds,
  68. }
  69. ),
  70. validate_preset
  71. )
  72. CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
  73. {
  74. cv.GenerateID(CONF_ID): cv.declare_id(XiaomiBslamp2LightState),
  75. cv.GenerateID(CONF_LIGHT_HAL_ID): cv.use_id(LightHAL),
  76. cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(XiaomiBslamp2LightOutput),
  77. cv.Optional(CONF_ON_BRIGHTNESS): automation.validate_automation(
  78. {
  79. cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(BrightnessTrigger),
  80. }
  81. ),
  82. cv.GenerateID(CONF_PRESETS_ID): cv.declare_id(PresetsContainer),
  83. cv.Optional(CONF_PRESETS): cv.Schema({
  84. str.lower: cv.Schema({
  85. str.lower: PRESET_SCHEMA
  86. })
  87. }),
  88. }
  89. )
  90. def is_preset_group(value):
  91. return value
  92. def is_preset(value):
  93. return value
  94. def maybe_simple_preset_action(schema):
  95. def validator(value):
  96. if isinstance(value, dict):
  97. return schema(value)
  98. value = value.lower()
  99. conf = {}
  100. if value == "next_group":
  101. conf[CONF_NEXT] = CONF_GROUP
  102. elif value == "next_preset":
  103. conf[CONF_NEXT] = CONF_PRESET
  104. elif "." not in value:
  105. conf[CONF_GROUP] = value
  106. else:
  107. group, preset = value.split(".", 2)
  108. conf[CONF_GROUP] = group
  109. conf[CONF_PRESET] = preset
  110. return schema(conf)
  111. return validator
  112. @automation.register_action(
  113. "light.disco_on", DiscoAction, light.automation.LIGHT_TURN_ON_ACTION_SCHEMA
  114. )
  115. def disco_action_on_to_code(config, action_id, template_arg, args):
  116. light_var = yield cg.get_variable(config[CONF_ID])
  117. var = cg.new_Pvariable(action_id, template_arg, light_var)
  118. if CONF_STATE in config:
  119. template_ = yield cg.templatable(config[CONF_STATE], args, bool)
  120. cg.add(var.set_state(template_))
  121. if CONF_TRANSITION_LENGTH in config:
  122. template_ = yield cg.templatable(
  123. config[CONF_TRANSITION_LENGTH], args, cg.uint32
  124. )
  125. cg.add(var.set_transition_length(template_))
  126. if CONF_FLASH_LENGTH in config:
  127. template_ = yield cg.templatable(config[CONF_FLASH_LENGTH], args, cg.uint32)
  128. cg.add(var.set_flash_length(template_))
  129. if CONF_BRIGHTNESS in config:
  130. template_ = yield cg.templatable(config[CONF_BRIGHTNESS], args, float)
  131. cg.add(var.set_brightness(template_))
  132. if CONF_RED in config:
  133. template_ = yield cg.templatable(config[CONF_RED], args, float)
  134. cg.add(var.set_red(template_))
  135. if CONF_GREEN in config:
  136. template_ = yield cg.templatable(config[CONF_GREEN], args, float)
  137. cg.add(var.set_green(template_))
  138. if CONF_BLUE in config:
  139. template_ = yield cg.templatable(config[CONF_BLUE], args, float)
  140. cg.add(var.set_blue(template_))
  141. if CONF_COLOR_TEMPERATURE in config:
  142. template_ = yield cg.templatable(config[CONF_COLOR_TEMPERATURE], args, float)
  143. cg.add(var.set_color_temperature(template_))
  144. if CONF_EFFECT in config:
  145. template_ = yield cg.templatable(config[CONF_EFFECT], args, cg.std_string)
  146. cg.add(var.set_effect(template_))
  147. yield var
  148. @automation.register_action(
  149. "light.disco_off", DiscoAction, light.automation.LIGHT_TURN_OFF_ACTION_SCHEMA
  150. )
  151. def disco_action_off_to_code(config, action_id, template_arg, args):
  152. light_var = yield cg.get_variable(config[CONF_ID])
  153. var = cg.new_Pvariable(action_id, template_arg, light_var)
  154. cg.add(var.set_disco_state(False))
  155. yield var
  156. @automation.register_action(
  157. "preset.activate",
  158. ActivatePresetAction,
  159. cv.Schema(
  160. maybe_simple_preset_action(cv.Any(
  161. cv.Schema({
  162. cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer),
  163. cv.Required(CONF_GROUP): is_preset_group,
  164. cv.Optional(CONF_PRESET): is_preset
  165. }),
  166. cv.Schema({
  167. cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer),
  168. cv.Required(CONF_NEXT): cv.one_of(CONF_GROUP, CONF_PRESET, lower=True)
  169. })
  170. ))
  171. )
  172. )
  173. def preset_activate_to_code(config, action_id, template_arg, args):
  174. presets_var = yield cg.get_variable(config[CONF_PRESETS_ID])
  175. action_var = cg.new_Pvariable(action_id, template_arg, presets_var)
  176. if CONF_NEXT in config:
  177. cg.add(action_var.set_operation(f"next_{config[CONF_NEXT]}"))
  178. elif CONF_PRESET in config:
  179. cg.add(action_var.set_operation("activate_preset"))
  180. cg.add(action_var.set_group(config[CONF_GROUP]))
  181. cg.add(action_var.set_preset(config[CONF_PRESET]))
  182. else:
  183. cg.add(action_var.set_operation("activate_group"))
  184. cg.add(action_var.set_group(config[CONF_GROUP]))
  185. yield action_var
  186. @coroutine
  187. def light_output_to_code(config):
  188. light_output_var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
  189. yield light.register_light(light_output_var, config)
  190. light_hal_var = yield cg.get_variable(config[CONF_LIGHT_HAL_ID])
  191. cg.add(light_output_var.set_parent(light_hal_var))
  192. @coroutine
  193. def on_brightness_to_code(config):
  194. light_output_var = yield cg.get_variable(config[CONF_OUTPUT_ID])
  195. for conf in config.get(CONF_ON_BRIGHTNESS, []):
  196. trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_output_var)
  197. yield automation.build_automation(trigger, [(float, "x")], conf)
  198. @coroutine
  199. def preset_to_code(config, preset_group, preset_name):
  200. light_var = yield cg.get_variable(config[CONF_ID])
  201. preset_var = cg.new_Pvariable(
  202. config[CONF_PRESET_ID], light_var, preset_group, preset_name)
  203. if CONF_TRANSITION_LENGTH in config:
  204. cg.add(preset_var.set_transition_length(config[CONF_TRANSITION_LENGTH]))
  205. if CONF_BRIGHTNESS in config:
  206. cg.add(preset_var.set_brightness(config[CONF_BRIGHTNESS]))
  207. if CONF_RED in config:
  208. cg.add(preset_var.set_red(config[CONF_RED]))
  209. if CONF_GREEN in config:
  210. cg.add(preset_var.set_green(config[CONF_GREEN]))
  211. if CONF_BLUE in config:
  212. cg.add(preset_var.set_blue(config[CONF_BLUE]))
  213. if CONF_COLOR_TEMPERATURE in config:
  214. cg.add(preset_var.set_color_temperature(config[CONF_COLOR_TEMPERATURE]))
  215. if CONF_EFFECT in config:
  216. cg.add(preset_var.set_effect(config[CONF_EFFECT]))
  217. else:
  218. cg.add(preset_var.set_effect("None"))
  219. yield cg.register_component(preset_var, config)
  220. @coroutine
  221. def presets_to_code(config):
  222. presets_var = cg.new_Pvariable(config[CONF_PRESETS_ID])
  223. yield cg.register_component(presets_var, config)
  224. for preset_group, presets in config.get(CONF_PRESETS, {}).items():
  225. for preset_name, preset_config in presets.items():
  226. preset = yield preset_to_code(preset_config, preset_group, preset_name)
  227. cg.add(presets_var.add_preset(preset))
  228. def to_code(config):
  229. yield light_output_to_code(config)
  230. yield on_brightness_to_code(config)
  231. yield presets_to_code(config)