Overview
Package fan is Home Assistant's fan domain: its types and its MQTT discovery config.
Entity controls one fan entity over HTTP or MQTT. Device serves one over MQTT.
Home Assistant documents the domain here:
Integration: https://www.home-assistant.io/integrations/fan/
MQTT discovery: https://www.home-assistant.io/integrations/fan.mqtt/
Entity model (developers): https://contributors.home-assistant.io/core/entities/fan/
Control
// client is an *api.Client or a connected *mqtt.Client
target := fan.Get(client, "my_fan")
state, err := target.State(ctx)
err = target.DecreaseSpeed(ctx)Serve
// mqttClient is a connected *mqtt.Client
device, err := fan.NewDevice(ctx, mqttClient, "my-fan",
fan.WithName("My fan"),
)
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 |
|---|---|---|
SetDirection |
fan.set_direction |
direction_command_topic |
SetPercentage |
fan.set_percentage |
percentage_command_topic |
SetPresetMode |
fan.set_preset_mode |
preset_mode_command_topic |
DecreaseSpeed |
fan.decrease_speed |
not available |
IncreaseSpeed |
fan.increase_speed |
not available |
Toggle |
fan.toggle |
not available |
TurnOff |
fan.turn_off |
command_topic |
TurnOn |
fan.turn_on |
command_topic |
Services without a method
Call these with the REST or WebSocket client.
| Service | Why |
|---|---|
fan.oscillate | needs required parameters; not offered yet |
Control
type Entity struct
Entity controls one fan 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 fan entity with the given id: an entity id over HTTP (the "fan." 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 fan entity the transport can see: all of Home Assistant's over HTTP, the discovered ones over MQTT.
func (e *Entity) DecreaseSpeed(ctx context.Context, options ...entity.ServiceOption) error
DecreaseSpeed calls fan.decrease_speed. It has no MQTT equivalent. Core describes the service: Decreases the speed of a fan.
func (e *Entity) IncreaseSpeed(ctx context.Context, options ...entity.ServiceOption) error
IncreaseSpeed calls fan.increase_speed. It has no MQTT equivalent. Core describes the service: Increases the speed of a fan.
func (e *Entity) SetDirection(ctx context.Context, direction Direction) error
SetDirection calls fan.set_direction, or publishes to direction_command_topic. Core describes the service: Sets a fan's rotation direction.
func (e *Entity) SetPercentage(ctx context.Context, percentage float64) error
SetPercentage calls fan.set_percentage, or publishes to percentage_command_topic. Core describes the service: Sets the speed of a fan.
func (e *Entity) SetPresetMode(ctx context.Context, mode string) error
SetPresetMode calls fan.set_preset_mode, or publishes to preset_mode_command_topic. Core describes the service: Sets the preset mode of a fan.
func (e *Entity) Toggle(ctx context.Context) error
Toggle calls fan.toggle. It has no MQTT equivalent. Core describes the service: Toggles a fan on/off.
func (e *Entity) TurnOff(ctx context.Context) error
TurnOff calls fan.turn_off, or publishes payload_off to command_topic. Core describes the service: Turns off a fan.
func (e *Entity) TurnOn(ctx context.Context, options ...entity.ServiceOption) error
TurnOn calls fan.turn_on, or publishes payload_on to command_topic. Core describes the service: Turns on a fan.
type State = entity.State[Attributes]
State is a snapshot of one fan entity.
func Percentage(value float64) entity.ServiceOption
Percentage sets the "percentage" service data field.
func PercentageStep(value float64) entity.ServiceOption
PercentageStep sets the "percentage_step" service data field.
func PresetMode(value string) entity.ServiceOption
PresetMode sets the "preset_mode" service data field.
Serve over MQTT
type Device struct
Device is one fan 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 fan 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>/fan/<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) OnDirectionCommand(fn func(ctx context.Context, direction Direction) error)
OnDirectionCommand registers the callback for Home Assistant requesting a change of direction.
func (d *Device) OnPercentageCommand(fn func(ctx context.Context, percentage float64) error)
OnPercentageCommand registers the callback for Home Assistant requesting a change of percentage.
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) OnPresetModeCommand(fn func(ctx context.Context, mode string) error)
OnPresetModeCommand registers the callback for Home Assistant requesting a change of preset_mode.
func (d *Device) ReportCurrentDirection(ctx context.Context, direction Direction, opts ...mqtt.StateOption) error
ReportCurrentDirection reports the current_direction on direction_state_topic. opts override the client's state QoS and retain defaults for this publish.
func (d *Device) ReportOscillation(ctx context.Context, oscillation string, opts ...mqtt.StateOption) error
ReportOscillation reports the oscillation on oscillation_state_topic. opts override the client's state QoS and retain defaults for this publish.
func (d *Device) ReportPercentage(ctx context.Context, percentage float64, opts ...mqtt.StateOption) error
ReportPercentage reports the percentage on percentage_state_topic. opts override the client's state QoS and retain defaults for this publish.
func (d *Device) ReportPresetMode(ctx context.Context, mode string, opts ...mqtt.StateOption) error
ReportPresetMode reports the preset_mode on preset_mode_state_topic. opts override the client's state QoS and retain defaults for this publish.
func (d *Device) ReportState(ctx context.Context, on bool, opts ...mqtt.StateOption) error
ReportState reports the entity as on or off 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.
func WithPresetModes(modes ...string) DeviceOption
WithPresetModes declares the supported preset_mode options and enables their topics.
State and values
type Attributes struct
Attributes are the state attributes of a fan entity, under the names Home Assistant publishes them with.
See: https://www.home-assistant.io/integrations/fan/
| Field | Type | Wire key | Description |
|---|---|---|---|
CurrentDirection | *string | direction | Direction. Over MQTT it comes from direction_state_topic. |
Oscillating | *bool | oscillating | Oscillating. |
Percentage | *int | percentage | Speed. Over MQTT it comes from percentage_state_topic. |
PercentageStep | float64 | percentage_step | Speed step. |
PresetMode | *string | preset_mode | Preset mode. Over MQTT it comes from preset_mode_state_topic. |
PresetModes | *[]string | preset_modes | Available preset modes. Over MQTT it comes from the preset_modes option. |
SupportedFeatures | Feature | supported_features | A set of Feature flags. |
Direction
type Direction string
Direction lists the built-in values of the direction attribute, as Home Assistant translates them. Integrations may use others, which are kept as they are.
| Constant | Value |
|---|---|
DirectionUnknown | "Unknown" |
DirectionForward | "forward" |
DirectionReverse | "reverse" |
func DirectionFromString(s string) Direction
DirectionFromString returns s as a Direction.
func (v *Direction) UnmarshalJSON(data []byte) error
UnmarshalJSON reads any spelling DirectionFromString accepts.
Feature
type Feature uint64
Feature is a set of fan supported features, with the values of core's FanEntityFeature: the supported_features attribute decodes into it.
| Constant | Value |
|---|---|
FeatureUnknown | 0 |
FeatureSetSpeed | 1 << 0 |
FeatureOscillate | 1 << 1 |
FeatureDirection | 1 << 2 |
FeaturePresetMode | 1 << 3 |
FeatureTurnOff | 1 << 4 |
FeatureTurnOn | 1 << 5 |
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.
MQTT discovery config
type Config struct
Config is the MQTT discovery config of fan.
See: https://www.home-assistant.io/integrations/fan.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 | Required. Entity.TurnOff publishes to it. Entity.TurnOn publishes to it. Device.OnPowerCommand receives what is published to it. NewDevice defaults it to <base>/set. |
DefaultEntityId | *string | default_entity_id | |
Device | *ha.MQTTDeviceDetails | device | |
DirectionCommandTemplate | *ha.Template | direction_command_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to direction_command_topic. |
DirectionCommandTopic | *string | direction_command_topic | Entity.SetDirection publishes to it. Device.OnDirectionCommand receives what is published to it. NewDevice defaults it to <base>/direction/set. |
DirectionStateTopic | *string | direction_state_topic | Over MQTT, Entity reads the "direction" attribute from it. Device.ReportCurrentDirection publishes to it. NewDevice defaults it to <base>/direction/state. |
DirectionValueTemplate | *ha.Template | direction_value_template | Only Home Assistant renders templates: with it set, Entity skips direction_state_topic when reading state. |
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 | |
OscillationCommandTemplate | *ha.Template | oscillation_command_template | |
OscillationCommandTopic | *string | oscillation_command_topic | The library does not use it: a boolean over payloads the config chooses, which no setter can pick between. |
OscillationStateTopic | *string | oscillation_state_topic | Over MQTT, Entity watches it but does not decode its payload. Device.ReportOscillation publishes to it. NewDevice defaults it to <base>/oscillation/state. |
OscillationValueTemplate | *ha.Template | oscillation_value_template | Only Home Assistant renders templates: with it set, Entity skips oscillation_state_topic when reading state. |
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. Entity compares what state_topic says with it. Device.OnPowerCommand matches the received payload against it. Device.ReportState publishes it. Default: "OFF" |
PayloadOn | *string | payload_on | The payload that represents the "on" state. Entity.TurnOn publishes it. Entity compares what state_topic says with it. Device.OnPowerCommand matches the received payload against it. Device.ReportState publishes it. Default: "ON" |
PayloadOscillationOff | *string | payload_oscillation_off | The payload that represents the oscillation "off" state. Default: "oscillate_off" |
PayloadOscillationOn | *string | payload_oscillation_on | The payload that represents the oscillation "on" state. Default: "oscillate_on" |
PayloadResetPercentage | *string | payload_reset_percentage | A special payload that resets the fan speed percentage state attribute to unknown when received at the percentage state topic. Default: "None" |
PayloadResetPresetMode | *string | payload_reset_preset_mode | A special payload that resets the fan preset mode state attribute to unknown when received at the preset mode state topic. Default: "None" |
PercentageCommandTemplate | *ha.Template | percentage_command_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to percentage_command_topic. |
PercentageCommandTopic | *string | percentage_command_topic | Entity.SetPercentage publishes to it. Device.OnPercentageCommand receives what is published to it. NewDevice defaults it to <base>/percentage/set. |
PercentageStateTopic | *string | percentage_state_topic | Over MQTT, Entity reads the "percentage" attribute from it. Device.ReportPercentage publishes to it. NewDevice defaults it to <base>/percentage/state. |
PercentageValueTemplate | *ha.Template | percentage_value_template | Only Home Assistant renders templates: with it set, Entity skips percentage_state_topic when reading state. |
PresetModeCommandTemplate | *ha.Template | preset_mode_command_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to preset_mode_command_topic. |
PresetModeCommandTopic | *string | preset_mode_command_topic | Entity.SetPresetMode publishes to it. Device.OnPresetModeCommand receives what is published to it. NewDevice defaults it to <base>/preset_mode/set when preset_modes is set. |
PresetModeStateTopic | *string | preset_mode_state_topic | Over MQTT, Entity reads the "preset_mode" attribute from it. Device.ReportPresetMode publishes to it. NewDevice defaults it to <base>/preset_mode/state when preset_modes is set. |
PresetModeValueTemplate | *ha.Template | preset_mode_value_template | Only Home Assistant renders templates: with it set, Entity skips preset_mode_state_topic when reading state. |
PresetModes | []string | preset_modes | Over MQTT, Entity reports it as the "preset_modes" attribute. Set it with WithPresetModes. |
Qos | *int | qos | Default: 0 |
Retain | *bool | retain | Default: false |
SpeedRangeMax | *int | speed_range_max | The maximum of numeric output range (representing 100 %). The percentage step is 100 / number of speeds within the "speed range". Default: 100 |
SpeedRangeMin | *int | speed_range_min | The minimum of numeric output range (off not included, so speed_range_min - 1 represents 0 %). The percentage step is 100 / the number of speeds within the "speed range". Default: 1 |
StateTopic | *string | state_topic | Over MQTT, Entity reads the entity state from it. Device.ReportState publishes to it. NewDevice defaults it to <base>/state. |
StateValueTemplate | *ha.Template | state_value_template | Only Home Assistant renders templates: with it set, Entity skips state_topic when reading state. |
UniqueId | *string | unique_id | |
VisibleByDefault | *bool | visible_by_default | Default: true |
func (*Config) IsMQTTDiscoverySchema()
IsMQTTDiscoverySchema marks Config as a discovery payload (ha.MQTTDiscoverySchema).