Skip to main content

franklinwh-ha-notify

Home Assistant notifications with buttons, shared by the FranklinWH bridges (the Direct Connect bridge and the Modbus bridge). It finds the targets that can receive notifications, sends them with buttons, receives the taps, and keeps a record of both.

It holds no FranklinWH device code. What a button does is up to the bridge.

The design, decisions and plan are in docs/DESIGN.md.

What it does

Piece Module
Find each HA instance's notify.* targets; mark which can show buttons (notify.mobile_app_*) targets
Send one notification to many targets, with a delivery count send
Receive taps over HA's WebSocket (mobile_app_notification_action), reconnecting listener
Single-use, expiring requests; wait for an answer with a timeout. In memory, or kept across restarts with Requests.persistent(conn) actions
Title and message templates per event, with variables and reset templates
A record of every send and tap, kept for a number of days. create=False leaves the table to the bridge's own migration (apply SCHEMA) ledger
Optional: route a send by priority (P0–P4), presence and Do Not Disturb, and escalate an unanswered ask to the rooms' speakers routing
All of the above behind one object Notifier

The button contract

A button's action id is FWH_<VERB>_<request id>, for example FWH_YES_3f9c0a1b2c3d4e5f. The request id is inside the action id because every Companion app echoes action back, while other data fields come back differently on iOS and Android. Buttons without the FWH_ prefix (Snooze and the like, added by the phone) are ignored.

A tap resolves as one of:

  • accepted: the bridge's handler runs;
  • unknown: never issued, or lost in a restart (in-memory requests);
  • expired;
  • used: already answered;
  • not-allowed: a button the request didn't offer.

Every outcome is recorded.

Taps arrive only over the bridge's own authenticated WebSocket to Home Assistant. There is no webhook and nothing on the bridge accepts an unauthenticated call.

Use

import os
import sqlite3
from franklinwh_ha_notify import (YES, NO, Button, Event, HAInstance, Ledger, Notifier,
                                  Templates, discover)

ha = HAInstance("home", "http://supervisor/core", token=lambda: os.environ["SUPERVISOR_TOKEN"])
templates = Templates()
templates.register(Event("force_left_running", "Force still running on {gateway}",
                         "A force from before the restart is still running. Release it?"))
# The ledger writes on a worker thread, so the connection must allow that.
db = sqlite3.connect("data/bridge.db", check_same_thread=False)
n = Notifier({"home": ha}, Ledger(db), templates, enabled=lambda: settings.notifications_on)

async def release(request, answer):
    if answer.verb == YES:
        await bridge.release_force(request.gateway)     # the bridge's own gated command
        return "released"
    return "kept"

n.on("force_left_running", release)
n.listen()                                   # one tap listener per HA instance

# Each tap's handler runs in its own task, so a handler may itself ask_and_wait.

targets = [t for t in await discover(ha) if t.buttons]
req = await n.ask("force_left_running", targets, [Button(YES, "Release"), Button(NO, "Keep")],
                  ttl_s=900, gateway="Home")
if req is None:
    ...                                      # nothing delivered (or switched off): no tap will come

# Or wait for the answer inline, as a scheduler step does:
answer = await n.ask_and_wait("ok_to_charge", targets, [Button(YES, "Yes"), Button(NO, "No")],
                              timeout_s=300, gateway="Home")
if answer.timed_out:
    ...                                      # the step's "if no reply" rule

Adding or removing an HA instance while running

When the user adds, edits or removes an HA instance in the bridge's settings, tell the running notifier; no restart is needed for taps:

await n.add_instance(HAInstance("cabin", "https://ha.example.com", token))  # listener starts
await n.add_instance(HAInstance("cabin", "https://ha.example.com", new_token))  # signs in again
await n.remove_instance("cabin")                                             # listener closes
await n.set_instances(instances_from_settings)                               # or sync them all

A new instance gets a listener only while the notifier is listening (after listen(), before close()). Call these on the notifier's event loop.

Requests that survive a restart

In memory is the default. To keep open requests across a restart, keep them in the bridge's database (table notify_requests; create=False leaves it to your migration, which applies SCHEMA, now version 2):

db = sqlite3.connect("data/bridge.db", check_same_thread=False)
n = Notifier({"home": ha}, Ledger(db), templates, requests=Requests.persistent(db))

req = await n.ask("ok_to_charge", targets, [Button(YES, "Yes"), Button(NO, "No")],
                  ttl_s=1800, gateway="Home")
if req is not None:                          # None: nothing delivered, nothing to wait for
    save_step_state(request_id=req.id)       # the bridge's own record

# After a restart: a tap inside the TTL resolves to the original request and runs the
# on() handler. A waiting step finds its request again and waits for the time left:
req = next((r for r in n.pending("ok_to_charge") if r.id == saved_id), None)
answer = await n.wait(req, timeout_s=1800) if req else None

wait returns an answer already given at once, and times out at once (recorded as "no reply") when the request expired while the bridge was down.

Presence- and Do-Not-Disturb-aware routing

Off until the bridge configures it. A Notifier without routing=, or a send without a priority, goes to exactly the targets given, as before.

