Overview
Package waterheater is Home Assistant's water_heater domain: its types and its MQTT discovery config.
Entity controls one water_heater entity over HTTP or MQTT. Device serves one over MQTT.
Home Assistant documents the domain here:
Integration: https://www.home-assistant.io/integrations/water_heater/
MQTT discovery: https://www.home-assistant.io/integrations/water_heater.mqtt/
Entity model (developers): https://contributors.home-assistant.io/core/entities/water-heater/
Control
// client is an *api.Client or a connected *mqtt.Client
target := waterheater.Get(client, "my_water_heater")
state, err := target.State(ctx)
err = target.TurnOff(ctx)Serve
// mqttClient is a connected *mqtt.Client
device, err := waterheater.NewDevice(ctx, mqttClient, "my-water-heater",
waterheater.WithName("My water heater"),
)
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 |
|---|---|---|
SetOperationMode |
water_heater.set_operation_mode |
mode_command_topic |
SetTemperature |
water_heater.set_temperature |
temperature_command_topic |
TurnOff |
water_heater.turn_off |
power_command_topic |
TurnOn |
water_heater.turn_on |
power_command_topic |
Services without a method
Call these with the REST or WebSocket client.
| Service | Why |
|---|---|
water_heater.set_away_mode | needs required parameters; not offered yet |
Control
type Entity struct
Entity controls one water_heater 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 water_heater entity with the given id: an entity id over HTTP (the "water_heater." 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 water_heater entity the transport can see: all of Home Assistant's over HTTP, the discovered ones over MQTT.
func (e *Entity) SetOperationMode(ctx context.Context, mode OperationMode) error
SetOperationMode calls water_heater.set_operation_mode, or publishes to mode_command_topic. Core describes the service: Sets the operation mode of a water heater.
func (e *Entity) SetTemperature(ctx context.Context, temperature float64) error
SetTemperature calls water_heater.set_temperature, or publishes to temperature_command_topic. Core describes the service: Sets the target temperature of a water heater.
func (e *Entity) TurnOff(ctx context.Context) error
TurnOff calls water_heater.turn_off, or publishes payload_off to power_command_topic. Core describes the service: Turns off a water heater.
func (e *Entity) TurnOn(ctx context.Context) error
TurnOn calls water_heater.turn_on, or publishes payload_on to power_command_topic. Core describes the service: Turns on a water heater.
type State = entity.State[Attributes]
State is a snapshot of one water_heater entity.
Serve over MQTT
type Device struct
Device is one water_heater 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 water_heater 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>/water_heater/<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) OnOperationModeCommand(fn func(ctx context.Context, mode OperationMode) error)
OnOperationModeCommand registers the callback for Home Assistant requesting a change of operation_mode.
func (d *Device) OnPowerCommand(fn func(ctx context.Context, on bool) error)
OnPowerCommand registers the callback for Home Assistant turning the device on or off.
func (d *Device) OnTemperatureCommand(fn func(ctx context.Context, temperature float64) error)
OnTemperatureCommand registers the callback for Home Assistant requesting a change of temperature.
func (d *Device) ReportCurrentTemperature(ctx context.Context, currentTemperature float64, opts ...mqtt.StateOption) error
ReportCurrentTemperature reports the current_temperature on current_temperature_topic. opts override the client's state QoS and retain defaults for this publish.
func (d *Device) ReportOperationMode(ctx context.Context, mode OperationMode, opts ...mqtt.StateOption) error
ReportOperationMode reports the operation_mode on mode_state_topic. opts override the client's state QoS and retain defaults for this publish.
func (d *Device) ReportTargetTemperature(ctx context.Context, temperature float64, opts ...mqtt.StateOption) error
ReportTargetTemperature reports the target_temperature on temperature_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.
func WithOperationModes(modes ...OperationMode) DeviceOption
WithOperationModes declares the supported operation_mode options.
State and values
type Attributes struct
Attributes are the state attributes of a water_heater entity, under the names Home Assistant publishes them with.
See: https://www.home-assistant.io/integrations/water_heater/
| Field | Type | Wire key | Description |
|---|---|---|---|
IsAwayModeOn | *string | away_mode | Away mode. |
CurrentTemperature | *float64 | current_temperature | Current temperature. Over MQTT it comes from current_temperature_topic. |
MaxTemp | float64 | max_temp | Max target temperature. Over MQTT it comes from the max_temp option. |
MinTemp | float64 | min_temp | Min target temperature. Over MQTT it comes from the min_temp option. |
OperationList | *[]string | operation_list | Over MQTT it comes from the modes option. |
CurrentOperation | *string | operation_mode | Operation mode. |
SupportedFeatures | Feature | supported_features | A set of Feature flags. |
TargetTemperatureHigh | *float64 | target_temp_high | Upper target temperature. |
TargetTemperatureLow | *float64 | target_temp_low | Lower target temperature. |
TargetTemperatureStep | *float64 | target_temp_step | |
TargetTemperature | *float64 | temperature | Over MQTT it comes from temperature_state_topic. |
TemperatureUnit | string | temperature_unit | Over MQTT it comes from the temperature_unit option. |
AwayMode
type AwayMode string
AwayMode lists the built-in values of the away_mode attribute, as Home Assistant translates them. Integrations may use others, which are kept as they are.
| Constant | Value |
|---|---|
AwayModeUnknown | "Unknown" |
AwayModeOff | "off" |
AwayModeOn | "on" |
func AwayModeFromString(s string) AwayMode
AwayModeFromString returns s as a AwayMode.
func (v *AwayMode) UnmarshalJSON(data []byte) error
UnmarshalJSON reads any spelling AwayModeFromString accepts.
Feature
type Feature uint64
Feature is a set of water_heater supported features, with the values of core's WaterHeaterEntityFeature: the supported_features attribute decodes into it.
| Constant | Value |
|---|---|
FeatureUnknown | 0 |
FeatureTargetTemperature | 1 << 0 |
FeatureOperationMode | 1 << 1 |
FeatureAwayMode | 1 << 2 |
FeatureOnOff | 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.
OperationMode
type OperationMode string
OperationMode lists the built-in values of the operation_mode attribute, as Home Assistant translates them. Integrations may use others, which are kept as they are.
| Constant | Value |
|---|---|
OperationModeUnknown | "Unknown" |
OperationModeEco | "eco" |
OperationModeElectric | "electric" |
OperationModeGas | "gas" |
OperationModeHeatPump | "heat_pump" |
OperationModeHighDemand | "high_demand" |
OperationModeOff | "off" |
OperationModePerformance | "performance" |
func OperationModeFromString(s string) OperationMode
OperationModeFromString returns s as a OperationMode.
func (v *OperationMode) UnmarshalJSON(data []byte) error
UnmarshalJSON reads any spelling OperationModeFromString accepts.
StateValue
type StateValue string
StateValue lists the built-in values of the entity state, as Home Assistant translates them. Integrations may use others, which are kept as they are.
| Constant | Value |
|---|---|
StateValueUnknown | "Unknown" |
StateValueEco | "eco" |
StateValueElectric | "electric" |
StateValueGas | "gas" |
StateValueHeatPump | "heat_pump" |
StateValueHighDemand | "high_demand" |
StateValueOff | "off" |
StateValuePerformance | "performance" |
func StateValueFromString(s string) StateValue
StateValueFromString returns s as a StateValue.
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 water_heater.
See: https://www.home-assistant.io/integrations/water_heater.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. |
CurrentTemperatureTemplate | *ha.Template | current_temperature_template | Only Home Assistant renders templates: with it set, Entity skips current_temperature_topic when reading state. |
CurrentTemperatureTopic | *string | current_temperature_topic | Over MQTT, Entity reads the "current_temperature" attribute from it. Device.ReportCurrentTemperature publishes to it. NewDevice defaults it to <base>/current_temperature/state. |
DefaultEntityId | *string | default_entity_id | |
Device | *ha.MQTTDeviceDetails | device | |
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 | |
Initial | *float64 | initial | The device initializes with this target temperature. |
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. |
MaxTemp | *float64 | max_temp | The maximum target temperature that can be set. Over MQTT, Entity reports it as the "max_temp" attribute. |
MessageExpiryInterval | *map[string]any | message_expiry_interval | Retention time interval for published message. |
MinTemp | *float64 | min_temp | The minimum target temperature that can be set. Over MQTT, Entity reports it as the "min_temp" attribute. |
ModeCommandTemplate | *ha.Template | mode_command_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to mode_command_topic. |
ModeCommandTopic | *string | mode_command_topic | Entity.SetOperationMode publishes to it. Device.OnOperationModeCommand receives what is published to it. NewDevice defaults it to <base>/mode/set. |
ModeStateTemplate | *ha.Template | mode_state_template | Only Home Assistant renders templates: with it set, Entity skips mode_state_topic when reading state. |
ModeStateTopic | *string | mode_state_topic | Over MQTT, Entity reads the entity state from it. Device.ReportOperationMode publishes to it. NewDevice defaults it to <base>/mode/state. |
Modes | []string | modes | Over MQTT, Entity reports it as the "operation_list" attribute. Set it with WithOperationModes. Default: [ "eco", "electric", "gas", "heat_pump", "high_demand", "performance", "off" ] |
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" |
PayloadNotAvailable | *string | payload_not_available | The payload that indicates the device is not available (defaults to 'offline'). Default: "offline" |
PayloadOff | *string | payload_off | The payload that represents the "off" state. Entity.TurnOff publishes it. Device.OnPowerCommand matches the received payload against it. Default: "OFF" |
PayloadOn | *string | payload_on | The payload that represents the "on" state. Entity.TurnOn publishes it. Device.OnPowerCommand matches the received payload against it. Default: "ON" |
PowerCommandTemplate | *ha.Template | power_command_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to power_command_topic. |
PowerCommandTopic | *string | power_command_topic | Entity.TurnOff publishes to it. Entity.TurnOn publishes to it. Device.OnPowerCommand receives what is published to it. NewDevice defaults it to <base>/power/set. |
Precision | *float64 | precision | The precision in degrees the thermostat is working at. Allowed: 0.1, 0.5, 1. |
Qos | *int | qos | Default: 0 |
Retain | *bool | retain | Default: false |
TemperatureCommandTemplate | *ha.Template | temperature_command_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to temperature_command_topic. |
TemperatureCommandTopic | *string | temperature_command_topic | Entity.SetTemperature publishes to it. Device.OnTemperatureCommand receives what is published to it. NewDevice defaults it to <base>/temperature/set. |
TemperatureStateTemplate | *ha.Template | temperature_state_template | Only Home Assistant renders templates: with it set, Entity skips temperature_state_topic when reading state. |
TemperatureStateTopic | *string | temperature_state_topic | Over MQTT, Entity reads the "temperature" attribute from it. Device.ReportTargetTemperature publishes to it. NewDevice defaults it to <base>/temperature/state. |
TemperatureUnit | *string | temperature_unit | This determines the native unit of measurement the MQTT device works with. Over MQTT, Entity reports it as the "temperature_unit" attribute. |
UniqueId | *string | unique_id | |
ValueTemplate | *ha.Template | value_template | 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).