cover

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

Overview

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

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

Home Assistant documents the domain here:

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

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

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

Control

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

Serve

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

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
SetPosition cover.set_cover_position set_position_topic
SetTiltPosition cover.set_cover_tilt_position tilt_command_topic
CloseCover cover.close_cover command_topic
CloseCoverTilt cover.close_cover_tilt not available
OpenCover cover.open_cover command_topic
OpenCoverTilt cover.open_cover_tilt not available
StopCover cover.stop_cover command_topic
StopCoverTilt cover.stop_cover_tilt not available
Toggle cover.toggle not available
ToggleCoverTilt cover.toggle_cover_tilt not available

Control

type Entity struct

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

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

CloseCover calls cover.close_cover, or publishes payload_close to command_topic. Core describes the service: Closes a cover.

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

CloseCoverTilt calls cover.close_cover_tilt. It has no MQTT equivalent. Core describes the service: Tilts a cover to close.

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

OpenCover calls cover.open_cover, or publishes payload_open to command_topic. Core describes the service: Opens a cover.

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

OpenCoverTilt calls cover.open_cover_tilt. It has no MQTT equivalent. Core describes the service: Tilts a cover open.

func (e *Entity) SetPosition(ctx context.Context, position float64) error

SetPosition calls cover.set_cover_position, or publishes to set_position_topic. Core describes the service: Moves a cover to a specific position.

func (e *Entity) SetTiltPosition(ctx context.Context, tiltPosition float64) error

SetTiltPosition calls cover.set_cover_tilt_position, or publishes to tilt_command_topic. Core describes the service: Moves a cover tilt to a specific position.

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

StopCover calls cover.stop_cover, or publishes payload_stop to command_topic. Core describes the service: Stops a cover's movement.

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

StopCoverTilt calls cover.stop_cover_tilt. It has no MQTT equivalent. Core describes the service: Stops a tilting cover movement.

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

Toggle calls cover.toggle. It has no MQTT equivalent. Core describes the service: Toggles a cover open/closed.

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

ToggleCoverTilt calls cover.toggle_cover_tilt. It has no MQTT equivalent. Core describes the service: Toggles a cover tilt open/closed.

type State = entity.State[Attributes]

State is a snapshot of one cover entity.

func Speed(value string) entity.ServiceOption

Speed sets the "speed" service data field.

Serve over MQTT

type Device struct

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

OnCloseCoverCommand registers the callback for Home Assistant requesting close_cover.

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

OnOpenCoverCommand registers the callback for Home Assistant requesting open_cover.

func (d *Device) OnPositionCommand(fn func(ctx context.Context, position float64) error)

OnPositionCommand registers the callback for Home Assistant requesting a change of position.

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

OnStopCoverCommand registers the callback for Home Assistant requesting stop_cover.

func (d *Device) OnTiltPositionCommand(fn func(ctx context.Context, tiltPosition float64) error)

OnTiltPositionCommand registers the callback for Home Assistant requesting a change of tilt_position.

func (d *Device) ReportCurrentCoverPosition(ctx context.Context, position float64, opts ...mqtt.StateOption) error

ReportCurrentCoverPosition reports the current_cover_position on position_topic. opts override the client's state QoS and retain defaults for this publish.

func (d *Device) ReportCurrentCoverTiltPosition(ctx context.Context, tiltPosition float64, opts ...mqtt.StateOption) error

ReportCurrentCoverTiltPosition reports the current_cover_tilt_position on tilt_status_topic. opts override the client's state QoS and retain defaults for this publish.

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

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

FieldTypeWire keyDescription
CurrentCoverPosition*intcurrent_position

Position. Over MQTT it comes from position_topic.

CurrentCoverTiltPosition*intcurrent_tilt_position

Tilt position. Over MQTT it comes from tilt_status_topic.

DeviceClass*DeviceClassdevice_class

Over MQTT it comes from the device_class option.

IsClosed*boolis_closed
SupportedFeaturesFeaturesupported_features

A set of Feature flags.

SupportedSpeeds*[]stringsupported_speeds

CoverSpeed

type CoverSpeed string

CoverSpeed is core's CoverSpeed.

ConstantValue
CoverSpeedUnknown"Unknown"
CoverSpeedFast"fast"
CoverSpeedMedium"medium"
CoverSpeedSlow"slow"
func CoverSpeedFromString(s string) CoverSpeed

CoverSpeedFromString returns the CoverSpeed spelled s, by its value or by the name of core's member, and CoverSpeedUnknown for anything else.

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

UnmarshalJSON reads any spelling CoverSpeedFromString accepts.

DeviceClass

type DeviceClass string

DeviceClass is core's CoverDeviceClass.

ConstantValue
DeviceClassUnknown"Unknown"
DeviceClassAwning"awning"
DeviceClassBlind"blind"
DeviceClassCurtain"curtain"
DeviceClassDamper"damper"
DeviceClassDoor"door"
DeviceClassGarage"garage"
DeviceClassGate"gate"
DeviceClassShade"shade"
DeviceClassShutter"shutter"
DeviceClassWindow"window"
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 cover supported features, with the values of core's CoverEntityFeature: the supported_features attribute decodes into it.

