notification.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. """Pydantic schemas for notification providers."""
  2. from datetime import datetime
  3. from typing import Any
  4. from pydantic import BaseModel, Field, field_validator
  5. from backend.app.core.compat import StrEnum
  6. class ProviderType(StrEnum):
  7. """Supported notification provider types."""
  8. CALLMEBOT = "callmebot"
  9. NTFY = "ntfy"
  10. PUSHOVER = "pushover"
  11. TELEGRAM = "telegram"
  12. EMAIL = "email"
  13. DISCORD = "discord"
  14. WEBHOOK = "webhook"
  15. HOMEASSISTANT = "homeassistant"
  16. class NotificationProviderBase(BaseModel):
  17. """Base schema for notification providers."""
  18. name: str = Field(..., min_length=1, max_length=100, description="User-defined name")
  19. provider_type: ProviderType = Field(..., description="Type of notification provider")
  20. enabled: bool = Field(default=True, description="Whether notifications are enabled")
  21. config: dict[str, Any] = Field(..., description="Provider-specific configuration")
  22. # Event triggers - print lifecycle
  23. on_print_start: bool = Field(default=False, description="Notify on print start")
  24. on_print_complete: bool = Field(default=True, description="Notify on print complete")
  25. on_print_failed: bool = Field(default=True, description="Notify on print failed")
  26. on_print_stopped: bool = Field(default=True, description="Notify when print is stopped/cancelled")
  27. on_print_progress: bool = Field(default=False, description="Notify at 25%, 50%, 75% progress")
  28. on_print_missing_spool_assignment: bool = Field(
  29. default=False,
  30. description="Notify when a print starts with required trays missing spool assignments",
  31. )
  32. # Event triggers - printer status
  33. on_printer_offline: bool = Field(default=False, description="Notify when printer goes offline")
  34. on_printer_error: bool = Field(default=False, description="Notify on printer errors (AMS, etc.)")
  35. on_filament_low: bool = Field(default=False, description="Notify when filament is running low")
  36. on_maintenance_due: bool = Field(default=False, description="Notify when maintenance is due")
  37. # Event triggers - AMS environmental alarms (regular AMS)
  38. on_ams_humidity_high: bool = Field(default=False, description="Notify when AMS humidity exceeds threshold")
  39. on_ams_temperature_high: bool = Field(default=False, description="Notify when AMS temperature exceeds threshold")
  40. # Event triggers - AMS-HT environmental alarms
  41. on_ams_ht_humidity_high: bool = Field(default=False, description="Notify when AMS-HT humidity exceeds threshold")
  42. on_ams_ht_temperature_high: bool = Field(
  43. default=False, description="Notify when AMS-HT temperature exceeds threshold"
  44. )
  45. # Event triggers - Build plate detection
  46. on_plate_not_empty: bool = Field(default=True, description="Notify when objects detected on plate before print")
  47. # Event triggers - Bed cooled
  48. on_bed_cooled: bool = Field(default=False, description="Notify when bed cools after print")
  49. # Event triggers - First layer complete
  50. on_first_layer_complete: bool = Field(default=False, description="Notify when first layer completes")
  51. # Event triggers - Print queue
  52. on_queue_job_added: bool = Field(default=False, description="Notify when job is added to queue")
  53. on_queue_job_assigned: bool = Field(default=False, description="Notify when model-based job is assigned to printer")
  54. on_queue_job_started: bool = Field(default=False, description="Notify when queue job starts printing")
  55. on_queue_job_waiting: bool = Field(default=True, description="Notify when job is waiting for filament or printer")
  56. on_queue_job_skipped: bool = Field(default=True, description="Notify when job is skipped")
  57. on_queue_job_failed: bool = Field(default=True, description="Notify when job fails to start")
  58. on_queue_completed: bool = Field(default=False, description="Notify when all queue jobs finish")
  59. # Quiet hours
  60. quiet_hours_enabled: bool = Field(default=False, description="Enable quiet hours")
  61. quiet_hours_start: str | None = Field(default=None, description="Start time in HH:MM format")
  62. quiet_hours_end: str | None = Field(default=None, description="End time in HH:MM format")
  63. # Daily digest
  64. daily_digest_enabled: bool = Field(default=False, description="Batch notifications into daily digest")
  65. daily_digest_time: str | None = Field(default=None, description="Time to send digest in HH:MM format")
  66. # Printer filter
  67. printer_id: int | None = Field(default=None, description="Specific printer ID or null for all")
  68. @field_validator("quiet_hours_start", "quiet_hours_end", "daily_digest_time")
  69. @classmethod
  70. def validate_time_format(cls, v: str | None) -> str | None:
  71. if v is None:
  72. return v
  73. try:
  74. parts = v.split(":")
  75. if len(parts) != 2:
  76. raise ValueError("Invalid time format")
  77. hour, minute = int(parts[0]), int(parts[1])
  78. if not (0 <= hour <= 23 and 0 <= minute <= 59):
  79. raise ValueError("Invalid time range")
  80. return f"{hour:02d}:{minute:02d}"
  81. except (ValueError, TypeError):
  82. raise ValueError("Time must be in HH:MM format (e.g., 22:00)")
  83. class NotificationProviderCreate(NotificationProviderBase):
  84. """Schema for creating a notification provider."""
  85. pass
  86. class NotificationProviderUpdate(BaseModel):
  87. """Schema for updating a notification provider (all fields optional)."""
  88. name: str | None = Field(default=None, min_length=1, max_length=100)
  89. provider_type: ProviderType | None = None
  90. enabled: bool | None = None
  91. config: dict[str, Any] | None = None
  92. # Event triggers - print lifecycle
  93. on_print_start: bool | None = None
  94. on_print_complete: bool | None = None
  95. on_print_failed: bool | None = None
  96. on_print_stopped: bool | None = None
  97. on_print_progress: bool | None = None
  98. on_print_missing_spool_assignment: bool | None = None
  99. # Event triggers - printer status
  100. on_printer_offline: bool | None = None
  101. on_printer_error: bool | None = None
  102. on_filament_low: bool | None = None
  103. on_maintenance_due: bool | None = None
  104. # Event triggers - AMS environmental alarms (regular AMS)
  105. on_ams_humidity_high: bool | None = None
  106. on_ams_temperature_high: bool | None = None
  107. # Event triggers - AMS-HT environmental alarms
  108. on_ams_ht_humidity_high: bool | None = None
  109. on_ams_ht_temperature_high: bool | None = None
  110. # Event triggers - Build plate detection
  111. on_plate_not_empty: bool | None = None
  112. # Event triggers - Bed cooled
  113. on_bed_cooled: bool | None = None
  114. # Event triggers - First layer complete
  115. on_first_layer_complete: bool | None = None
  116. # Event triggers - Print queue
  117. on_queue_job_added: bool | None = None
  118. on_queue_job_assigned: bool | None = None
  119. on_queue_job_started: bool | None = None
  120. on_queue_job_waiting: bool | None = None
  121. on_queue_job_skipped: bool | None = None
  122. on_queue_job_failed: bool | None = None
  123. on_queue_completed: bool | None = None
  124. # Quiet hours
  125. quiet_hours_enabled: bool | None = None
  126. quiet_hours_start: str | None = None
  127. quiet_hours_end: str | None = None
  128. # Daily digest
  129. daily_digest_enabled: bool | None = None
  130. daily_digest_time: str | None = None
  131. # Printer filter
  132. printer_id: int | None = None
  133. class NotificationProviderResponse(NotificationProviderBase):
  134. """Schema for notification provider API responses."""
  135. id: int
  136. last_success: datetime | None = None
  137. last_error: str | None = None
  138. last_error_at: datetime | None = None
  139. created_at: datetime
  140. updated_at: datetime
  141. class Config:
  142. from_attributes = True
  143. class NotificationTestRequest(BaseModel):
  144. """Schema for testing notification configuration."""
  145. provider_type: ProviderType
  146. config: dict[str, Any]
  147. class NotificationTestResponse(BaseModel):
  148. """Schema for test notification response."""
  149. success: bool
  150. message: str
  151. # Provider-specific config schemas for documentation/validation reference
  152. class CallMeBotConfig(BaseModel):
  153. """CallMeBot/WhatsApp configuration."""
  154. phone: str = Field(..., description="Phone number with country code (e.g., +1234567890)")
  155. apikey: str = Field(..., description="API key from CallMeBot")
  156. class NtfyConfig(BaseModel):
  157. """ntfy configuration."""
  158. server: str = Field(default="https://ntfy.sh", description="ntfy server URL")
  159. topic: str = Field(..., description="Topic name to publish to")
  160. auth_token: str | None = Field(default=None, description="Optional authentication token")
  161. class PushoverConfig(BaseModel):
  162. """Pushover configuration."""
  163. user_key: str = Field(..., description="Your Pushover user key")
  164. app_token: str = Field(..., description="Your Pushover application token")
  165. priority: int = Field(default=0, ge=-2, le=2, description="Message priority (-2 to 2)")
  166. class TelegramConfig(BaseModel):
  167. """Telegram bot configuration."""
  168. bot_token: str = Field(..., description="Bot token from @BotFather")
  169. chat_id: str = Field(..., description="Chat ID to send messages to")
  170. class EmailConfig(BaseModel):
  171. """Email/SMTP configuration."""
  172. smtp_server: str = Field(..., description="SMTP server hostname")
  173. smtp_port: int = Field(default=587, description="SMTP port (587 for TLS, 465 for SSL)")
  174. username: str = Field(..., description="SMTP username/email")
  175. password: str = Field(..., description="SMTP password or app password")
  176. from_email: str = Field(..., description="From email address")
  177. to_email: str = Field(..., description="Recipient email address")
  178. use_tls: bool = Field(default=True, description="Use TLS encryption")
  179. # Notification Log schemas
  180. class NotificationLogResponse(BaseModel):
  181. """Schema for notification log API responses."""
  182. id: int
  183. provider_id: int
  184. provider_name: str | None = None
  185. provider_type: str | None = None
  186. event_type: str
  187. title: str
  188. message: str
  189. success: bool
  190. error_message: str | None = None
  191. printer_id: int | None = None
  192. printer_name: str | None = None
  193. created_at: datetime
  194. class Config:
  195. from_attributes = True
  196. class NotificationLogStats(BaseModel):
  197. """Statistics for notification logs."""
  198. total: int
  199. success_count: int
  200. failure_count: int
  201. by_event_type: dict[str, int]
  202. by_provider: dict[str, int]