lawn_mower

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

Overview

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

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

Home Assistant documents the domain here:

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

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

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

Control

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

Serve

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

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
Dock lawn_mower.dock dock_command_topic
Pause lawn_mower.pause pause_command_topic
StartMowing lawn_mower.start_mowing start_mowing_command_topic
Stop lawn_mower.stop stop_command_topic

Control

type Entity struct

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

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

Dock calls lawn_mower.dock, or publishes "dock" to dock_command_topic. Core describes the service: Returns a lawn mower to its dock.

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

Pause calls lawn_mower.pause, or publishes "pause" to pause_command_topic. Core describes the service: Pauses a lawn mower's current task.

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

StartMowing calls lawn_mower.start_mowing, or publishes "start_mowing" to start_mowing_command_topic. Core describes the service: Starts a lawn mower's mowing task.

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

Stop calls lawn_mower.stop, or publishes "stop" to stop_command_topic. Core describes the service: Stops a lawn mower's current task.

type State = entity.State[Attributes]

State is a snapshot of one lawn_mower entity.

Serve over MQTT

type Device struct

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

OnDockCommand registers the callback for Home Assistant requesting dock.

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

OnPauseCommand registers the callback for Home Assistant requesting pause.

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

OnStartMowingCommand registers the callback for Home Assistant requesting start_mowing.

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

OnStopCommand registers the callback for Home Assistant requesting stop.

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

ReportState reports the state on activity_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 lawn_mower entity, under the names Home Assistant publishes them with.

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

FieldTypeWire keyDescription
SupportedFeaturesFeaturesupported_features

A set of Feature flags.

Activity

type Activity string

Activity is core's LawnMowerActivity.

ConstantValue
ActivityUnknown"Unknown"
ActivityError"error"
ActivityPaused"paused"
ActivityMowing"mowing"
ActivityDocked"docked"
ActivityReturning"returning"
ActivityIdle"idle"
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 lawn_mower supported features, with the values of core's LawnMowerEntityFeature: the supported_features attribute decodes into it.

ConstantValue
FeatureUnknown0
FeatureStartMowing1 << 0
FeaturePause1 << 1
FeatureDock1 << 2
FeatureStop1 << 3
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 lawn_mower.

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

FieldTypeWire keyDescription
ActivityStateTopic*stringactivity_state_topic

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

ActivityValueTemplate*ha.Templateactivity_value_template

Only Home Assistant renders templates: with it set, Entity skips activity_state_topic when reading state.

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.

DefaultEntityId*stringdefault_entity_id
Device*ha.MQTTDeviceDetailsdevice
DockCommandTemplate*ha.Templatedock_command_template

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

DockCommandTopic*stringdock_command_topic

Entity.Dock publishes to it. Device.OnDockCommand receives what is published to it. NewDevice defaults it to <base>/dock/set.

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

Origin*ha.DeviceOriginorigin
PauseCommandTemplate*ha.Templatepause_command_template

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

PauseCommandTopic*stringpause_command_topic

Entity.Pause publishes to it. Device.OnPauseCommand receives what is published to it. NewDevice defaults it to <base>/pause/set.

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

StartMowingCommandTemplate*ha.Templatestart_mowing_command_template

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

StartMowingCommandTopic*stringstart_mowing_command_topic

Entity.StartMowing publishes to it. Device.OnStartMowingCommand receives what is published to it. NewDevice defaults it to <base>/start_mowing/set.

StopCommandTemplate*ha.Templatestop_command_template

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

StopCommandTopic*stringstop_command_topic

Entity.Stop publishes to it. Device.OnStopCommand receives what is published to it. NewDevice defaults it to <base>/stop/set.

UniqueId*stringunique_id
VisibleByDefault*boolvisible_by_default

Default: true

func (*Config) IsMQTTDiscoverySchema()

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