Created
March 29, 2025 16:29
-
-
Save arest/4eca1056bed4966b6c716cad95404fbb to your computer and use it in GitHub Desktop.
Homeassistant - Print Wenhook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import aiohttp | |
| async def print_pdf(pdf_url, paper_size=None, page_range=None, callback=None): | |
| cups_addon_url = "http://localhost:8099/api/print" | |
| # Prepare the request data with the PDF URL and optional parameters | |
| request_data = { | |
| "endpoint": pdf_url, | |
| "paper_size": paper_size, | |
| "page_range": page_range, | |
| "callback": callback | |
| } | |
| # Remove None values from the request data | |
| request_data = {k: v for k, v in request_data.items() if v is not None} | |
| log.info(f"Request data {request_data}") | |
| # Send the request to the CUPS client addon using aiohttp | |
| async with aiohttp.ClientSession() as session: | |
| async with session.post(cups_addon_url, json=request_data) as resp: | |
| status = resp.status | |
| log.info(f"Cups Client response status {status}") | |
| @service | |
| @webhook_trigger("print_webhook", local_only=False) | |
| def print_webhook(payload): | |
| # Verify that payload contains pdf_url | |
| log.info(f"Print webhook {payload}") | |
| if not payload or 'pdf_url' not in payload: | |
| return {"success": False, "error": "Missing required 'pdf_url' in payload"} | |
| paper_size = None | |
| page_range = None | |
| pdf_url = payload['pdf_url'] | |
| callback = None | |
| if "paper_size" in payload: | |
| paper_size = payload['paper_size'] | |
| if "page_range" in payload: | |
| page_range = payload['page_range'] | |
| if "callback" in payload: | |
| callback = payload['callback'] | |
| print_pdf(pdf_url, paper_size, page_range, callback) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment