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.

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