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.

70 lines
2.6 KiB

  1. from esphome.cpp_generator import RawExpression
  2. from esphome import automation
  3. import esphome.codegen as cg
  4. import esphome.config_validation as cv
  5. from esphome.components.ota import ota_ns, OTAComponent
  6. from esphome.const import (
  7. CONF_ID,
  8. )
  9. CODEOWNERS = ["@mmakaay"]
  10. DEPENDENCIES = ["ota"]
  11. CONF_ON_BEGIN = "on_begin"
  12. CONF_ON_PROGRESS = "on_progress"
  13. CONF_ON_END = "on_end"
  14. CONF_ON_ERROR = "on_error"
  15. CONF_ON_BEGIN_TRIGGER_ID = "on_begin_trigger_id"
  16. CONF_ON_PROGRESS_TRIGGER_ID = "on_progress_trigger_id"
  17. CONF_ON_END_TRIGGER_ID = "on_end_trigger_id"
  18. CONF_ON_ERROR_TRIGGER_ID = "on_error_trigger_id"
  19. OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template())
  20. OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template())
  21. OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template())
  22. OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template())
  23. CONFIG_SCHEMA = cv.Schema(
  24. {
  25. cv.GenerateID(): cv.use_id(OTAComponent),
  26. cv.Optional(CONF_ON_BEGIN): automation.validate_automation(
  27. {
  28. cv.GenerateID(CONF_ON_BEGIN_TRIGGER_ID): cv.declare_id(OTAStartTrigger),
  29. }
  30. ),
  31. cv.Optional(CONF_ON_ERROR): automation.validate_automation(
  32. {
  33. cv.GenerateID(CONF_ON_ERROR_TRIGGER_ID): cv.declare_id(OTAErrorTrigger),
  34. }
  35. ),
  36. cv.Optional(CONF_ON_PROGRESS): automation.validate_automation(
  37. {
  38. cv.GenerateID(CONF_ON_PROGRESS_TRIGGER_ID): cv.declare_id(
  39. OTAProgressTrigger
  40. ),
  41. }
  42. ),
  43. cv.Optional(CONF_ON_END): automation.validate_automation(
  44. {
  45. cv.GenerateID(CONF_ON_END_TRIGGER_ID): cv.declare_id(OTAEndTrigger),
  46. }
  47. ),
  48. }
  49. ).extend(cv.COMPONENT_SCHEMA)
  50. async def to_code(config):
  51. var = await cg.get_variable(config[CONF_ID])
  52. for conf in config.get(CONF_ON_BEGIN, []):
  53. trigger = cg.new_Pvariable(conf[CONF_ON_BEGIN_TRIGGER_ID], var)
  54. await automation.build_automation(trigger, [], conf)
  55. for conf in config.get(CONF_ON_PROGRESS, []):
  56. trigger = cg.new_Pvariable(conf[CONF_ON_PROGRESS_TRIGGER_ID], var)
  57. await automation.build_automation(trigger, [(float, "x")], conf)
  58. for conf in config.get(CONF_ON_END, []):
  59. trigger = cg.new_Pvariable(conf[CONF_ON_END_TRIGGER_ID], var)
  60. await automation.build_automation(trigger, [], conf)
  61. for conf in config.get(CONF_ON_ERROR, []):
  62. trigger = cg.new_Pvariable(conf[CONF_ON_ERROR_TRIGGER_ID], var)
  63. await automation.build_automation(trigger, [(int, "x")], conf)