ConstantValue
FeatureUnknown0
FeatureOpen1 << 0
FeatureClose1 << 1
FeatureSetPosition1 << 2
FeatureStop1 << 3
FeatureOpenTilt1 << 4
FeatureCloseTilt1 << 5
FeatureStopTilt1 << 6
FeatureSetTiltPosition1 << 7
FeatureSpeed1 << 8
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.

StateValue

type StateValue string

StateValue is core's CoverState.

ConstantValue
StateValueUnknown"Unknown"
StateValueClosed"closed"
StateValueClosing"closing"
StateValueOpen"open"
StateValueOpening"opening"
func StateValueFromString(s string) StateValue

StateValueFromString returns the StateValue spelled s, by its value or by the name of core's member, and StateValueUnknown for anything else.

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

UnmarshalJSON reads any spelling StateValueFromString accepts.

MQTT discovery config

type Config struct

Config is the MQTT discovery config of cover.

See: https://www.home-assistant.io/integrations/cover.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.CloseCover publishes to it. Entity.OpenCover publishes to it. Entity.StopCover publishes to it. Device.OnCloseCoverCommand receives what is published to it. Device.OnOpenCoverCommand receives what is published to it. Device.OnStopCoverCommand receives what is published to it. NewDevice defaults it to <base>/close_cover/set.

DefaultEntityId*stringdefault_entity_id
Device*ha.MQTTDeviceDetailsdevice
DeviceClassDeviceClassdevice_class

Allowed: "awning", "blind", "curtain", "damper", "door", "garage", "gate", "shade", "shutter", "window". Over MQTT, Entity reports it as the "device_class" attribute.

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
PayloadAvailable*stringpayload_available

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

Default: "online"

PayloadClose*stringpayload_close

The payload sent when a "close" command is issued. Entity.CloseCover publishes it. Device.OnCloseCoverCommand matches the received payload against it.

Default: "CLOSE"

PayloadNotAvailable*stringpayload_not_available

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

Default: "offline"

PayloadOpen*stringpayload_open

Entity.OpenCover publishes it. Device.OnOpenCoverCommand matches the received payload against it.

Default: "OPEN"

PayloadStop*stringpayload_stop

Entity.StopCover publishes it. Device.OnStopCoverCommand matches the received payload against it.

Default: "STOP"

PayloadStopTilt*stringpayload_stop_tilt

The payload sent when a "stop tilt" command is issued.

Default: "STOP"

PositionClosed*intposition_closed

Default: 0

PositionOpen*intposition_open

Default: 100

PositionTemplate*ha.Templateposition_template

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

PositionTopic*stringposition_topic

Over MQTT, Entity reads the "current_position" attribute from it. Device.ReportCurrentCoverPosition publishes to it. NewDevice defaults it to <base>/position/state.

Qos*intqos

Default: 0

Retain*boolretain

Default: false

SetPositionTemplate*ha.Templateset_position_template

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

SetPositionTopic*stringset_position_topic

Entity.SetPosition publishes to it. Device.OnPositionCommand receives what is published to it. NewDevice defaults it to <base>/set_position/set.

StateClosed*stringstate_closed

The payload received at the state topic that represents the "closed" state.

Default: "closed"

StateClosing*stringstate_closing

The payload received at the state topic that represents the "closing" state.

Default: "closing"

StateOpen*stringstate_open

The payload received at the state topic that represents the "open" state.

Default: "open"

StateOpening*stringstate_opening

The payload received at the state topic that represents the "opening" state.

Default: "opening"

StateStopped*stringstate_stopped

The payload received at the state topic that represents the "stopped" state (for covers that do not report "open"/"closed" state).

Default: "stopped"

StateTopic*stringstate_topic

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

TiltClosedValue*inttilt_closed_value

The value that will be sent to the "tilt command topic" when the cover tilt is closed.

Default: 0

TiltCommandTemplate*ha.Templatetilt_command_template

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

TiltCommandTopic*stringtilt_command_topic

Entity.SetTiltPosition publishes to it. Device.OnTiltPositionCommand receives what is published to it. NewDevice defaults it to <base>/tilt/set.

TiltMax*inttilt_max

The maximum tilt value.

Default: 100

TiltMin*inttilt_min

The minimum tilt value.

Default: 0

TiltOpenedValue*inttilt_opened_value

The value that will be sent to the "tilt command topic" when the cover tilt is opened.

Default: 100

TiltOptimistic*booltilt_optimistic

Default: false

TiltStatusTemplate*ha.Templatetilt_status_template

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

TiltStatusTopic*stringtilt_status_topic

Over MQTT, Entity reads the "current_tilt_position" attribute from it. Device.ReportCurrentCoverTiltPosition publishes to it. NewDevice defaults it to <base>/tilt_status/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).