""" Routes API pour les notifications ntfy. """ from typing import Optional from fastapi import APIRouter, Depends from app.core.dependencies import verify_api_key from app.schemas.notification import NotificationRequest, NotificationResponse, NtfyConfig from app.services import notification_service router = APIRouter() @router.get("/config") async def get_notification_config(api_key_valid: bool = Depends(verify_api_key)): """Récupère la configuration actuelle des notifications ntfy.""" config = notification_service.config return { "enabled": config.enabled, "base_url": config.base_url, "default_topic": config.default_topic, "timeout": config.timeout, "has_auth": config.has_auth, } @router.post("/test") async def test_notification( topic: Optional[str] = None, message: str = "🧪 Test de notification depuis Homelab Automation API", api_key_valid: bool = Depends(verify_api_key) ): """Envoie une notification de test.""" success = await notification_service.send( topic=topic, message=message, title="🔔 Test Notification", priority=3, tags=["test_tube", "robot"] ) return { "success": success, "topic": topic or notification_service.config.default_topic, "message": "Notification envoyée" if success else "Échec de l'envoi (voir logs serveur)" } @router.post("/send", response_model=NotificationResponse) async def send_custom_notification( request: NotificationRequest, api_key_valid: bool = Depends(verify_api_key) ): """Envoie une notification personnalisée via ntfy.""" return await notification_service.send_request(request) @router.put("/config") async def update_notification_config( base_url: Optional[str] = None, default_topic: Optional[str] = None, enabled: Optional[bool] = None, timeout: Optional[int] = None, username: Optional[str] = None, password: Optional[str] = None, token: Optional[str] = None, api_key_valid: bool = Depends(verify_api_key) ): """Met à jour la configuration des notifications ntfy.""" current_config = notification_service.config new_config = NtfyConfig( base_url=base_url if base_url is not None else current_config.base_url, default_topic=default_topic if default_topic is not None else current_config.default_topic, enabled=enabled if enabled is not None else current_config.enabled, timeout=timeout if timeout is not None else current_config.timeout, username=username if username is not None else current_config.username, password=password if password is not None else current_config.password, token=token if token is not None else current_config.token, ) notification_service.reconfigure(new_config) return { "message": "Configuration mise à jour", "config": { "enabled": new_config.enabled, "base_url": new_config.base_url, "default_topic": new_config.default_topic, "timeout": new_config.timeout, "has_auth": new_config.has_auth, } } @router.post("/toggle") async def toggle_notifications( enabled: bool, api_key_valid: bool = Depends(verify_api_key) ): """Active ou désactive les notifications ntfy.""" current_config = notification_service.config new_config = NtfyConfig( base_url=current_config.base_url, default_topic=current_config.default_topic, enabled=enabled, timeout=current_config.timeout, username=current_config.username, password=current_config.password, token=current_config.token, ) notification_service.reconfigure(new_config) return { "enabled": enabled, "message": f"Notifications {'activées' if enabled else 'désactivées'}" }