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.
| Method | Over HTTP, calls | Over 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/
| Field | Type | Wire key | Description |
|---|---|---|---|
AutoUpdate | bool | auto_update | Auto update. |
DeviceClass | *DeviceClass | device_class | Over MQTT it comes from the device_class option. |
DisplayPrecision | int | display_precision | Over MQTT it comes from the display_precision option. |
InProgress | *bool | in_progress | In progress. |
InstalledVersion | *string | installed_version | Installed version. |
LatestVersion | *string | latest_version | Latest version. |
ReleaseSummary | *string | release_summary | Release summary. Over MQTT it comes from the release_summary option. |
ReleaseUrl | *string | release_url | Release URL. Over MQTT it comes from the release_url option. |
SkippedVersion | *string | skipped_version | Skipped version. |
SupportedFeatures | Feature | supported_features | A set of Feature flags. |
Title | *string | title | Title. Over MQTT it comes from the title option. |
UpdatePercentage | *float64 | update_percentage |
DeviceClass
type DeviceClass string
DeviceClass is core's UpdateDeviceClass.
| Constant | Value |
|---|---|
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.
| Constant | Value |
|---|---|
FeatureUnknown | 0 |
FeatureInstall | 1 << 0 |
FeatureSpecificVersion | 1 << 1 |
FeatureProgress | 1 << 2 |
FeatureBackup | 1 << 3 |
FeatureReleaseNotes | 1 << 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/
| Field | Type | Wire key | Description |
|---|---|---|---|
Availability | []ha.MQTTAvailability | availability | |
AvailabilityMode | *string | availability_mode | Allowed: "all", "any", "latest". Default: "latest" |
AvailabilityTemplate | *ha.Template | availability_template | |
AvailabilityTopic | *string | availability_topic | Topic to receive the availability payload on. NewDevice defaults it to <base>/availability. |
CommandTopic | *string | command_topic | Entity.Install publishes to it. Device.OnInstallCommand receives what is published to it. NewDevice defaults it to <base>/install/set. |
DefaultEntityId | *string | default_entity_id | |
Device | *ha.MQTTDeviceDetails | device | |
DeviceClass | DeviceClass | device_class | Allowed: "firmware". Over MQTT, Entity reports it as the "device_class" attribute. |
DisplayPrecision | *int | display_precision | Over MQTT, Entity reports it as the "display_precision" attribute. Default: 0 |
EnabledByDefault | *bool | enabled_by_default | Default: true |
Encoding | *string | encoding | Default: "utf-8" |
EntityCategory | *string | entity_category | Allowed: "config", "diagnostic". |
EntityPicture | *string | entity_picture | An URL to a picture to be assigned. |
Group | []string | group | |
Icon | *ha.Icon | icon | |
JsonAttributesTemplate | *ha.Template | json_attributes_template | |
JsonAttributesTopic | *string | json_attributes_topic | The library does not use it: extra attributes in a JSON document, which is not offered yet. |
LatestVersionTemplate | *ha.Template | latest_version_template | |
LatestVersionTopic | *string | latest_version_topic | The library does not use it: the latest version is reported in the state document. |
MessageExpiryInterval | *map[string]any | message_expiry_interval | Retention time interval for published message. |
Name | *string | name | |
Origin | *ha.DeviceOrigin | origin | |
PayloadAvailable | *string | payload_available | The payload that indicates the device is available (defaults to 'online'). Default: "online" |
PayloadInstall | *string | payload_install | Entity.Install publishes it. Device.OnInstallCommand matches the received payload against it. |
PayloadNotAvailable | *string | payload_not_available | The payload that indicates the device is not available (defaults to 'offline'). Default: "offline" |
Qos | *int | qos | Default: 0 |
ReleaseSummary | *string | release_summary | Over MQTT, Entity reports it as the "release_summary" attribute. |
ReleaseUrl | *string | release_url | Over MQTT, Entity reports it as the "release_url" attribute. |
Retain | *bool | retain | Default: false |
StateTopic | string | state_topic | Required. Over MQTT, Entity reads the entity state from it. Device.ReportState publishes to it. NewDevice defaults it to <base>/state. |
Title | *string | title | Over MQTT, Entity reports it as the "title" attribute. |
UniqueId | *string | unique_id | |
ValueTemplate | *ha.Template | value_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 | *bool | visible_by_default | Default: true |
func (*Config) IsMQTTDiscoverySchema()
IsMQTTDiscoverySchema marks Config as a discovery payload (ha.MQTTDiscoverySchema).