Overview
Package humidifier is Home Assistant's humidifier domain: its types and its MQTT discovery config.
Entity controls one humidifier entity over HTTP or MQTT. Device serves one over MQTT.
Home Assistant documents the domain here:
Integration: https://www.home-assistant.io/integrations/humidifier/
MQTT discovery: https://www.home-assistant.io/integrations/humidifier.mqtt/
Entity model (developers): https://contributors.home-assistant.io/core/entities/humidifier/
Control
// client is an *api.Client or a connected *mqtt.Client
target := humidifier.Get(client, "my_humidifier")
state, err := target.State(ctx)
err = target.Toggle(ctx)Serve
// mqttClient is a connected *mqtt.Client
device, err := humidifier.NewDevice(ctx, mqttClient, "my-humidifier",
humidifier.WithName("My humidifier"),
)
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 |
|---|---|---|
SetMode |
humidifier.set_mode |
mode_command_topic |
SetHumidity |
humidifier.set_humidity |
target_humidity_command_topic |
Toggle |
humidifier.toggle |
not available |
TurnOff |
humidifier.turn_off |
command_topic |
TurnOn |
humidifier.turn_on |
command_topic |
Control
type Entity struct
Entity controls one humidifier 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 humidifier entity with the given id: an entity id over HTTP (the "humidifier." 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 humidifier entity the transport can see: all of Home Assistant's over HTTP, the discovered ones over MQTT.
func (e *Entity) SetHumidity(ctx context.Context, humidity float64) error
SetHumidity calls humidifier.set_humidity, or publishes to target_humidity_command_topic. Core describes the service: Sets the target humidity of a humidifier.
func (e *Entity) SetMode(ctx context.Context, mode Mode) error
SetMode calls humidifier.set_mode, or publishes to mode_command_topic. Core describes the service: Sets the mode of a humidifier.
func (e *Entity) Toggle(ctx context.Context) error
Toggle calls humidifier.toggle. It has no MQTT equivalent. Core describes the service: Toggles a humidifier on/off.
func (e *Entity) TurnOff(ctx context.Context) error
TurnOff calls humidifier.turn_off, or publishes payload_off to command_topic. Core describes the service: Turns off a humidifier.
func (e *Entity) TurnOn(ctx context.Context) error
TurnOn calls humidifier.turn_on, or publishes payload_on to command_topic. Core describes the service: Turns on a humidifier.
type State = entity.State[Attributes]
State is a snapshot of one humidifier entity.
Serve over MQTT
type Device struct
Device is one humidifier 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 humidifier 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>/humidifier/<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) OnHumidityCommand(fn func(ctx context.Context, humidity float64) error)
OnHumidityCommand registers the callback for Home Assistant requesting a change of humidity.
func (d *Device) OnModeCommand(fn func(ctx context.Context, mode Mode) error)
OnModeCommand registers the callback for Home Assistant requesting a change of 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) ReportAction(ctx context.Context, action Action, opts ...mqtt.StateOption) error
ReportAction reports the action on action_topic. opts override the client's state QoS and retain defaults for this publish.
func (d *Device) ReportCurrentHumidity(ctx context.Context, currentHumidity float64, opts ...mqtt.StateOption) error
ReportCurrentHumidity reports the current_humidity on current_humidity_topic. opts override the client's state QoS and retain defaults for this publish.
func (d *Device) ReportMode(ctx context.Context, mode Mode, opts ...mqtt.StateOption) error
ReportMode reports the mode on 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.
func (d *Device) ReportTargetHumidity(ctx context.Context, humidity float64, opts ...mqtt.StateOption) error
ReportTargetHumidity reports the target_humidity on target_humidity_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 WithModes(modes ...Mode) DeviceOption
WithModes declares the supported mode options and enables their topics.
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 humidifier entity, under the names Home Assistant publishes them with.
See: https://www.home-assistant.io/integrations/humidifier/
| Field | Type | Wire key | Description |
|---|---|---|---|
Action | *Action | action | Action. Over MQTT it comes from action_topic. |
AvailableModes | *[]string | available_modes | Available modes. Over MQTT it comes from the modes option. |
CurrentHumidity | *float64 | current_humidity | Current humidity. Over MQTT it comes from current_humidity_topic. |
DeviceClass | *DeviceClass | device_class | Over MQTT it comes from the device_class option. |
TargetHumidity | *float64 | humidity | Target humidity. Over MQTT it comes from target_humidity_state_topic. |
MaxHumidity | float64 | max_humidity | Max target humidity. Over MQTT it comes from the max_humidity option. |
MinHumidity | float64 | min_humidity | Min target humidity. Over MQTT it comes from the min_humidity option. |
Mode | *string | mode | Mode. Over MQTT it comes from mode_state_topic. |
SupportedFeatures | Feature | supported_features | A set of Feature flags. |
TargetHumidityStep | *float64 | target_humidity_step |
Action
type Action string
Action is core's HumidifierAction.
| Constant | Value |
|---|---|
ActionUnknown | "Unknown" |
ActionHumidifying | "humidifying" |
ActionDrying | "drying" |
ActionIdle | "idle" |
ActionOff | "off" |
func ActionFromString(s string) Action
ActionFromString returns the Action spelled s, by its value or by the name of core's member, and ActionUnknown for anything else.
func (v *Action) UnmarshalJSON(data []byte) error
UnmarshalJSON reads any spelling ActionFromString accepts.
DeviceClass
type DeviceClass string
DeviceClass is core's HumidifierDeviceClass.
| Constant | Value |
|---|---|
DeviceClassUnknown | "Unknown" |
DeviceClassHumidifier | "humidifier" |
DeviceClassDehumidifier | "dehumidifier" |
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 humidifier supported features, with the values of core's HumidifierEntityFeature: the supported_features attribute decodes into it.
| Constant | Value |
|---|---|
FeatureUnknown | 0 |
FeatureModes | 1 << 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.
Mode
type Mode string
Mode lists the built-in values of the mode attribute, as Home Assistant translates them. Integrations may use others, which are kept as they are.
| Constant | Value |
|---|---|
ModeUnknown | "Unknown" |
ModeAuto | "auto" |
ModeAway | "away" |
ModeBaby | "baby" |
ModeBoost | "boost" |
ModeComfort | "comfort" |
ModeEco | "eco" |
ModeHome | "home" |
ModeNormal | "normal" |
ModeSleep | "sleep" |
func ModeFromString(s string) Mode
ModeFromString returns s as a Mode.
func (v *Mode) UnmarshalJSON(data []byte) error
UnmarshalJSON reads any spelling ModeFromString accepts.
MQTT discovery config
type Config struct
Config is the MQTT discovery config of humidifier.
See: https://www.home-assistant.io/integrations/humidifier.mqtt/
| Field | Type | Wire key | Description |
|---|---|---|---|
ActionTemplate | *ha.Template | action_template | Only Home Assistant renders templates: with it set, Entity skips action_topic when reading state. |
ActionTopic | *string | action_topic | Over MQTT, Entity reads the "action" attribute from it. Device.ReportAction publishes to it. NewDevice defaults it to <base>/action/state. |
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. |
CurrentHumidityTemplate | *ha.Template | current_humidity_template | Only Home Assistant renders templates: with it set, Entity skips current_humidity_topic when reading state. |
CurrentHumidityTopic | *string | current_humidity_topic | Over MQTT, Entity reads the "current_humidity" attribute from it. Device.ReportCurrentHumidity publishes to it. NewDevice defaults it to <base>/current_humidity/state. |
DefaultEntityId | *string | default_entity_id | |
Device | *ha.MQTTDeviceDetails | device | |
DeviceClass | DeviceClass | device_class | Allowed: "humidifier", "dehumidifier", null. Over MQTT, Entity reports it as the "device_class" attribute. Default: "humidifier" |
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. |
MaxHumidity | *float64 | max_humidity | The maximum target humidity that can be set. Over MQTT, Entity reports it as the "max_humidity" attribute. Default: 100 |
MessageExpiryInterval | *map[string]any | message_expiry_interval | Retention time interval for published message. |
MinHumidity | *float64 | min_humidity | The minimum target humidity that can be set. Over MQTT, Entity reports it as the "min_humidity" attribute. Default: 0 |
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.SetMode publishes to it. Device.OnModeCommand receives what is published to it. NewDevice defaults it to <base>/mode/set when modes is 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 "mode" attribute from it. Device.ReportMode publishes to it. NewDevice defaults it to <base>/mode/state when modes is set. |
Modes | []string | modes | Over MQTT, Entity reports it as the "available_modes" attribute. Set it with WithModes. |
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. 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" |
PayloadResetHumidity | *string | payload_reset_humidity | Default: "None" |
PayloadResetMode | *string | payload_reset_mode | Default: "None" |
Qos | *int | qos | Default: 0 |
Retain | *bool | retain | Default: false |
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. |
TargetHumidityCommandTemplate | *ha.Template | target_humidity_command_template | Only Home Assistant renders templates: with it set, Entity refuses to publish to target_humidity_command_topic. |
TargetHumidityCommandTopic | string | target_humidity_command_topic | Required. Entity.SetHumidity publishes to it. Device.OnHumidityCommand receives what is published to it. NewDevice defaults it to <base>/target_humidity/set. |
TargetHumidityStateTemplate | *ha.Template | target_humidity_state_template | Only Home Assistant renders templates: with it set, Entity skips target_humidity_state_topic when reading state. |
TargetHumidityStateTopic | *string | target_humidity_state_topic | Over MQTT, Entity reads the "humidity" attribute from it. Device.ReportTargetHumidity publishes to it. NewDevice defaults it to <base>/target_humidity/state. |
UniqueId | *string | unique_id | |
VisibleByDefault | *bool | visible_by_default | Default: true |
func (*Config) IsMQTTDiscoverySchema()
IsMQTTDiscoverySchema marks Config as a discovery payload (ha.MQTTDiscoverySchema).