lock

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

Overview

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

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

Home Assistant documents the domain here:

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

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

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

Control

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

Serve

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

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
Lock lock.lock command_topic
Open lock.open command_topic
Unlock lock.unlock command_topic

Control

type Entity struct

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

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

Lock calls lock.lock, or publishes payload_lock to command_topic. Core describes the service: Locks a lock.

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

Open calls lock.open, or publishes payload_open to command_topic. Core describes the service: Opens a lock.

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

Unlock calls lock.unlock, or publishes payload_unlock to command_topic. Core describes the service: Unlocks a lock.

type State = entity.State[Attributes]

State is a snapshot of one lock entity.

func Code(value string) entity.ServiceOption

Code sets the "code" service data field.

Serve over MQTT

type Device struct

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

OnLockCommand registers the callback for Home Assistant requesting lock.

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

OnOpenCommand registers the callback for Home Assistant requesting open.

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

OnUnlockCommand registers the callback for Home Assistant requesting unlock.

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

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

FieldTypeWire keyDescription
ChangedBy*stringchanged_by

Changed by.

CodeFormat*stringcode_format

Code format. Over MQTT it comes from the code_format option.

SupportedFeaturesFeaturesupported_features

A set of Feature flags.

Feature

type Feature uint64

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

ConstantValue
FeatureUnknown0
FeatureOpen1 << 0
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 LockState.

ConstantValue
StateValueUnknown"Unknown"
StateValueJammed"jammed"
StateValueOpening"opening"
StateValueLocking"locking"
StateValueOpen"open"
StateValueUnlocking"unlocking"
StateValueLocked"locked"
StateValueUnlocked"unlocked"
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 lock.

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

CodeFormat*stringcode_format

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

CommandTemplate*ha.Templatecommand_template

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

CommandTopicstringcommand_topic

Required. Entity.Lock publishes to it. Entity.Open publishes to it. Entity.Unlock publishes to it. Device.OnLockCommand receives what is published to it. Device.OnOpenCommand receives what is published to it. Device.OnUnlockCommand receives what is published to it. NewDevice defaults it to <base>/lock/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

Origin*ha.DeviceOriginorigin
PayloadAvailable*stringpayload_available

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

Default: "online"

PayloadLock*stringpayload_lock

The payload sent when a "lock" command is issued. Entity.Lock publishes it. Device.OnLockCommand matches the received payload against it.

Default: "LOCK"

PayloadNotAvailable*stringpayload_not_available

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

Default: "offline"

PayloadOpen*stringpayload_open

Entity.Open publishes it. Device.OnOpenCommand matches the received payload against it.

PayloadReset*stringpayload_reset

Default: "None"

PayloadUnlock*stringpayload_unlock

The payload sent when an "unlock" command is issued. Entity.Unlock publishes it. Device.OnUnlockCommand matches the received payload against it.

Default: "UNLOCK"

Qos*intqos

Default: 0

Retain*boolretain

Default: false

StateJammed*stringstate_jammed

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

Default: "JAMMED"

StateLocked*stringstate_locked

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

Default: "LOCKED"

StateLocking*stringstate_locking

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

Default: "LOCKING"

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"

StateTopic*stringstate_topic

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

StateUnlocked*stringstate_unlocked

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

Default: "UNLOCKED"

StateUnlocking*stringstate_unlocking

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

Default: "UNLOCKING"

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