select

Go types for Home Assistant's select 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/selects"

Overview

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

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

Home Assistant documents the domain here:

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

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

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

Control

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

Serve

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

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
SetOption select.select_option command_topic
SelectFirst select.select_first not available
SelectLast select.select_last not available
SelectNext select.select_next not available
SelectPrevious select.select_previous not available

Control

type Entity struct

Entity controls one select 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 select entity with the given id: an entity id over HTTP (the "select." 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 select entity the transport can see: all of Home Assistant's over HTTP, the discovered ones over MQTT.

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

SelectFirst calls select.select_first. It has no MQTT equivalent. Core describes the service: Selects the first option of a select.

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

SelectLast calls select.select_last. It has no MQTT equivalent. Core describes the service: Selects the last option of a select.

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

SelectNext calls select.select_next. It has no MQTT equivalent. Core describes the service: Selects the next option of a select.

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

SelectPrevious calls select.select_previous. It has no MQTT equivalent. Core describes the service: Selects the previous option of a select.

func (e *Entity) SetOption(ctx context.Context, option string) error

SetOption calls select.select_option, or publishes to command_topic. Core describes the service: Selects an option of a select.

type State = entity.State[Attributes]

State is a snapshot of one select entity.

func Cycle(value bool) entity.ServiceOption

Cycle sets the "cycle" service data field.

Serve over MQTT

type Device struct

Device is one select 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 select 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>/select/<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) OnOptionCommand(fn func(ctx context.Context, option string) error)

OnOptionCommand registers the callback for Home Assistant requesting a change of option.

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

ReportOption reports the option 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.

func WithOptions(values ...string) DeviceOption

WithOptions declares the supported option options.

State and values

type Attributes struct

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

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

FieldTypeWire keyDescription
Options[]stringoptions

Options. Over MQTT it comes from the options option.

MQTT discovery config

type Config struct

Config is the MQTT discovery config of select.

See: https://www.home-assistant.io/integrations/select.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.

CommandTemplate*ha.Templatecommand_template

Only Home Assistant renders templates: with it set, Entity refuses to publish to command_topic.

CommandTopicstringcommand_topic

Required. Entity.SetOption publishes to it. Device.OnOptionCommand receives what is published to it. NewDevice defaults it to <base>/set.

DefaultEntityId*stringdefault_entity_id
Device*ha.MQTTDeviceDetailsdevice
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.

MessageExpiryInterval*map[string]anymessage_expiry_interval

Retention time interval for published message.

Name*stringname
Optimistic*booloptimistic

Default: false

Options[]stringoptions

Required. Over MQTT, Entity reports it as the "options" attribute. Set it with WithOptions.

Origin*ha.DeviceOriginorigin
PayloadAvailable*stringpayload_available

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

Default: "online"

PayloadNotAvailable*stringpayload_not_available

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

Default: "offline"

Qos*intqos

Default: 0

Retain*boolretain

Default: false

StateTopic*stringstate_topic

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

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).