vacuum

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

Overview

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

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

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

Home Assistant documents the domain here:

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

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

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

Control

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

Serve

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

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
SetFanSpeed vacuum.set_fan_speed set_fan_speed_topic
CleanArea vacuum.clean_area not available
CleanSpot vacuum.clean_spot command_topic
Locate vacuum.locate command_topic
Pause vacuum.pause command_topic
ReturnToBase vacuum.return_to_base command_topic
Start vacuum.start command_topic
Stop vacuum.stop command_topic

Services without a method

Call these with the REST or WebSocket client.

ServiceWhy
vacuum.send_commandneeds required parameters; not offered yet

Control

type Entity struct

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

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

CleanArea calls vacuum.clean_area. It has no MQTT equivalent. Core describes the service: Tells a vacuum cleaner to clean one or more areas.

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

CleanSpot calls vacuum.clean_spot, or publishes payload_clean_spot to command_topic. Core describes the service: Tells a vacuum cleaner to do a spot clean-up.

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

Locate calls vacuum.locate, or publishes payload_locate to command_topic. Core describes the service: Locates a vacuum cleaner.

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

Pause calls vacuum.pause, or publishes payload_pause to command_topic. Core describes the service: Pauses a vacuum cleaner's current task.

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

ReturnToBase calls vacuum.return_to_base, or publishes payload_return_to_base to command_topic. Core describes the service: Sends a vacuum cleaner back to its dock.

func (e *Entity) SetFanSpeed(ctx context.Context, fanSpeed string) error

SetFanSpeed calls vacuum.set_fan_speed, or publishes to set_fan_speed_topic. Core describes the service: Sets the fan speed of a vacuum cleaner.

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

Start calls vacuum.start, or publishes payload_start to command_topic. Core describes the service: Starts or resumes a vacuum cleaner's cleaning task.

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

Stop calls vacuum.stop, or publishes payload_stop to command_topic. Core describes the service: Stops a vacuum cleaner's current task.

type State = entity.State[Attributes]

State is a snapshot of one vacuum entity.

func CleaningAreaId(value []string) entity.ServiceOption

CleaningAreaId sets the "cleaning_area_id" service data field.

Serve over MQTT

type Device struct

Device is one vacuum 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 vacuum 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>/vacuum/<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) OnCleanSpotCommand(fn func(ctx context.Context) error)

OnCleanSpotCommand registers the callback for Home Assistant requesting clean_spot.

func (d *Device) OnFanSpeedCommand(fn func(ctx context.Context, fanSpeed string) error)

OnFanSpeedCommand registers the callback for Home Assistant requesting a change of fan_speed.

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

OnLocateCommand registers the callback for Home Assistant requesting locate.

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

OnPauseCommand registers the callback for Home Assistant requesting pause.

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

OnReturnToBaseCommand registers the callback for Home Assistant requesting return_to_base.

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

OnStartCommand registers the callback for Home Assistant requesting start.

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

OnStopCommand registers the callback for Home Assistant requesting stop.

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 vacuum entity, under the names Home Assistant publishes them with.

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

FieldTypeWire keyDescription
FanSpeed*stringfan_speed
FanSpeedList[]stringfan_speed_list

Over MQTT it comes from the fan_speed_list option.

SupportedFeaturesFeaturesupported_features

A set of Feature flags.

Activity

type Activity string

Activity is core's VacuumActivity.

ConstantValue
ActivityUnknown"Unknown"
ActivityCleaning"cleaning"
ActivityDocked"docked"
ActivityIdle"idle"
ActivityPaused"paused"
ActivityReturning"returning"
ActivityError"error"
func ActivityFromString(s string) Activity

ActivityFromString returns the Activity spelled s, by its value or by the name of core's member, and ActivityUnknown for anything else.

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

UnmarshalJSON reads any spelling ActivityFromString accepts.

Feature

type Feature uint64

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

ConstantValue
FeatureUnknown0
FeatureTurnOn1 << 0
FeatureTurnOff1 << 1
FeaturePause1 << 2
FeatureStop1 << 3
FeatureReturnHome1 << 4
FeatureFanSpeed1 << 5
FeatureStatus1 << 7
FeatureSendCommand1 << 8
FeatureLocate1 << 9
FeatureCleanSpot1 << 10
FeatureMap1 << 11
FeatureState1 << 12
FeatureStart1 << 13
FeatureCleanArea1 << 14
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.

type Segment struct

Segment is core's Segment.

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

FieldTypeWire keyDescription
Group*stringgroup
Idstringid
Namestringname

MQTT discovery config

type Config struct

Config is the MQTT discovery config of vacuum.

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

CleanSegmentsCommandTemplate*ha.Templateclean_segments_command_template
CleanSegmentsCommandTopic*stringclean_segments_command_topic

The library does not use it: not offered yet; adding it is additive.

CommandTopic*stringcommand_topic

Entity.CleanSpot publishes to it. Entity.Locate publishes to it. Entity.Pause publishes to it. Entity.ReturnToBase publishes to it. Entity.Start publishes to it. Entity.Stop publishes to it. Device.OnCleanSpotCommand receives what is published to it. Device.OnLocateCommand receives what is published to it. Device.OnPauseCommand receives what is published to it. Device.OnReturnToBaseCommand receives what is published to it. Device.OnStartCommand receives what is published to it. Device.OnStopCommand receives what is published to it. NewDevice defaults it to <base>/clean_spot/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.

FanSpeedList[]stringfan_speed_list

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

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
Origin*ha.DeviceOriginorigin
PayloadAvailable*stringpayload_available

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

Default: "online"

PayloadCleanSpot*stringpayload_clean_spot

Entity.CleanSpot publishes it. Device.OnCleanSpotCommand matches the received payload against it.

Default: "clean_spot"

PayloadLocate*stringpayload_locate

Entity.Locate publishes it. Device.OnLocateCommand matches the received payload against it.

Default: "locate"

PayloadNotAvailable*stringpayload_not_available

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

Default: "offline"

PayloadPause*stringpayload_pause

Entity.Pause publishes it. Device.OnPauseCommand matches the received payload against it.

Default: "pause"

PayloadReturnToBase*stringpayload_return_to_base

Entity.ReturnToBase publishes it. Device.OnReturnToBaseCommand matches the received payload against it.

Default: "return_to_base"

PayloadStart*stringpayload_start

Entity.Start publishes it. Device.OnStartCommand matches the received payload against it.

Default: "start"

PayloadStop*stringpayload_stop

Entity.Stop publishes it. Device.OnStopCommand matches the received payload against it.

Default: "stop"

Qos*intqos

Default: 0

Retain*boolretain

Default: false

SendCommandTopic*stringsend_command_topic

The library does not use it: not offered yet; adding it is additive.

SetFanSpeedTopic*stringset_fan_speed_topic

Entity.SetFanSpeed publishes to it. Device.OnFanSpeedCommand receives what is published to it. NewDevice defaults it to <base>/set_fan_speed/set.

StateTopic*stringstate_topic

The library does not use it: carries a JSON document, which is not decoded yet.

SupportedFeatures[]stringsupported_features

The features that the entity supports. Allowed: "start", "pause", "stop", "return_home", "fan_speed", "status", "send_command", "locate", "clean_spot".

Default: [ "start", "stop", "return_home", "clean_spot" ]

UniqueId*stringunique_id
VisibleByDefault*boolvisible_by_default

Default: true

func (*Config) IsMQTTDiscoverySchema()

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