water_heater

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

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.

MethodOver HTTP, callsOver 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.

ServiceWhy
water_heater.set_away_modeneeds 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/

FieldTypeWire keyDescription
IsAwayModeOn*stringaway_mode

Away mode.

CurrentTemperature*float64current_temperature

Current temperature. Over MQTT it comes from current_temperature_topic.

MaxTempfloat64max_temp

Max target temperature. Over MQTT it comes from the max_temp option.

MinTempfloat64min_temp

Min target temperature. Over MQTT it comes from the min_temp option.

OperationList*[]stringoperation_list

Over MQTT it comes from the modes option.

CurrentOperation*stringoperation_mode

Operation mode.

SupportedFeaturesFeaturesupported_features

A set of Feature flags.

TargetTemperatureHigh*float64target_temp_high

Upper target temperature.

TargetTemperatureLow*float64target_temp_low

Lower target temperature.

TargetTemperatureStep*float64target_temp_step
TargetTemperature*float64temperature

Over MQTT it comes from temperature_state_topic.

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

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

ConstantValue
FeatureUnknown0
FeatureTargetTemperature1 << 0
FeatureOperationMode1 << 1
FeatureAwayMode1 << 2
FeatureOnOff1 << 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.

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

ConstantValue
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/

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.

CurrentTemperatureTemplate*ha.Templatecurrent_temperature_template

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

CurrentTemperatureTopic*stringcurrent_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*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
Initial*float64initial

The device initializes with this target temperature.

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.

MaxTemp*float64max_temp

The maximum target temperature that can be set. Over MQTT, Entity reports it as the "max_temp" attribute.

MessageExpiryInterval*map[string]anymessage_expiry_interval

Retention time interval for published message.

MinTemp*float64min_temp

The minimum target temperature that can be set. Over MQTT, Entity reports it as the "min_temp" attribute.

ModeCommandTemplate*ha.Templatemode_command_template

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

ModeCommandTopic*stringmode_command_topic

Entity.SetOperationMode publishes to it. Device.OnOperationModeCommand receives what is published to it. NewDevice defaults it to <base>/mode/set.

ModeStateTemplate*ha.Templatemode_state_template

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

ModeStateTopic*stringmode_state_topic

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

Modes[]stringmodes

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*stringname
Optimistic*booloptimistic

Default: false

Origin*ha.DeviceOriginorigin
PayloadAvailable*stringpayload_available

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

Default: "online"

PayloadNotAvailable*stringpayload_not_available

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

Default: "offline"

PayloadOff*stringpayload_off

The payload that represents the "off" state. Entity.TurnOff publishes it. Device.OnPowerCommand matches the received payload against it.

Default: "OFF"

PayloadOn*stringpayload_on

The payload that represents the "on" state. Entity.TurnOn publishes it. Device.OnPowerCommand matches the received payload against it.

Default: "ON"

PowerCommandTemplate*ha.Templatepower_command_template

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

PowerCommandTopic*stringpower_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*float64precision

The precision in degrees the thermostat is working at. Allowed: 0.1, 0.5, 1.

Qos*intqos

Default: 0

Retain*boolretain

Default: false

TemperatureCommandTemplate*ha.Templatetemperature_command_template

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

TemperatureCommandTopic*stringtemperature_command_topic

Entity.SetTemperature publishes to it. Device.OnTemperatureCommand receives what is published to it. NewDevice defaults it to <base>/temperature/set.

TemperatureStateTemplate*ha.Templatetemperature_state_template

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

TemperatureStateTopic*stringtemperature_state_topic

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

TemperatureUnit*stringtemperature_unit

This determines the native unit of measurement the MQTT device works with. Over MQTT, Entity reports it as the "temperature_unit" attribute.

UniqueId*stringunique_id
ValueTemplate*ha.Templatevalue_template

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