From 9955d8cde68761554cdbfc8db5a36d2f241212c9 Mon Sep 17 00:00:00 2001 From: Joe Block Date: Mon, 3 Apr 2023 15:24:48 -0600 Subject: [PATCH 1/5] Add 32bit ARM to Multiarch image platforms Signed-off-by: Joe Block --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f95e152..10a7861 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ local: wheel docker buildx build --load -t ${IMAGE_NAME} --build-arg application_version=${MODULE_VERSION} . multiimage: wheel ## Make a multi-architecture docker image - docker buildx build --platform linux/arm64,linux/amd64 --push -t ${IMAGE_NAME}:${MODULE_VERSION} --build-arg application_version=${MODULE_VERSION} . + docker buildx build --platform linux/arm64,linux/amd64,linux/arm/v7 --push -t ${IMAGE_NAME}:${MODULE_VERSION} --build-arg application_version=${MODULE_VERSION} . make local clean: ## Clean up our checkout From c9054ac11c4009ac69a6f2b4f774c1791615825b Mon Sep 17 00:00:00 2001 From: Joe Block Date: Mon, 3 Apr 2023 16:25:46 -0600 Subject: [PATCH 2/5] Add build-publish.yml Set up automatic publishing to pypi and dockerhub when a release is made that starts with a `v`, `v0.1.0` for example Signed-off-by: Joe Block --- .github/workflows/build-publish.yml | 80 +++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 .github/workflows/build-publish.yml diff --git a/.github/workflows/build-publish.yml b/.github/workflows/build-publish.yml new file mode 100644 index 0000000..ca31dc7 --- /dev/null +++ b/.github/workflows/build-publish.yml @@ -0,0 +1,80 @@ +# This workflow will build a Python wheel, a Docker image containing the CLI tool and publish both to PyPi and DockerHub + +name: Build and publish + +# Run only when pushing a Git version tag +on: + push: + tags: + - "v*" + +permissions: + contents: read + +jobs: + build-wheel: + name: Build and publish Python package + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install poetry + run: pipx install poetry + + - name: Set up Python 3.10 + uses: actions/setup-python@v4 + with: + python-version: "3.10" + cache: "poetry" + + - name: Bump version and build package + run: | + # Extract the package version from the latest git tag, removing the leading `v` + VERSION=$(git describe --tags --abbrev=0 | sed --quiet --regexp-extended 's/v(.*)/\1/p') + echo "$VERSION" + # Set package version using poetry CLI + poetry version "$VERSION" + poetry build + + - name: Upload package + uses: actions/upload-artifact@v3 + with: + name: Python package + path: dist/ + if-no-files-found: error + + - name: Publish wheel + run: | + poetry publish --username __token__ --password ${{ secrets.PYPI_PASSWORD }} + + docker-image: + needs: build-wheel + name: Build and publish Docker image + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Download Python package + uses: actions/download-artifact@v3 + with: + name: Python package + path: dist/ + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Build Docker image + uses: docker/build-push-action@v4 + with: + context: . + build-args: | + application_version: ${{ github.ref_name }} + push: true + platforms: linux/arm64,linux/amd64,linux/arm/v7 + tags: unixorn/ha-franklin:${{ github.ref_name }} From 1e8a36dd3f1fd6b36d999f5e55d2117813bcba86 Mon Sep 17 00:00:00 2001 From: Joe Block Date: Mon, 3 Apr 2023 16:31:48 -0600 Subject: [PATCH 3/5] Version bump Signed-off-by: Joe Block --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5565b00..2d183be 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ha-franklin" -version = "0.1.0" +version = "0.1.1" description = "ha-franklin monitors CUPSD queues and writes information to MQTT for Home Assistant" authors = ["Joe Block "] readme = "README.md" From cafafb1322f6398ad3f769f7b52d99b62c663f8d Mon Sep 17 00:00:00 2001 From: Joe Block Date: Mon, 3 Apr 2023 16:32:01 -0600 Subject: [PATCH 4/5] Run as `nobody` inside the container Signed-off-by: Joe Block --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 588401f..aca53b4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,5 +26,6 @@ COPY dist/*.whl /app RUN pip install --no-cache-dir --disable-pip-version-check /app/*.whl \ && rm -fr /tmp/* +USER nobody # Default shell CMD ["/bin/bash",] From 317e32783d9488f0a2919b931c0ae9cf2de0967c Mon Sep 17 00:00:00 2001 From: Joe Block Date: Mon, 3 Apr 2023 16:42:04 -0600 Subject: [PATCH 5/5] Add Home Assistant instructions - Add Home Assistant instructions to README.md - Add `make doctoc` stanza to update the TOC in README.md Signed-off-by: Joe Block --- Makefile | 3 +++ README.md | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 10a7861..bb463f5 100644 --- a/Makefile +++ b/Makefile @@ -44,3 +44,6 @@ multi: requirements.txt: poetry.lock Makefile poetry export -o requirements.txt + +doctoc: ## Update the TOC + doctoc --title '## Table of Contents' README.md diff --git a/README.md b/README.md index e2fa634..9dc6bb4 100644 --- a/README.md +++ b/README.md @@ -14,16 +14,17 @@ - [Usage](#usage) - [Configuration](#configuration) - [Running the Monitor](#running-the-monitor) + - [Home Assistant](#home-assistant) ## Background -I wanted a non-toy test example of using [ha-mqtt-discoverable](https://github.com/unixorn/ha-mqtt-discoverable/tree/v0.8.1). +I wanted a non-toy test example of using the [ha-mqtt-discoverable](https://github.com/unixorn/ha-mqtt-discoverable/tree/v0.8.1) module. `ha-franklin` will monitor CUPSD print queues, and present a binary sensor to Home Assistant over MQTT showing whether the printer is printing. -I use this to turn the smart switch for the HP 4050N in the basement on and off so that by the time I walk downstairs from my office after printing something, Home Assistant has turned on the power to the printer and the job has started printing. +I use this to turn the smart switch for the HP 4050N in the basement on and off so that by the time I walk downstairs from my office after printing something, Home Assistant has turned on the power to the printer and the job has at least started printing. ## Usage @@ -47,3 +48,15 @@ The easiest way to create a configuration file is to start by copying `config/co I recommend using `docker`, `nerdctl` or `podman` to run the tooling in a container. `docker run -v "$(pwd)/config":/config --rm unixorn/ha-franklin ha-cupsd-monitor-queues --settings-file /config/config.yaml` + +### Home Assistant + +The container will create a set of MQTT topics using Home Assistant's MQTT discoverability protocol so that your print queue's printing status shows up in Home Assistant. + +I set up two automations - one to turn on the peanut plug my HP 4050N is plugged into when jobs appear in the cupsd queue when the sensor turns to on, and a second that turns it off once the sensor switches back to off for ten minutes. + +I give it ten minutes for a couple of reasons: + +First, because CUPSD will report the queue as done printing when all the postscript has been spooled to the printer. Depending on the complexity of the print job, it may take a minute or two to print the last few pages of the job, even though cupsd considers it complete. If the power gets turned off too soon, you can lose the last page or two of the job, and more annoyingly, cause a printer jam if the power cuts off while a page is moving through the paper path. + +Secondly, because although I print rarely, when I do, I typically print several things within a few minutes and I'd prefer to not toggle the printer on and off more than is strictly necessary.