settings.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import json
  2. from pydantic import BaseModel, Field, field_validator
  3. class AppSettings(BaseModel):
  4. """Application settings schema."""
  5. auto_archive: bool = Field(default=True, description="Automatically archive prints when completed")
  6. save_thumbnails: bool = Field(default=True, description="Extract and save preview images from 3MF files")
  7. capture_finish_photo: bool = Field(
  8. default=True, description="Capture photo from printer camera when print completes"
  9. )
  10. default_filament_cost: float = Field(default=25.0, description="Default filament cost per kg")
  11. currency: str = Field(default="USD", description="Currency for cost tracking")
  12. energy_cost_per_kwh: float = Field(default=0.15, description="Electricity cost per kWh for energy tracking")
  13. energy_tracking_mode: str = Field(
  14. default="total",
  15. description="Energy display mode on stats: 'print' shows sum of per-print energy, 'total' shows lifetime plug consumption",
  16. )
  17. # Spoolman integration
  18. spoolman_enabled: bool = Field(default=False, description="Enable Spoolman integration for filament tracking")
  19. spoolman_url: str = Field(default="", description="Spoolman server URL (e.g., http://localhost:7912)")
  20. spoolman_sync_mode: str = Field(
  21. default="auto", description="Sync mode: 'auto' syncs immediately, 'manual' requires button press"
  22. )
  23. spoolman_disable_weight_sync: bool = Field(
  24. default=False,
  25. description="Disable remaining_weight sync. When enabled, only location is updated for existing spools.",
  26. )
  27. spoolman_report_partial_usage: bool = Field(
  28. default=True,
  29. description="Report Partial Usage for Failed Prints. When a print fails or is cancelled, report the estimated filament used up to that point based on layer progress.",
  30. )
  31. disable_filament_warnings: bool = Field(
  32. default=False,
  33. description="Disable insufficient filament warnings when printing or queueing prints",
  34. )
  35. # Updates
  36. check_updates: bool = Field(default=True, description="Automatically check for updates on startup")
  37. check_printer_firmware: bool = Field(default=True, description="Check for printer firmware updates from Bambu Lab")
  38. include_beta_updates: bool = Field(default=False, description="Include beta/prerelease versions in update checks")
  39. # Language
  40. language: str = Field(default="en", description="UI language (en, de, fr, ja, it, pt-BR)")
  41. notification_language: str = Field(default="en", description="Language for push notifications (en, de)")
  42. # Bed cooled notification threshold
  43. bed_cooled_threshold: float = Field(
  44. default=35.0, description="Bed temperature threshold for cooled notification (°C)"
  45. )
  46. # AMS threshold settings for humidity and temperature coloring
  47. ams_humidity_good: int = Field(default=40, description="Humidity threshold for good (green): <= this value")
  48. ams_humidity_fair: int = Field(
  49. default=60, description="Humidity threshold for fair (orange): <= this value, > is red"
  50. )
  51. ams_temp_good: float = Field(default=28.0, description="Temperature threshold for good (blue): <= this value")
  52. ams_temp_fair: float = Field(
  53. default=35.0, description="Temperature threshold for fair (orange): <= this value, > is red"
  54. )
  55. ams_history_retention_days: int = Field(default=30, description="Number of days to keep AMS sensor history data")
  56. # Queue auto-drying settings
  57. queue_drying_enabled: bool = Field(
  58. default=False, description="Automatically dry AMS filament between queued prints"
  59. )
  60. queue_drying_block: bool = Field(
  61. default=False,
  62. description="Block queue until drying completes (when disabled, prints take priority over drying)",
  63. )
  64. ambient_drying_enabled: bool = Field(
  65. default=False,
  66. description="Automatically dry AMS filament on idle printers when humidity exceeds threshold, regardless of queue",
  67. )
  68. drying_presets: str = Field(
  69. default="",
  70. description="JSON blob of drying presets per filament type (empty = use built-in defaults)",
  71. )
  72. # Print modal settings
  73. per_printer_mapping_expanded: bool = Field(
  74. default=False, description="Expand custom filament mapping by default in print modal"
  75. )
  76. # Date/time display format
  77. date_format: str = Field(default="system", description="Date format: system, us, eu, iso")
  78. time_format: str = Field(default="system", description="Time format: system, 12h, 24h")
  79. # Default printer for operations
  80. default_printer_id: int | None = Field(default=None, description="Default printer ID for uploads, reprints, etc.")
  81. # Virtual Printer
  82. virtual_printer_enabled: bool = Field(default=False, description="Enable virtual printer for slicer uploads")
  83. virtual_printer_access_code: str = Field(default="", description="Access code for virtual printer authentication")
  84. virtual_printer_mode: str = Field(
  85. default="immediate",
  86. description="Mode: 'immediate' (archive now), 'review' (pending review), or 'print_queue' (add to print queue)",
  87. )
  88. # Dark mode theme settings
  89. dark_style: str = Field(default="classic", description="Dark mode style: classic, glow, vibrant")
  90. dark_background: str = Field(
  91. default="neutral", description="Dark mode background: neutral, warm, cool, oled, slate, forest"
  92. )
  93. dark_accent: str = Field(default="green", description="Dark mode accent: green, teal, blue, orange, purple, red")
  94. # Light mode theme settings
  95. light_style: str = Field(default="classic", description="Light mode style: classic, glow, vibrant")
  96. light_background: str = Field(default="neutral", description="Light mode background: neutral, warm, cool")
  97. light_accent: str = Field(default="green", description="Light mode accent: green, teal, blue, orange, purple, red")
  98. # FTP retry settings for unreliable WiFi connections
  99. ftp_retry_enabled: bool = Field(default=True, description="Enable automatic retry for FTP operations")
  100. ftp_retry_count: int = Field(default=3, description="Number of retry attempts for FTP operations (1-10)")
  101. ftp_retry_delay: int = Field(default=2, description="Seconds to wait between FTP retry attempts (1-30)")
  102. ftp_timeout: int = Field(default=30, description="FTP connection timeout in seconds (10-300)")
  103. # MQTT Relay settings for publishing events to external broker
  104. mqtt_enabled: bool = Field(default=False, description="Enable MQTT event publishing to external broker")
  105. mqtt_broker: str = Field(default="", description="MQTT broker hostname or IP address")
  106. mqtt_port: int = Field(default=1883, description="MQTT broker port (default 1883, TLS typically 8883)")
  107. mqtt_username: str = Field(default="", description="MQTT username for authentication (optional)")
  108. mqtt_password: str = Field(default="", description="MQTT password for authentication (optional)")
  109. mqtt_topic_prefix: str = Field(default="bambuddy", description="Topic prefix for all published messages")
  110. mqtt_use_tls: bool = Field(default=False, description="Use TLS/SSL encryption for MQTT connection")
  111. # External URL for notifications
  112. external_url: str = Field(
  113. default="", description="External URL where Bambuddy is accessible (for notification images)"
  114. )
  115. # Home Assistant integration for smart plug control
  116. ha_enabled: bool = Field(default=False, description="Enable Home Assistant integration for smart plug control")
  117. ha_url: str = Field(default="", description="Home Assistant URL (e.g., http://192.168.1.100:8123)")
  118. ha_token: str = Field(default="", description="Home Assistant Long-Lived Access Token")
  119. ha_url_from_env: bool = Field(default=False, description="Whether HA URL is set via HA_URL environment variable")
  120. ha_token_from_env: bool = Field(
  121. default=False, description="Whether HA token is set via HA_TOKEN environment variable"
  122. )
  123. ha_env_managed: bool = Field(
  124. default=False, description="Whether HA integration is fully managed by environment variables"
  125. )
  126. # File Manager / Library settings
  127. library_archive_mode: str = Field(
  128. default="ask",
  129. description="When printing from File Manager, create archive entry: 'always', 'never', or 'ask'",
  130. )
  131. library_disk_warning_gb: float = Field(
  132. default=5.0,
  133. description="Show warning when free disk space falls below this threshold (GB)",
  134. )
  135. # Camera view settings
  136. camera_view_mode: str = Field(
  137. default="window",
  138. description="Camera view mode: 'window' opens in new browser window, 'embedded' shows overlay on main screen",
  139. )
  140. # Preferred slicer application
  141. preferred_slicer: str = Field(
  142. default="bambu_studio",
  143. description="Preferred slicer: 'bambu_studio' or 'orcaslicer'",
  144. )
  145. # Prometheus metrics endpoint
  146. prometheus_enabled: bool = Field(default=False, description="Enable Prometheus metrics endpoint at /metrics")
  147. prometheus_token: str = Field(
  148. default="", description="Bearer token for Prometheus metrics authentication (optional)"
  149. )
  150. # Inventory low stock threshold
  151. low_stock_threshold: float = Field(
  152. default=20.0,
  153. ge=0.1,
  154. le=99.9,
  155. description="Low stock threshold percentage (%) for inventory filtering and display",
  156. )
  157. # User email notifications (requires Advanced Authentication)
  158. user_notifications_enabled: bool = Field(
  159. default=True,
  160. description="Enable user email notifications for print job events (requires Advanced Authentication)",
  161. )
  162. # Staggered batch start for multi-printer jobs
  163. stagger_group_size: int = Field(
  164. default=2, ge=1, le=50, description="Number of printers to start simultaneously in staggered mode"
  165. )
  166. stagger_interval_minutes: int = Field(
  167. default=5, ge=1, le=60, description="Minutes between staggered printer groups"
  168. )
  169. # Plate-clear confirmation for queue scheduling
  170. require_plate_clear: bool = Field(
  171. default=True,
  172. description="Require per-printer plate-clear confirmation before starting queued prints on finished printers",
  173. )
  174. # Default sidebar order (admin-set for all users)
  175. default_sidebar_order: str = Field(
  176. default="",
  177. description="JSON object with 'order' key containing array of sidebar item IDs (empty = no default)",
  178. )
  179. class AppSettingsUpdate(BaseModel):
  180. """Schema for updating settings (all fields optional)."""
  181. auto_archive: bool | None = None
  182. save_thumbnails: bool | None = None
  183. capture_finish_photo: bool | None = None
  184. default_filament_cost: float | None = None
  185. currency: str | None = None
  186. energy_cost_per_kwh: float | None = None
  187. energy_tracking_mode: str | None = None
  188. spoolman_enabled: bool | None = None
  189. spoolman_url: str | None = None
  190. spoolman_sync_mode: str | None = None
  191. spoolman_disable_weight_sync: bool | None = None
  192. spoolman_report_partial_usage: bool | None = None
  193. disable_filament_warnings: bool | None = None
  194. check_updates: bool | None = None
  195. check_printer_firmware: bool | None = None
  196. include_beta_updates: bool | None = None
  197. language: str | None = None
  198. notification_language: str | None = None
  199. bed_cooled_threshold: float | None = None
  200. ams_humidity_good: int | None = None
  201. ams_humidity_fair: int | None = None
  202. ams_temp_good: float | None = None
  203. ams_temp_fair: float | None = None
  204. ams_history_retention_days: int | None = None
  205. queue_drying_enabled: bool | None = None
  206. queue_drying_block: bool | None = None
  207. ambient_drying_enabled: bool | None = None
  208. drying_presets: str | None = None
  209. per_printer_mapping_expanded: bool | None = None
  210. date_format: str | None = None
  211. time_format: str | None = None
  212. default_printer_id: int | None = None
  213. virtual_printer_enabled: bool | None = None
  214. virtual_printer_access_code: str | None = None
  215. virtual_printer_mode: str | None = None
  216. dark_style: str | None = None
  217. dark_background: str | None = None
  218. dark_accent: str | None = None
  219. light_style: str | None = None
  220. light_background: str | None = None
  221. light_accent: str | None = None
  222. ftp_retry_enabled: bool | None = None
  223. ftp_retry_count: int | None = None
  224. ftp_retry_delay: int | None = None
  225. ftp_timeout: int | None = None
  226. mqtt_enabled: bool | None = None
  227. mqtt_broker: str | None = None
  228. mqtt_port: int | None = None
  229. mqtt_username: str | None = None
  230. mqtt_password: str | None = None
  231. mqtt_topic_prefix: str | None = None
  232. mqtt_use_tls: bool | None = None
  233. external_url: str | None = None
  234. ha_enabled: bool | None = None
  235. ha_url: str | None = None
  236. ha_token: str | None = None
  237. library_archive_mode: str | None = None
  238. library_disk_warning_gb: float | None = None
  239. camera_view_mode: str | None = None
  240. preferred_slicer: str | None = None
  241. prometheus_enabled: bool | None = None
  242. prometheus_token: str | None = None
  243. low_stock_threshold: float | None = Field(default=None, ge=0.1, le=99.9)
  244. user_notifications_enabled: bool | None = None
  245. stagger_group_size: int | None = Field(default=None, ge=1, le=50)
  246. stagger_interval_minutes: int | None = Field(default=None, ge=1, le=60)
  247. require_plate_clear: bool | None = None
  248. default_sidebar_order: str | None = None
  249. @field_validator("default_sidebar_order")
  250. @classmethod
  251. def validate_default_sidebar_order(cls, v: str | None) -> str | None:
  252. if v is None or v == "":
  253. return v
  254. try:
  255. parsed = json.loads(v)
  256. except json.JSONDecodeError:
  257. raise ValueError("default_sidebar_order must be valid JSON or empty")
  258. if isinstance(parsed, dict):
  259. order = parsed.get("order")
  260. elif isinstance(parsed, list):
  261. order = parsed
  262. else:
  263. raise ValueError("default_sidebar_order must be a JSON object with 'order' key or a JSON array")
  264. if not isinstance(order, list) or not all(isinstance(item, str) for item in order):
  265. raise ValueError("sidebar order must be an array of strings")
  266. return v