Overview
Package scene is Home Assistant's scene domain: its types and its MQTT discovery config.
Entity controls one scene entity over HTTP or MQTT. Device serves one over MQTT.
No method for scene.apply, scene.create and scene.reload, which target no entity: call api.Client.CallService.
Home Assistant documents the domain here:
Integration: https://www.home-assistant.io/integrations/scene/
MQTT discovery: https://www.home-assistant.io/integrations/scene.mqtt/
Entity model (developers): https://contributors.home-assistant.io/core/entities/scene/
Control
// client is an *api.Client or a connected *mqtt.Client
target := scene.Get(client, "my_scene")
state, err := target.State(ctx)
err = target.Delete(ctx)Serve
// mqttClient is a connected *mqtt.Client
device, err := scene.NewDevice(ctx, mqttClient, "my-scene",
scene.WithName("My scene"),
)
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 |
|---|---|---|
Delete |
scene.delete |
not available |
TurnOn |
scene.turn_on |
command_topic |
Services without a method
Call these with the REST or WebSocket client.
| Service | Why |
|---|---|
scene.apply | it targets no entity |
scene.create | it targets no entity |
scene.reload | it targets no entity |
Control
type Entity struct
Entity controls one scene 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 scene entity with the given id: an entity id over HTTP (the "scene." 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 scene entity the transport can see: all of Home Assistant's over HTTP, the discovered ones over MQTT.
func (e *Entity) Delete(ctx context.Context) error
Delete calls scene.delete. It has no MQTT equivalent. Core describes the service: Deletes a dynamically created scene.
func (e *Entity) TurnOn(ctx context.Context, options ...entity.ServiceOption) error
TurnOn calls scene.turn_on, or publishes payload_on to command_topic. Core describes the service: Activates a scene.
type State = entity.State[Attributes]
State is a snapshot of one scene entity.
func Transition(value float64) entity.ServiceOption
Transition sets the "transition" service data field.
Serve over MQTT
type Device struct
Device is one scene 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 scene 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>/scene/<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) OnTurnOnCommand(fn func(ctx context.Context) error)
OnTurnOnCommand registers the callback for Home Assistant requesting turn_on.
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 scene entity, under the names Home Assistant publishes them with.
See: https://www.home-assistant.io/integrations/scene/
| Field | Type | Wire key | Description |
|---|---|---|---|
EntityId | []string | entity_id | |
Id | *string | id |
MQTT discovery config
type Config struct
Config is the MQTT discovery config of scene.
See: https://www.home-assistant.io/integrations/scene.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. |
CommandTopic | string | command_topic | Required. Entity.TurnOn publishes to it. Device.OnTurnOnCommand receives what is published to it. NewDevice defaults it to <base>/turn_on/set. |
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 | |
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 | |
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" |
PayloadOn | *string | payload_on | The payload that represents the "on" state. Entity.TurnOn publishes it. Device.OnTurnOnCommand matches the received payload against it. |
Qos | *int | qos | Default: 0 |
Retain | *bool | retain | Default: false |
UniqueId | *string | unique_id | |
VisibleByDefault | *bool | visible_by_default | Default: true |
func (*Config) IsMQTTDiscoverySchema()
IsMQTTDiscoverySchema marks Config as a discovery payload (ha.MQTTDiscoverySchema).