After a few RMA of my great Epson ET-2800, the customer support told me it’s recommended to print at least twice a week to prevent clog. I asked if there is a settings to do that automatically. He told me no.
I started using 2printer and windows task scheduling on my PC, however: it print an unnecessary page (I didn’t pay for it) and it only works when I’m at home when the event triggers. Going to work or worse, on vacation, will be a problem … Time to automate using Home Assistant!
The setup was somewhat easy. First, install the CUPS addon from Max Winterstein addon repo. Beware, as of writing, CUPS addon only works with HAOS 9 on RPI. Then go to the web UI of cups and add your printer. For mine, the URL of my printer was ipps://MY_PRINTER_IP:631/ipp/print.
Then I needed to run a python script using pycups. Home Assistant does have python script support, but it only works with built-in module. Worse, pycups need a software install next to it… So I found that the built-in AppDaemon addon would do the trick just fine! Before your start it, though, go in the addon config and add pycups in python packages, and cups-dev in system packages. Then start the AppDaemon. Then, just go to a file editor and you will find a new directory: /config/appdaemon/ . While a normal person would create a new python script in the app folder, I just edited hello.py. I’m lazy.
import appdaemon.plugins.hass.hassapi as hass
import cups
import json
import tempfile
import requests
import os
class HelloWorld(hass.Hass):
def initialize(self):
self.log("My printing script initialize")
self.listen_event(self.mode_event, "plz_print_purge")
def mode_event(self, event, data, kvargs):
self.log("Starting the print")
self.print_purge()
self.log("Print is done!!")
def print_purge(self):
cups.setServer("192.168.1.1:631") # <- Your CUPS server IP here!
conn = cups.Connection(host='192.168.1.1', port=631)# <- Your CUPS server IP here!
# Download the PDF file using requests
response = requests.get("https://www.testprint.net/wp-content/uploads/2022/05/Testprint-testpage-CMYK.pdf")
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Create a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as temp_file:
temp_file.write(response.content)
temp_file.flush()
printer = conn.getDefault()
self.log(f"Printing on {printer}")
# Print the temporary file
job_id = conn.printFile(printer, temp_file.name, 'Print Job', {})
# Remove the temporary file
os.unlink(temp_file.name)
else:
print("Failed to download the file.")
The IP is there two time, because of a bug in pycups.
Just test this script by going in developer tool of Home Assistant, and raise an event called plz_print_purge.
To automate in Home Assistant, I did the following to print 3 time per week:
alias: Printing CYMK 3 times a week
description: ""
trigger:
- platform: time
at: "13:15:00"
condition:
- condition: time
weekday:
- mon
- wed
- fri
action:
- event: plz_print_purge
event_data: {}
mode: single
This took surprisingly a low amount of time, effort and it is quite elegant…
Hope this helps you!!
Top comments (0)