discovery

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package discovery provides structures to support Home Assistant MQTT Discovery.

See https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery

Index

Constants

View Source
const (
	BinarySensor = "binary_sensor"
	Button       = "button"
	Sensor       = "sensor"
	Switch       = "switch"
)

Home Assistant entity platforms

View Source
const (
	Diagnostic = "diagnostic"
)

Home Assitant entity categories

Variables

This section is empty.

Functions

This section is empty.

Types

type Component

type Component map[Option]any

type Connection

type Connection [2]string

type Device

type Device struct {
	ConfigurationURL string       `json:"cu,omitempty"`
	Connections      []Connection `json:"cns,omitempty"`
	HWVersion        string       `json:"hw,omitempty"`
	Identifiers      []string     `json:"ids,omitempty"`
	Manufacturer     string       `json:"mf,omitempty"`
	Model            string       `json:"mdl,omitempty"`
	ModelID          string       `json:"mdl_id,omitempty"`
	Name             string       `json:"name,omitempty"`
	SerialNumber     string       `json:"sn,omitempty"`
	SuggestedArea    string       `json:"sa,omitempty"`
	SWVersion        string       `json:"sw,omitempty"`
}

Device implements the device mapping for the discovery payload. This ties components together in Home Assistant's device registry.

func NewDevice

func NewDevice() (*Device, error)

NewDevice returns a new Device with an identifier equal to the sha256 sum of the device's machine id, encoded in base64.

type Discoverer

type Discoverer interface {
	Discover(d *Discovery)
}

Discoverer is the interface that is implemented by a value to add it to the discovery payload. Implementations should add each of its Components to the provided Discovery's Components field using unique keys.

type Discovery

type Discovery struct {
	Origin     *Origin              `json:"o"`
	Device     *Device              `json:"dev"`
	Components map[string]Component `json:"cmps"`

	AvailabilityTopic string              `json:"-"`
	ObjectID          string              `json:"-"`
	NodeID            string              `json:"-"`
	Nodes             map[string][]string `json:"_nodes,omitempty"`
	Method            string              `json:"_method,omitempty"`
	// contains filtered or unexported fields
}

Discovery is the struct that is encoded into the device discovery payload.

func Load added in v0.0.15

func Load(path string) (*Discovery, error)

Load returns the decoded value of a discovery payload at the file path.

func New

func New(cfg *config.DiscoveryConfig) (*Discovery, error)

New returns a new Discovery struct initialized from the provided config.

func (*Discovery) Diff added in v0.0.15

func (d *Discovery) Diff(old *Discovery) bool

Diff adds an empty component to d for each component in old that isn't already in d. Diff returns true if d should be migrated.

func (*Discovery) Migrate added in v0.0.15

func (d *Discovery) Migrate(ctx context.Context, c mqtt.Client) error

Migrate publishes `{"migrate_discovery": true}` to each component's discovery topic. This is the first step required for migrating component discoveries to a device discovery.

func (*Discovery) Publish added in v0.0.15

func (d *Discovery) Publish(ctx context.Context, c mqtt.Client, migrate bool, args ...string) (err error)

Publish publishes the discovery payload. If migrate is true, Publish migrates the discovery payload either from a device discovery to individual component discoveries, or from individual component discoveries to a device discovery.

func (*Discovery) Rollback added in v0.0.15

func (d *Discovery) Rollback(ctx context.Context, c mqtt.Client) error

Rollback publishes `{"migrate_discovery": true}` to the device discovery topic. This is the first step required for rolling back a device discovery to individual component discoveries.

func (*Discovery) SetAvailability added in v0.0.7

func (d *Discovery) SetAvailability(avail Component)

SetAvailability sets the availability of all components to the one provided.

func (*Discovery) Topic

func (d *Discovery) Topic(prefix, component, nodeID, objectID string) (string, error)

Topic returns the topic to publish the discovery payload to using the provided prefix.

func (*Discovery) Wait added in v0.0.15

func (d *Discovery) Wait(ctx context.Context, c mqtt.Client) error

Wait blocks until the given payload is received on the wait topic, if defined, otherwise Wait returns immediately.

func (*Discovery) Write added in v0.0.15

func (d *Discovery) Write(path string) error

Write writes the json-encoded value of d to path.

type Option

type Option string
const (
	Name       Option = "name"
	SWVersion  Option = "sw"
	SupportURL Option = "url"
)

Options for origin

const (
	ConfigurationURL Option = "cu"
	Connections      Option = "cns"
	Identifiers      Option = "ids"
	Manufacturer     Option = "mf"
	Model            Option = "mdl"
	ModelID          Option = "mdl_id"
	HWVersion        Option = "hw"
	SuggestedArea    Option = "sa"
	SerialNumber     Option = "sn"
)

Options for device

const (
	Availability              Option = "avty"
	AvailabilityMode          Option = "avty_mode"
	AvailabilityTopic         Option = "avty_t"
	AvailabilityTemplate      Option = "avty_tpl"
	CommandOffTemplate        Option = "cmd_off_tpl"
	CommandOnTemplate         Option = "cmd_on_tpl"
	CommandTopic              Option = "cmd_t"
	CommandTemplate           Option = "cmd_tpl"
	DeviceClass               Option = "dev_cla"
	DisplayPrecision          Option = "dsp_prc"
	EnabledByDefault          Option = "en"
	EntityCategory            Option = "ent_cat"
	ForceUpdate               Option = "frc_upd"
	Icon                      Option = "ic"
	JSONAttributes            Option = "json_attr"
	JSONAttributesTopic       Option = "json_attr_t"
	JSONAttributesTemplate    Option = "json_attr_tpl"
	Max                       Option = "max"
	MaxTemp                   Option = "max_temp"
	Min                       Option = "min"
	MinTemp                   Option = "min_temp"
	ObjectID                  Option = "obj_id"
	Options                   Option = "ops"
	Platform                  Option = "p"
	Payload                   Option = "pl"
	PayloadAvailable          Option = "pl_ avail"
	PayloadNotAvailable       Option = "pl_not_avail"
	Retain                    Option = "ret"
	StateClass                Option = "stat_cla"
	StateTopic                Option = "stat_t"
	StateTemplate             Option = "stat_tpl"
	StateValueTemplate        Option = "stat_val_tpl"
	SuggestedDisplayPrecision Option = "sug_dsp_prc"
	Topic                     Option = "t"
	TemperatureStateTopic     Option = "temp_stat_t"
	TemperatureStateTemplate  Option = "temp_stat_tpl"
	TemperatureUnit           Option = "temp_unit"
	UniqueID                  Option = "uniq_id"
	UnitOfMeasurement         Option = "unit_of_meas"
	ValueTemplate             Option = "val_tpl"
)

Options for components

type Origin

type Origin struct {
	Name       string `json:"name"`
	SWVersion  string `json:"sw,omitempty"`
	SupportURL string `json:"url,omitempty"`
}

Origin implements the origin mapping for the discovery payload. This provides context to Home Assistant on the origin of the components.

func NewOrigin

func NewOrigin() *Origin

NewOrigin returns the default Origin with the following values: - Name: "mqttop" - SWVersion: [build.Version()] - SupportURL: "https://github.com/lone-faerie/mqttop"

Directories

Path Synopsis
Package icon provides a few useful [Material Design Icons].
Package icon provides a few useful [Material Design Icons].

Jump to

Keyboard shortcuts

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