Skip to content

Commit

Permalink
Merge pull request #2 from unixorn/prep-for-release
Browse files Browse the repository at this point in the history
Prep for release
  • Loading branch information
unixorn authored Apr 3, 2023
2 parents f931754 + 317e327 commit 14a888d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/build-publish.yml
Original file line number Diff line number Diff line change
@@ -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 }}
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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",]
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
- [Usage](#usage)
- [Configuration](#configuration)
- [Running the Monitor](#running-the-monitor)
- [Home Assistant](#home-assistant)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## 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
Expand All @@ -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.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <jpb@unixorn.net>"]
readme = "README.md"
Expand Down

0 comments on commit 14a888d

Please sign in to comment.