The bridge supplies the pairings, from Home Assistant entities the user picks:

from franklinwh_ha_notify import (NO, P1, P2, YES, Button, Escalation, Ledger, Notifier,
                                  Pairings, Phone, Room, Router, Target)

alice = Target("home", "mobile_app_alice_iphone", "Alice")
bob = Target("home", "mobile_app_bob_pixel", "Bob")
kitchen = Target("home", "alexa_media_kitchen", "Kitchen speaker")
lounge = Target("home", "mobile_app_lounge_tablet", "Lounge tablet")

pairings = Pairings(
    people=("person.alice", "person.bob"),                    # who's home
    phones=(
        Phone(alice, dnd="binary_sensor.alice_iphone_focus", person="person.alice"),      # iOS
        Phone(bob, dnd="sensor.bob_pixel_do_not_disturb_sensor", person="person.bob"),    # Android
    ),
    rooms=(
        Room("Kitchen", sensors=("binary_sensor.kitchen_mmwave",), targets=(kitchen,)),
        Room("Lounge", sensors=("binary_sensor.lounge_motion", "binary_sensor.lounge_occupancy"),
             targets=(lounge,)),
    ),
)
n = Notifier({"home": ha}, Ledger(db), templates, routing=Router(pairings))
n.routing.pairings = new_pairings          # after the user changes them; the next send uses them

await n.notify("grid_down", targets, gateway="Home", priority=P1)

# Escalation, opt in per ask: phone; no answer in 5 minutes -> occupied rooms;
# another 5 minutes -> every room. Stops when anyone answers, or the request expires.
req = await n.ask("ok_to_charge", targets, [Button(YES, "Yes"), Button(NO, "No")],
                  ttl_s=1800, gateway="Home", priority=P2, escalate=Escalation(after_s=300))
if req is None:
    ...                                      # routing or delivery left nobody to ask
Priority Phone Speakers / tablets
P0–P1 (critical) always, even through DND/Focus announce in occupied rooms; every room if unacknowledged (escalation step 2)
P2 skipped if DND/Focus on announce only in an occupied room, only if someone is home
P3–P4 skipped if DND/Focus on, or the phone's owner is away never announced
  • Routing filters the targets you pass; a target in no pairing is sent as before. Room targets are announced only if you pass them, or by escalation, which adds the paired rooms. So a bridge that leaves speakers out of an ask still gets them on escalation.
  • States are read from each pairing's own HA instance (GET /api/states/<entity>) at the send and again at each escalation step. people are read from Pairings.instance_id, or the first paired target's instance.
  • An unknown, unavailable or unreadable state never suppresses P0–P1: phones are sent and a room with unknown occupancy is announced. Below that, unknown DND or presence doesn't skip a phone; a room is announced at P2 only when it is known to be occupied and someone is known to be home (or no people are paired).
  • Android's DND sensor counts as on in any mode but off (priority_only, total_silence, alarms_only). A person in any zone other than home is away.
  • Every decision is a ROUTED row in the ledger: per target, sent or skipped, and why; escalation adds a row per step, and one when it stops (acknowledged or expired).
  • Escalation runs on the notifier's event loop; close() stops it, and it does not survive a restart.

Development

python3.12 -m venv .venv && .venv/bin/pip install -e ".[test]"
.venv/bin/pytest -q

The tests run against a fake Home Assistant: REST through an httpx mock transport, and a real local WebSocket server that speaks HA's auth and subscribe_events handshake.

Releases: bump version in pyproject.toml, add the CHANGELOG section, tag v<version>. The publish workflow tests, checks the tag matches, and publishes to PyPI.

Licence

MIT

Metadata

Release files for franklinwh-ha-notify 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for franklinwh-ha-notify 0.2.0
File Size Uploaded Provenance Actions
franklinwh_ha_notify-0.2.0.tar.gz 37.6 kB With provenance Details for franklinwh_ha_notify-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for franklinwh-ha-notify 0.2.0
File Interpreter ABI Platform Actions
franklinwh_ha_notify-0.2.0-py3-none-any.whl Python 3 none any Details for franklinwh_ha_notify-0.2.0-py3-none-any.whl

Total release size: 65.1 kB

Release files / franklinwh_ha_notify-0.2.0.tar.gz

Download URL franklinwh_ha_notify-0.2.0.tar.gz
Size 37.6 kB
Tags Source
SHA-256 checksum
How to use checksums
927dcd079956dfb026970a17e6775dd4a59545632add1e3051645f4e02718f96
BLAKE2b-256 checksum
How to use checksums
2bf099a19ebdf0269f79e48224c568e9099e2c86904fcd44c0b9e63a4071cde0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Release files / franklinwh_ha_notify-0.2.0-py3-none-any.whl

Download URL franklinwh_ha_notify-0.2.0-py3-none-any.whl
Size 27.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d74eb69e98183926c1d90321ee587ea4d13068aeda66de4cdbbf234bc5a8028
BLAKE2b-256 checksum
How to use checksums
3029f3adb2c11ce85d86dc27b32a90a07c300ffa0ecb2b0cd47f0579963eb1a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Security for franklinwh-ha-notify 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for franklinwh_ha_notify-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page