mqtt

package
v1.2.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package mqtt bridges the state bus to MQTT with Home Assistant auto-discovery: it publishes BLE sensors, a screen intent switch plus a live-power binary_sensor, and routes screen commands back.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	OnConnect(func())
	OnConnectionLost(func(err error))
	Connect() error
	Disconnect()
	Publish(topic string, qos byte, retain bool, payload []byte) error
	Subscribe(topic string, qos byte, handler func(payload []byte, retained bool)) error
}

Client is the minimal MQTT transport the Hub needs (paho in the adapter subpackage). OnConnect must fire on every (re)connect.

type Deps added in v1.2.3

type Deps struct {
	Bus     *state.Bus
	Screen  Screen
	Metrics MetricsReader
	Host    HostReader
	Power   PowerController
}

Deps groups the Publisher's collaborators to keep New's signature short.

type DeviceMeta added in v1.2.1

type DeviceMeta struct {
	Manufacturer     string
	Model            string
	HwVersion        string
	SwVersion        string
	ConfigurationURL string
}

DeviceMeta is the optional hardware/software metadata shown on the HA device card.

type HostReader added in v1.2.3

type HostReader interface {
	Hostname() string
	IP() string
}

IP is read per tick because DHCP can move it under a running frame.

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub multiplexes one MQTT connection; subscriptions and OnConnect handlers persist across reconnects.

func NewHub

func NewHub(log *slog.Logger, client Client) *Hub

NewHub wires Hub into client callbacks; consumers go through Hub from here on.

func (*Hub) AddOnConnect

func (h *Hub) AddOnConnect(f func())

AddOnConnect registers f for every (re)connect. f runs on paho's onConnect goroutine, hop off it if you own mutable state.

func (*Hub) Connect

func (h *Hub) Connect(ctx context.Context) bool

Connect retries until success or ctx is cancelled; paho keeps the connection alive after.

func (*Hub) Disconnect

func (h *Hub) Disconnect()

Disconnect closes the underlying connection. Safe to call without Connect.

func (*Hub) Publish

func (h *Hub) Publish(topic string, qos byte, retain bool, payload []byte) error

Publish forwards to the underlying client.

func (*Hub) Subscribe

func (h *Hub) Subscribe(topic string, qos byte, handler func(payload []byte, retained bool))

Subscribe records a persistent subscription, replayed on every (re)connect; also issued synchronously when called on an already-connected Hub.

type MetricsReader added in v1.2.1

type MetricsReader interface {
	Read(ctx context.Context) hostmetrics.Metrics
}

MetricsReader supplies Pi telemetry snapshots; implemented by *hostmetrics.Reader.

type PowerController added in v1.2.3

type PowerController interface {
	Reboot(ctx context.Context) error
	PowerOff(ctx context.Context) error
}

type Publisher

type Publisher struct {
	// contains filtered or unexported fields
}

Publisher mirrors bus state to MQTT and applies inbound screen commands.

func New

func New(log *slog.Logger, hub *Hub, deps Deps, set Settings, specs []SensorSpec) *Publisher

New registers with the Hub at construction so a connect that beats Run still queues a republish.

func (*Publisher) Run

func (p *Publisher) Run(ctx context.Context)

Run mirrors bus events to MQTT until ctx is cancelled. Bridge-offline is published on exit only if a connection was ever established.

type Screen

type Screen interface {
	On(ctx context.Context) error
	Off(ctx context.Context) error
	Auto() bool  // intent: motion auto-wake enabled (the switch)
	State() bool // live panel power (the binary_sensor)
}

Screen is the manual screen-control surface; implemented by *display.Screen.

type SensorSpec

type SensorSpec struct {
	ID    string   // matches Reading.DeviceID and the config sensor id
	Role  string   // friendly label; falls back to ID
	Kinds []string // "temperature" | "humidity" | "motion"
}

SensorSpec is one sensor and the kinds it emits, used to build HA entities before any reading arrives.

type Settings

type Settings struct {
	NodeID          string
	BaseTopic       string
	DiscoveryPrefix string
	DeviceName      string // HA device friendly name; defaults to a title-cased NodeID
	StaleAfter      time.Duration
	Device          DeviceMeta // hardware/software metadata for the HA device card
	Undervoltage    bool       // advertise the undervoltage entity (vcgencmd available)
	Hostname        string     // OS hostname for the diagnostic sensor
	CanReboot       bool       // logind permits reboot (polkit rule present)
	CanPowerOff     bool       // logind permits power-off (polkit rule present)
}

Settings configures the MQTT bridge. NodeID identifies this frame, it forms the HA device id, prefixes every unique_id, and groups discovery topics, so multiple frames need only distinct NodeIDs (no hardcoded MACs).

func (Settings) BridgeAvailabilityTopic

func (s Settings) BridgeAvailabilityTopic() string

BridgeAvailabilityTopic is the bridge availability topic; callers set it as the MQTT Last Will so an unclean disconnect marks the frame offline.

Directories

Path Synopsis
Package adapter implements mqtt.Client over eclipse/paho.mqtt.golang.
Package adapter implements mqtt.Client over eclipse/paho.mqtt.golang.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL