update

Go types for Home Assistant's update domain: control over HTTP and MQTT, serve over MQTT discovery.

Control: HTTP Control: MQTT Serve a device Discovery config
import "github.com/taumatix/homeassistant-go/domains/update"

Overview

Package update is Home Assistant's update domain: its types and its MQTT discovery config.

Entity controls one update entity over HTTP or MQTT. Device serves one over MQTT.

The MQTT update state topic carries a JSON document and is not decoded yet.

Home Assistant documents the domain here:

Integration: https://www.home-assistant.io/integrations/update/

MQTT discovery: https://www.home-assistant.io/integrations/update.mqtt/

Entity model (developers): https://contributors.home-assistant.io/core/entities/update/

Control

// client is an *api.Client or a connected *mqtt.Client
target := update.Get(client, "my_update")
state, err := target.State(ctx)
err = target.ClearSkipped(ctx)

Serve

// mqttClient is a connected *mqtt.Client
device, err := update.NewDevice(ctx, mqttClient, "my-update",
	update.WithName("My update"),
)

Commands

What each Entity method does on each transport. MQTT publishes to the topic the entity's discovery config names in that option.

MethodOver HTTP, callsOver MQTT, publishes to
ClearSkipped update.clear_skipped not available
Install update.install command_topic
Skip update.skip not available

Control

type Entity struct

Entity controls one update entity over HTTP (api.Client) or MQTT (mqtt.Client). State and Subscribe come from the embedded Handle.

Embeds entity.Handle[Attributes], whose methods it has too.

func Get(transport entity.Transport, id string) *Entity

Get returns the update entity with the given id: an entity id over HTTP (the "update." prefix may be omitted), a discovery object path or unique id over MQTT. It does no I/O.

func List(ctx context.Context, transport entity.Transport) ([]*Entity, error)

List returns every update entity the transport can see: all of Home Assistant's over HTTP, the discovered ones over MQTT.

func (e *Entity) ClearSkipped(ctx context.Context) error

ClearSkipped calls update.clear_skipped. It has no MQTT equivalent. Core describes the service: Removes the skipped version marker from an update.

func (e *Entity) Install(ctx context.Context, options ...entity.ServiceOption) error

Install calls update.install, or publishes payload_install to command_topic. Core describes the service: Installs an update for a device or service.

func (e *Entity) Skip(ctx context.Context) error

Skip calls update.skip. It has no MQTT equivalent. Core describes the service: Marks a currently available update as skipped.

type State = entity.State[Attributes]

State is a snapshot of one update entity.

func Backup(value bool) entity.ServiceOption

Backup sets the "backup" service data field.

func Version(value string) entity.ServiceOption

Version sets the "version" service data field.

Serve over MQTT

type Device struct

Device is one update entity served by this process over MQTT. Close, Delete and ReportAvailability come from the embedded mqtt.Device.

Commands from Home Assistant arrive through the On*Command callbacks. A callback's ctx is cancelled when the client closes; an error it returns goes to the client's error path (mqtt.WithLogger, mqtt.WithOnError).

Embeds *mqtt.Device, whose methods it has too.

func NewDevice(ctx context.Context, client *mqtt.Client, objectID string, options ...DeviceOption) (*Device, error)

NewDevice announces a update entity through MQTT discovery and returns the device serving it. objectID may carry one "node_id/" prefix. Command, state and availability topics default to <topic root>/update/<objectID>/... and the options can override them. The client must be connected.

func (d *Device) Config() Config

Config returns the discovery config announced to Home Assistant.

func (d *Device) OnInstallCommand(fn func(ctx context.Context) error)

OnInstallCommand registers the callback for Home Assistant requesting install.

func (d *Device) ReportState(ctx context.Context, state string, opts ...mqtt.StateOption) error

ReportState reports the state on state_topic. opts override the client's state QoS and retain defaults for this publish.

type DeviceOption func(config *Config)

DeviceOption customizes the discovery config of a device before it is announced. Options run before the default topics are derived, so a topic they set is kept.

func WithConfig(fn func(config *Config)) DeviceOption

WithConfig applies fn to the discovery config for adjustments not covered by the other options.

func WithDeviceDetails(details ha.MQTTDeviceDetails) DeviceOption

WithDeviceDetails ties the entity to a device in the Home Assistant device registry.

func WithName(name string) DeviceOption

WithName sets the friendly name announced to Home Assistant.

State and values

type Attributes struct

Attributes are the state attributes of a update entity, under the names Home Assistant publishes them with.

See: https://www.home-assistant.io/integrations/update/

FieldTypeWire keyDescription
AutoUpdateboolauto_update

Auto update.

DeviceClass*DeviceClassdevice_class

Over MQTT it comes from the device_class option.

DisplayPrecisionintdisplay_precision

Over MQTT it comes from the display_precision option.

