settings.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. # Auto-print G-code injection (#422)
  77. gcode_snippets: str = Field(
  78. default="",
  79. description="JSON: per-model G-code injection snippets {model: {start_gcode, end_gcode}}",
  80. )
  81. # Scheduled local backup (#884)
  82. local_backup_enabled: bool = Field(default=False, description="Enable scheduled local backups")
  83. local_backup_schedule: str = Field(default="daily", description="Backup frequency: hourly, daily, weekly")
  84. local_backup_time: str = Field(default="03:00", description="Time of day for daily/weekly backups (HH:MM, 24h)")
  85. local_backup_retention: int = Field(default=5, description="Number of backup files to keep (1-100)")
  86. local_backup_path: str = Field(default="", description="Backup output directory (empty = DATA_DIR/backups)")
  87. # Print modal settings
  88. per_printer_mapping_expanded: bool = Field(
  89. default=False, description="Expand custom filament mapping by default in print modal"
  90. )
  91. # Date/time display format
  92. date_format: str = Field(default="system", description="Date format: system, us, eu, iso")
  93. time_format: str = Field(default="system", description="Time format: system, 12h, 24h")
  94. # Default printer for operations
  95. default_printer_id: int | None = Field(default=None, description="Default printer ID for uploads, reprints, etc.")
  96. # Virtual Printer
  97. virtual_printer_enabled: bool = Field(default=False, description="Enable virtual printer for slicer uploads")
  98. virtual_printer_access_code: str = Field(default="", description="Access code for virtual printer authentication")
  99. virtual_printer_mode: str = Field(
  100. default="immediate",
  101. description="Mode: 'immediate' (archive now), 'review' (pending review), or 'print_queue' (add to print queue)",
  102. )
  103. virtual_printer_archive_name_source: str = Field(
  104. default="metadata",
  105. description="Source for the archive's display name on virtual-printer uploads: 'metadata' uses the 3MF's embedded print_name (default, matches Bambu's behavior), 'filename' uses the filename Bambu Studio sent over FTP (lets users rename via the slicer's 'send to printer' dialog).",
  106. )
  107. # Dark mode theme settings
  108. dark_style: str = Field(default="vibrant", description="Dark mode style: classic, glow, vibrant")
  109. dark_background: str = Field(
  110. default="cool", description="Dark mode background: neutral, warm, cool, oled, slate, forest"
  111. )
  112. dark_accent: str = Field(default="green", description="Dark mode accent: green, teal, blue, orange, purple, red")
  113. # Light mode theme settings
  114. light_style: str = Field(default="classic", description="Light mode style: classic, glow, vibrant")
  115. light_background: str = Field(default="neutral", description="Light mode background: neutral, warm, cool")
  116. light_accent: str = Field(default="green", description="Light mode accent: green, teal, blue, orange, purple, red")
  117. # FTP retry settings for unreliable WiFi connections
  118. ftp_retry_enabled: bool = Field(default=True, description="Enable automatic retry for FTP operations")
  119. ftp_retry_count: int = Field(default=3, description="Number of retry attempts for FTP operations (1-10)")
  120. ftp_retry_delay: int = Field(default=2, description="Seconds to wait between FTP retry attempts (1-30)")
  121. ftp_timeout: int = Field(default=30, description="FTP connection timeout in seconds (10-300)")
  122. # MQTT Relay settings for publishing events to external broker
  123. mqtt_enabled: bool = Field(default=False, description="Enable MQTT event publishing to external broker")
  124. mqtt_broker: str = Field(default="", description="MQTT broker hostname or IP address")
  125. mqtt_port: int = Field(default=1883, description="MQTT broker port (default 1883, TLS typically 8883)")
  126. mqtt_username: str = Field(default="", description="MQTT username for authentication (optional)")
  127. mqtt_password: str = Field(default="", description="MQTT password for authentication (optional)")
  128. mqtt_topic_prefix: str = Field(default="bambuddy", description="Topic prefix for all published messages")
  129. mqtt_use_tls: bool = Field(default=False, description="Use TLS/SSL encryption for MQTT connection")
  130. # External URL for notifications
  131. external_url: str = Field(
  132. default="", description="External URL where Bambuddy is accessible (for notification images)"
  133. )
  134. # Home Assistant integration for smart plug control
  135. ha_enabled: bool = Field(default=False, description="Enable Home Assistant integration for smart plug control")
  136. ha_url: str = Field(default="", description="Home Assistant URL (e.g., http://192.168.1.100:8123)")
  137. ha_token: str = Field(default="", description="Home Assistant Long-Lived Access Token")
  138. ha_url_from_env: bool = Field(default=False, description="Whether HA URL is set via HA_URL environment variable")
  139. ha_token_from_env: bool = Field(
  140. default=False, description="Whether HA token is set via HA_TOKEN environment variable"
  141. )
  142. ha_env_managed: bool = Field(
  143. default=False, description="Whether HA integration is fully managed by environment variables"
  144. )
  145. # File Manager / Library settings
  146. library_archive_mode: str = Field(
  147. default="ask",
  148. description="When printing from File Manager, create archive entry: 'always', 'never', or 'ask'",
  149. )
  150. library_disk_warning_gb: float = Field(
  151. default=5.0,
  152. description="Show warning when free disk space falls below this threshold (GB)",
  153. )
  154. # Camera view settings
  155. camera_view_mode: str = Field(
  156. default="window",
  157. description="Camera view mode: 'window' opens in new browser window, 'embedded' shows overlay on main screen",
  158. )
  159. # Preferred slicer application
  160. preferred_slicer: str = Field(
  161. default="bambu_studio",
  162. description="Preferred slicer: 'bambu_studio' or 'orcaslicer'",
  163. )
  164. # Slicer dispatch mode: when True, "Slice" actions open the in-app
  165. # SliceModal and call the slicer-API sidecar. When False (default), they
  166. # hand off to the user's local desktop slicer via URI scheme — preserving
  167. # the original Bambuddy behavior for users who don't run a sidecar.
  168. use_slicer_api: bool = Field(
  169. default=False,
  170. description="Use the slicer-API sidecar for slicing instead of the desktop slicer URI scheme",
  171. )
  172. # Slicer-API sidecar base URLs. Per-installation, configured via the
  173. # Settings UI (the "Slicer" card). Empty string means "fall back to the
  174. # SLICER_API_URL / BAMBU_STUDIO_API_URL env vars" — which themselves
  175. # default to the docker-compose ports in core/config.py.
  176. orcaslicer_api_url: str = Field(
  177. default="",
  178. description="OrcaSlicer sidecar URL (e.g. http://localhost:3003). Empty falls back to the SLICER_API_URL env var.",
  179. )
  180. bambu_studio_api_url: str = Field(
  181. default="",
  182. description="BambuStudio sidecar URL (e.g. http://localhost:3001). Empty falls back to the BAMBU_STUDIO_API_URL env var.",
  183. )
  184. # Prometheus metrics endpoint
  185. prometheus_enabled: bool = Field(default=False, description="Enable Prometheus metrics endpoint at /metrics")
  186. prometheus_token: str = Field(
  187. default="", description="Bearer token for Prometheus metrics authentication (optional)"
  188. )
  189. # Inventory low stock threshold
  190. low_stock_threshold: float = Field(
  191. default=20.0,
  192. ge=0.1,
  193. le=99.9,
  194. description="Low stock threshold percentage (%) for inventory filtering and display",
  195. )
  196. # User email notifications (requires Advanced Authentication)
  197. user_notifications_enabled: bool = Field(
  198. default=True,
  199. description="Enable user email notifications for print job events (requires Advanced Authentication)",
  200. )
  201. # Default print options
  202. default_bed_levelling: bool = Field(default=True, description="Default bed levelling option for new prints")
  203. default_flow_cali: bool = Field(default=False, description="Default flow calibration option for new prints")
  204. default_vibration_cali: bool = Field(
  205. default=True, description="Default vibration calibration option for new prints"
  206. )
  207. default_layer_inspect: bool = Field(
  208. default=False, description="Default first layer inspection option for new prints"
  209. )
  210. default_timelapse: bool = Field(default=False, description="Default timelapse option for new prints")
  211. # Staggered batch start for multi-printer jobs
  212. stagger_group_size: int = Field(
  213. default=2, ge=1, le=50, description="Number of printers to start simultaneously in staggered mode"
  214. )
  215. stagger_interval_minutes: int = Field(
  216. default=5, ge=1, le=60, description="Minutes between staggered printer groups"
  217. )
  218. # Plate-clear confirmation for queue scheduling
  219. require_plate_clear: bool = Field(
  220. default=False,
  221. description="Require per-printer plate-clear confirmation before starting queued prints on finished printers",
  222. )
  223. queue_shortest_first: bool = Field(
  224. default=False,
  225. description="Shortest Job First — scheduler prioritizes shorter print jobs over longer ones",
  226. )
  227. # LDAP authentication (#794)
  228. ldap_enabled: bool = Field(default=False, description="Enable LDAP authentication")
  229. ldap_server_url: str = Field(default="", description="LDAP server URL (e.g., ldap://ldap.example.com:389)")
  230. ldap_bind_dn: str = Field(default="", description="Bind DN for LDAP searches (e.g., cn=admin,dc=example,dc=com)")
  231. ldap_bind_password: str = Field(default="", description="Bind password for LDAP searches")
  232. ldap_search_base: str = Field(default="", description="Search base DN (e.g., ou=users,dc=example,dc=com)")
  233. ldap_user_filter: str = Field(
  234. default="(sAMAccountName={username})",
  235. description="LDAP user search filter. {username} is replaced with the login username",
  236. )
  237. ldap_security: str = Field(default="starttls", description="LDAP security: 'starttls' or 'ldaps'")
  238. ldap_group_mapping: str = Field(
  239. default="",
  240. description="JSON: LDAP group to BamBuddy group mapping {ldap_group_dn: bambuddy_group_name}",
  241. )
  242. ldap_auto_provision: bool = Field(
  243. default=False,
  244. description="Auto-create BamBuddy user on first successful LDAP login",
  245. )
  246. ldap_default_group: str = Field(
  247. default="",
  248. description="Fallback BamBuddy group name assigned when an LDAP user authenticates but has no mapped groups. Empty = no fallback.",
  249. )
  250. # Obico AI failure detection (#172)
  251. obico_enabled: bool = Field(default=False, description="Enable Obico AI print failure detection")
  252. obico_ml_url: str = Field(
  253. default="",
  254. description="Self-hosted Obico ML API base URL (e.g., http://192.168.1.10:3333)",
  255. )
  256. obico_sensitivity: str = Field(
  257. default="medium",
  258. description="Detection sensitivity: 'low', 'medium', or 'high' (adjusts LOW/HIGH thresholds)",
  259. )
  260. obico_action: str = Field(
  261. default="notify",
  262. description="Action on detected failure: 'notify', 'pause', or 'pause_and_off'",
  263. )
  264. obico_poll_interval: int = Field(
  265. default=10,
  266. ge=5,
  267. le=120,
  268. description="Seconds between detection checks while a print is running",
  269. )
  270. obico_enabled_printers: str = Field(
  271. default="",
  272. description="JSON array of printer IDs to monitor (empty = all connected printers)",
  273. )
  274. # Inventory forecasting
  275. forecast_global_lead_time_days: int = Field(
  276. default=0,
  277. ge=0,
  278. description="Global lead time floor (days) used in reorder point calculation for all SKUs",
  279. )
  280. # Default sidebar order (admin-set for all users)
  281. default_sidebar_order: str = Field(
  282. default="",
  283. description="JSON object with 'order' key containing array of sidebar item IDs (empty = no default)",
  284. )
  285. class AppSettingsUpdate(BaseModel):
  286. """Schema for updating settings (all fields optional)."""
  287. auto_archive: bool | None = None
  288. save_thumbnails: bool | None = None
  289. capture_finish_photo: bool | None = None
  290. default_filament_cost: float | None = None
  291. currency: str | None = None
  292. energy_cost_per_kwh: float | None = None
  293. energy_tracking_mode: str | None = None
  294. spoolman_enabled: bool | None = None
  295. spoolman_url: str | None = None
  296. spoolman_sync_mode: str | None = None
  297. spoolman_disable_weight_sync: bool | None = None
  298. spoolman_report_partial_usage: bool | None = None
  299. disable_filament_warnings: bool | None = None
  300. prefer_lowest_filament: bool | None = None
  301. check_updates: bool | None = None
  302. check_printer_firmware: bool | None = None
  303. include_beta_updates: bool | None = None
  304. language: str | None = None
  305. notification_language: str | None = None
  306. bed_cooled_threshold: float | None = None
  307. ams_humidity_good: int | None = None
  308. ams_humidity_fair: int | None = None
  309. ams_temp_good: float | None = None
  310. ams_temp_fair: float | None = None
  311. ams_history_retention_days: int | None = None
  312. queue_drying_enabled: bool | None = None
  313. queue_drying_block: bool | None = None
  314. ambient_drying_enabled: bool | None = None
  315. drying_presets: str | None = None
  316. per_printer_mapping_expanded: bool | None = None
  317. date_format: str | None = None
  318. time_format: str | None = None
  319. default_printer_id: int | None = None
  320. virtual_printer_enabled: bool | None = None
  321. virtual_printer_access_code: str | None = None
  322. virtual_printer_mode: str | None = None
  323. virtual_printer_archive_name_source: str | None = None
  324. dark_style: str | None = None
  325. dark_background: str | None = None
  326. dark_accent: str | None = None
  327. light_style: str | None = None
  328. light_background: str | None = None
  329. light_accent: str | None = None
  330. ftp_retry_enabled: bool | None = None
  331. ftp_retry_count: int | None = None
  332. ftp_retry_delay: int | None = None
  333. ftp_timeout: int | None = None
  334. mqtt_enabled: bool | None = None
  335. mqtt_broker: str | None = None
  336. mqtt_port: int | None = None
  337. mqtt_username: str | None = None
  338. mqtt_password: str | None = None
  339. mqtt_topic_prefix: str | None = None
  340. mqtt_use_tls: bool | None = None
  341. external_url: str | None = None
  342. ha_enabled: bool | None = None
  343. ha_url: str | None = None
  344. ha_token: str | None = None
  345. library_archive_mode: str | None = None
  346. library_disk_warning_gb: float | None = None
  347. camera_view_mode: str | None = None
  348. preferred_slicer: str | None = None
  349. use_slicer_api: bool | None = None
  350. orcaslicer_api_url: str | None = None
  351. bambu_studio_api_url: str | None = None
  352. prometheus_enabled: bool | None = None
  353. prometheus_token: str | None = None
  354. low_stock_threshold: float | None = Field(default=None, ge=0.1, le=99.9)
  355. user_notifications_enabled: bool | None = None
  356. default_bed_levelling: bool | None = None
  357. default_flow_cali: bool | None = None
  358. default_vibration_cali: bool | None = None
  359. default_layer_inspect: bool | None = None
  360. default_timelapse: bool | None = None
  361. stagger_group_size: int | None = Field(default=None, ge=1, le=50)
  362. stagger_interval_minutes: int | None = Field(default=None, ge=1, le=60)
  363. require_plate_clear: bool | None = None
  364. queue_shortest_first: bool | None = None
  365. gcode_snippets: str | None = None
  366. local_backup_enabled: bool | None = None
  367. local_backup_schedule: str | None = None
  368. local_backup_time: str | None = None
  369. local_backup_retention: int | None = None
  370. local_backup_path: str | None = None
  371. ldap_enabled: bool | None = None
  372. ldap_server_url: str | None = None
  373. ldap_bind_dn: str | None = None
  374. ldap_bind_password: str | None = None
  375. ldap_search_base: str | None = None
  376. ldap_user_filter: str | None = None
  377. ldap_security: str | None = None
  378. ldap_group_mapping: str | None = None
  379. ldap_auto_provision: bool | None = None
  380. ldap_default_group: str | None = None
  381. obico_enabled: bool | None = None
  382. obico_ml_url: str | None = None
  383. obico_sensitivity: str | None = None
  384. obico_action: str | None = None
  385. obico_poll_interval: int | None = Field(default=None, ge=5, le=120)
  386. obico_enabled_printers: str | None = None
  387. default_sidebar_order: str | None = None
  388. forecast_global_lead_time_days: int | None = Field(default=None, ge=0)
  389. @field_validator("gcode_snippets")
  390. @classmethod
  391. def validate_gcode_snippets(cls, v: str | None) -> str | None:
  392. if v is None or v == "":
  393. return v
  394. try:
  395. parsed = json.loads(v)
  396. except json.JSONDecodeError:
  397. raise ValueError("gcode_snippets must be valid JSON or empty")
  398. if not isinstance(parsed, dict):
  399. raise ValueError("gcode_snippets must be a JSON object keyed by printer model")
  400. return v
  401. @field_validator("ldap_group_mapping")
  402. @classmethod
  403. def validate_ldap_group_mapping(cls, v: str | None) -> str | None:
  404. if v is None or v == "":
  405. return v
  406. try:
  407. parsed = json.loads(v)
  408. except json.JSONDecodeError:
  409. raise ValueError("ldap_group_mapping must be valid JSON or empty")
  410. if not isinstance(parsed, dict):
  411. raise ValueError("ldap_group_mapping must be a JSON object mapping LDAP group DNs to BamBuddy group names")
  412. return v
  413. @field_validator("obico_enabled_printers")
  414. @classmethod
  415. def validate_obico_enabled_printers(cls, v: str | None) -> str | None:
  416. if v is None or v == "":
  417. return v
  418. try:
  419. parsed = json.loads(v)
  420. except json.JSONDecodeError:
  421. raise ValueError("obico_enabled_printers must be valid JSON or empty")
  422. if not isinstance(parsed, list) or not all(isinstance(item, int) for item in parsed):
  423. raise ValueError("obico_enabled_printers must be a JSON array of printer IDs (integers)")
  424. return v
  425. @field_validator("obico_sensitivity")
  426. @classmethod
  427. def validate_obico_sensitivity(cls, v: str | None) -> str | None:
  428. if v is None:
  429. return v
  430. if v not in ("low", "medium", "high"):
  431. raise ValueError("obico_sensitivity must be 'low', 'medium', or 'high'")
  432. return v
  433. @field_validator("obico_action")
  434. @classmethod
  435. def validate_obico_action(cls, v: str | None) -> str | None:
  436. if v is None:
  437. return v
  438. if v not in ("notify", "pause", "pause_and_off"):
  439. raise ValueError("obico_action must be 'notify', 'pause', or 'pause_and_off'")
  440. return v
  441. @field_validator("default_sidebar_order")
  442. @classmethod
  443. def validate_default_sidebar_order(cls, v: str | None) -> str | None:
  444. if v is None or v == "":
  445. return v
  446. try:
  447. parsed = json.loads(v)
  448. except json.JSONDecodeError:
  449. raise ValueError("default_sidebar_order must be valid JSON or empty")
  450. if isinstance(parsed, dict):
  451. order = parsed.get("order")
  452. elif isinstance(parsed, list):
  453. order = parsed
  454. else:
  455. raise ValueError("default_sidebar_order must be a JSON object with 'order' key or a JSON array")
  456. if not isinstance(order, list) or not all(isinstance(item, str) for item in order):
  457. raise ValueError("sidebar order must be an array of strings")
  458. return v