settings.py 15 KB

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