InProgress*boolin_progress

In progress.

InstalledVersion*stringinstalled_version

Installed version.

LatestVersion*stringlatest_version

Latest version.

ReleaseSummary*stringrelease_summary

Release summary. Over MQTT it comes from the release_summary option.

ReleaseUrl*stringrelease_url

Release URL. Over MQTT it comes from the release_url option.

SkippedVersion*stringskipped_version

Skipped version.

SupportedFeaturesFeaturesupported_features

A set of Feature flags.

Title*stringtitle

Title. Over MQTT it comes from the title option.

UpdatePercentage*float64update_percentage

DeviceClass

type DeviceClass string

DeviceClass is core's UpdateDeviceClass.

ConstantValue
DeviceClassUnknown"Unknown"
DeviceClassFirmware"firmware"
func DeviceClassFromString(s string) DeviceClass

DeviceClassFromString returns the DeviceClass spelled s, by its value or by the name of core's member, and DeviceClassUnknown for anything else.

func (v *DeviceClass) UnmarshalJSON(data []byte) error

UnmarshalJSON reads any spelling DeviceClassFromString accepts.

Feature

type Feature uint64

Feature is a set of update supported features, with the values of core's UpdateEntityFeature: the supported_features attribute decodes into it.

ConstantValue
FeatureUnknown0
FeatureInstall1 << 0
FeatureSpecificVersion1 << 1
FeatureProgress1 << 2
FeatureBackup1 << 3
FeatureReleaseNotes1 << 4
func FeatureFromString(s string) Feature

FeatureFromString returns the Feature spelled s, by its value or by the name of core's member, and FeatureUnknown for anything else.

MQTT discovery config

type Config struct

Config is the MQTT discovery config of update.

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

FieldTypeWire keyDescription
Availability[]ha.MQTTAvailabilityavailability
AvailabilityMode*stringavailability_mode

Allowed: "all", "any", "latest".

Default: "latest"

AvailabilityTemplate*ha.Templateavailability_template
AvailabilityTopic*stringavailability_topic

Topic to receive the availability payload on. NewDevice defaults it to <base>/availability.

CommandTopic*stringcommand_topic

Entity.Install publishes to it. Device.OnInstallCommand receives what is published to it. NewDevice defaults it to <base>/install/set.

DefaultEntityId*stringdefault_entity_id
Device*ha.MQTTDeviceDetailsdevice
DeviceClassDeviceClassdevice_class

Allowed: "firmware". Over MQTT, Entity reports it as the "device_class" attribute.

DisplayPrecision*intdisplay_precision

Over MQTT, Entity reports it as the "display_precision" attribute.

Default: 0

EnabledByDefault*boolenabled_by_default

Default: true

Encoding*stringencoding

Default: "utf-8"

EntityCategory*stringentity_category

Allowed: "config", "diagnostic".

EntityPicture*stringentity_picture

An URL to a picture to be assigned.

Group[]stringgroup
Icon*ha.Iconicon
JsonAttributesTemplate*ha.Templatejson_attributes_template
JsonAttributesTopic*stringjson_attributes_topic

The library does not use it: extra attributes in a JSON document, which is not offered yet.

LatestVersionTemplate*ha.Templatelatest_version_template
LatestVersionTopic*stringlatest_version_topic

The library does not use it: the latest version is reported in the state document.

MessageExpiryInterval*map[string]anymessage_expiry_interval

Retention time interval for published message.

Name*stringname
Origin*ha.DeviceOriginorigin
PayloadAvailable*stringpayload_available

The payload that indicates the device is available (defaults to 'online').

Default: "online"

PayloadInstall*stringpayload_install

Entity.Install publishes it. Device.OnInstallCommand matches the received payload against it.

PayloadNotAvailable*stringpayload_not_available

The payload that indicates the device is not available (defaults to 'offline').

Default: "offline"

Qos*intqos

Default: 0

ReleaseSummary*stringrelease_summary

Over MQTT, Entity reports it as the "release_summary" attribute.

ReleaseUrl*stringrelease_url

Over MQTT, Entity reports it as the "release_url" attribute.

Retain*boolretain

Default: false

StateTopicstringstate_topic

Required. Over MQTT, Entity reads the entity state from it. Device.ReportState publishes to it. NewDevice defaults it to <base>/state.

Title*stringtitle

Over MQTT, Entity reports it as the "title" attribute.

UniqueId*stringunique_id
ValueTemplate*ha.Templatevalue_template

Only Home Assistant renders templates: with it set, Entity skips state_topic when reading state. It applies to every state topic without a template of its own; Entity skips those topics when reading state.

VisibleByDefault*boolvisible_by_default

Default: true

func (*Config) IsMQTTDiscoverySchema()

IsMQTTDiscoverySchema marks Config as a discovery payload (ha.MQTTDiscoverySchema).