Overview
Package valve is Home Assistant's valve domain: its types and its MQTT discovery config.
Entity controls one valve entity over HTTP or MQTT. Device serves one over MQTT.
Home Assistant documents the domain here:
Integration: https://www.home-assistant.io/integrations/valve/
MQTT discovery: https://www.home-assistant.io/integrations/valve.mqtt/
Entity model (developers): https://contributors.home-assistant.io/core/entities/valve/
Control
// client is an *api.Client or a connected *mqtt.Client
target := valve.Get(client, "my_valve")
state, err := target.State(ctx)
err = target.CloseValve(ctx)Serve
// mqttClient is a connected *mqtt.Client
device, err := valve.NewDevice(ctx, mqttClient, "my-valve",
valve.WithName("My valve"),
)
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 |
valve.set_valve_position |
command_topic |
CloseValve |
valve.close_valve |
command_topic |
OpenValve |
valve.open_valve |
command_topic |
StopValve |
valve.stop_valve |
command_topic |
Toggle |
valve.toggle |
not available |
Control
type Entity struct
Entity controls one valve 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 valve entity with the given id: an entity id over HTTP (the "valve." 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 valve entity the transport can see: all of Home Assistant's over HTTP, the discovered ones over MQTT.
func (e *Entity) CloseValve(ctx context.Context) error
CloseValve calls valve.close_valve, or publishes payload_close to command_topic. Core describes the service: Closes a valve.
func (e *Entity) OpenValve(ctx context.Context) error
OpenValve calls valve.open_valve, or publishes payload_open to command_topic. Core describes the service: Opens a valve.
func (e *Entity) SetPosition(ctx context.Context, position float64) error
SetPosition calls valve.set_valve_position, or publishes to command_topic. Core describes the service: Moves a valve to a specific position.
func (e *Entity) StopValve(ctx context.Context) error
StopValve calls valve.stop_valve, or publishes payload_stop to command_topic. Core describes the service: Stops a valve.
func (e *Entity) Toggle(ctx context.Context) error
Toggle calls valve.toggle. It has no MQTT equivalent. Core describes the service: Toggles a valve open/closed.
type State = entity.State[Attributes]
State is a snapshot of one valve entity.
Serve over MQTT
type Device struct
Device is one valve 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 valve 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>/valve/<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) OnCloseValveCommand(fn func(ctx context.Context) error)
OnCloseValveCommand registers the callback for Home Assistant requesting close_valve.
func (d *Device) OnOpenValveCommand(fn func(ctx context.Context) error)
OnOpenValveCommand registers the callback for Home Assistant requesting open_valve.
func (d *Device) OnStopValveCommand(fn func(ctx context.Context) error)
OnStopValveCommand registers the callback for Home Assistant requesting stop_valve.
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 valve entity, under the names Home Assistant publishes them with.
See: https://www.home-assistant.io/integrations/valve/
| Field | Type | Wire key | Description |
|---|---|---|---|
CurrentValvePosition | *int | current_position | Position. |
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. |
DeviceClass
type DeviceClass string
DeviceClass is core's ValveDeviceClass.
| Constant | Value |
|---|---|
DeviceClassUnknown | "Unknown" |
DeviceClassWater | "water" |
DeviceClassGas | "gas" |
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 valve supported features, with the values of core's ValveEntityFeature: the supported_features attribute decodes into it.
| Constant | Value |
|---|---|
FeatureUnknown | 0 |
FeatureOpen | 1 << 0 |
FeatureClose | 1 << 1 |
FeatureSetPosition | 1 << 2 |
FeatureStop | 1 << 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.
StateValue
type StateValue string
StateValue is core's ValveState.
| Constant | Value |
|---|---|
StateValueUnknown | "Unknown" |
StateValueOpening | "opening" |
StateValueClosing | "closing" |
StateValueClosed | "closed" |
StateValueOpen | "open" |
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 valve.
See: https://www.home-assistant.io/integrations/valve.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. |
CommandTemplate | *ha.Template | command_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to command_topic. |
CommandTopic | *string | command_topic | Entity.SetPosition publishes to it. Entity.CloseValve publishes to it. Entity.OpenValve publishes to it. Entity.StopValve publishes to it. Device.OnCloseValveCommand receives what is published to it. Device.OnOpenValveCommand receives what is published to it. Device.OnStopValveCommand receives what is published to it. NewDevice defaults it to <base>/close_valve/set. |
DefaultEntityId | *string | default_entity_id | |
Device | *ha.MQTTDeviceDetails | device | |
DeviceClass | DeviceClass | device_class | Allowed: "water", "gas". 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.CloseValve publishes it. Device.OnCloseValveCommand matches the received payload against it. |
PayloadNotAvailable | *string | payload_not_available | The payload that indicates the device is not available (defaults to 'offline'). Default: "offline" |
PayloadOpen | *string | payload_open | Entity.OpenValve publishes it. Device.OnOpenValveCommand matches the received payload against it. |
PayloadStop | *string | payload_stop | Entity.StopValve publishes it. Device.OnStopValveCommand matches the received payload against it. |
PositionClosed | *int | position_closed | Default: 0 |
PositionOpen | *int | position_open | Default: 100 |
Qos | *int | qos | Default: 0 |
ReportsPosition | *bool | reports_position | Set this option if the valve reports the position or supports setting the position. Enabling this option will cause the position to be published instead of a payload defined by payload "open", payload "close" or payload "stop". When receiving messages, state topic will accept numeric payloads or one of the configured state messages. Default: false |
Retain | *bool | retain | Default: false |
StateClosed | *string | state_closed | The payload received at the state topic that represents the "closed" state. |
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. |
StateOpening | *string | state_opening | The payload received at the state topic that represents the "opening" state. Default: "opening" |
StateTopic | *string | state_topic | Over MQTT, Entity reads the entity state from it. Device.ReportState publishes to it. NewDevice defaults it to <base>/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).