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.
| Method | Over HTTP, calls | Over 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/
| Field | Type | Wire key | Description |
|---|---|---|---|
CurrentCoverPosition | *int | current_position | Position. Over MQTT it comes from position_topic. |
CurrentCoverTiltPosition | *int | current_tilt_position | Tilt position. Over MQTT it comes from tilt_status_topic. |
DeviceClass | *DeviceClass | device_class | Over MQTT it comes from the device_class option. |
IsClosed | *bool | is_closed | |
SupportedFeatures | Feature | supported_features | A set of Feature flags. |
SupportedSpeeds | *[]string | supported_speeds |
CoverSpeed
type CoverSpeed string
CoverSpeed is core's CoverSpeed.
| Constant | Value |
|---|---|
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.
| Constant | Value |
|---|---|
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.
| Constant | Value |
|---|---|
FeatureUnknown | 0 |
FeatureOpen | 1 << 0 |
FeatureClose | 1 << 1 |
FeatureSetPosition | 1 << 2 |
FeatureStop | 1 << 3 |
FeatureOpenTilt | 1 << 4 |
FeatureCloseTilt | 1 << 5 |
FeatureStopTilt | 1 << 6 |
FeatureSetTiltPosition | 1 << 7 |
FeatureSpeed | 1 << 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.
| Constant | Value |
|---|---|
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/
| 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.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 | *string | default_entity_id | |
Device | *ha.MQTTDeviceDetails | device | |
DeviceClass | DeviceClass | device_class | Allowed: "awning", "blind", "curtain", "damper", "door", "garage", "gate", "shade", "shutter", "window". Over MQTT, Entity reports it as the "device_class" attribute. |
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. |
MessageExpiryInterval | *map[string]any | message_expiry_interval | Retention time interval for published message. |
Name | *string | name | |
Optimistic | *bool | optimistic | Default: false |
Origin | *ha.DeviceOrigin | origin | |
PayloadAvailable | *string | payload_available | The payload that indicates the device is available (defaults to 'online'). Default: "online" |
PayloadClose | *string | payload_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 | *string | payload_not_available | The payload that indicates the device is not available (defaults to 'offline'). Default: "offline" |
PayloadOpen | *string | payload_open | Entity.OpenCover publishes it. Device.OnOpenCoverCommand matches the received payload against it. Default: "OPEN" |
PayloadStop | *string | payload_stop | Entity.StopCover publishes it. Device.OnStopCoverCommand matches the received payload against it. Default: "STOP" |
PayloadStopTilt | *string | payload_stop_tilt | The payload sent when a "stop tilt" command is issued. Default: "STOP" |
PositionClosed | *int | position_closed | Default: 0 |
PositionOpen | *int | position_open | Default: 100 |
PositionTemplate | *ha.Template | position_template | Only Home Assistant renders templates: with it set, Entity skips position_topic when reading state. |
PositionTopic | *string | position_topic | Over MQTT, Entity reads the "current_position" attribute from it. Device.ReportCurrentCoverPosition publishes to it. NewDevice defaults it to <base>/position/state. |
Qos | *int | qos | Default: 0 |
Retain | *bool | retain | Default: false |
SetPositionTemplate | *ha.Template | set_position_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to set_position_topic. |
SetPositionTopic | *string | set_position_topic | Entity.SetPosition publishes to it. Device.OnPositionCommand receives what is published to it. NewDevice defaults it to <base>/set_position/set. |
StateClosed | *string | state_closed | The payload received at the state topic that represents the "closed" state. Default: "closed" |
StateClosing | *string | state_closing | The payload received at the state topic that represents the "closing" state. Default: "closing" |
StateOpen | *string | state_open | The payload received at the state topic that represents the "open" state. Default: "open" |
StateOpening | *string | state_opening | The payload received at the state topic that represents the "opening" state. Default: "opening" |
StateStopped | *string | state_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 | *string | state_topic | Over MQTT, Entity reads the entity state from it. Device.ReportState publishes to it. NewDevice defaults it to <base>/state. |
TiltClosedValue | *int | tilt_closed_value | The value that will be sent to the "tilt command topic" when the cover tilt is closed. Default: 0 |
TiltCommandTemplate | *ha.Template | tilt_command_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to tilt_command_topic. |
TiltCommandTopic | *string | tilt_command_topic | Entity.SetTiltPosition publishes to it. Device.OnTiltPositionCommand receives what is published to it. NewDevice defaults it to <base>/tilt/set. |
TiltMax | *int | tilt_max | The maximum tilt value. Default: 100 |
TiltMin | *int | tilt_min | The minimum tilt value. Default: 0 |
TiltOpenedValue | *int | tilt_opened_value | The value that will be sent to the "tilt command topic" when the cover tilt is opened. Default: 100 |
TiltOptimistic | *bool | tilt_optimistic | Default: false |
TiltStatusTemplate | *ha.Template | tilt_status_template | Only Home Assistant renders templates: with it set, Entity skips tilt_status_topic when reading state. |
TiltStatusTopic | *string | tilt_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 | *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).