Function signature
SubscribeStates sends a SubscribeStatesRequest to the device, registers handlers for all known state response types, and returns an unsubscribe function. Call the unsubscribe function to deregister all handlers.
Handling state messages
The handler receives aproto.Message. Use a type switch to dispatch to the correct concrete type. The Key field on each message matches the entity key from ListEntities.
Unsubscribing
The returnedunsubscribe function removes all message handlers registered by SubscribeStates and clears the saved handler so it will not be re-registered on reconnect.
Reconnect behaviour
The handler passed toSubscribeStates is saved internally. If auto-reconnect is enabled and the connection is re-established, the client automatically calls ListEntities and re-registers the saved handler without any action required from your code. See Auto-reconnect for configuration details.
State handlers run on the client’s read loop goroutine. Do not perform blocking operations (database writes, HTTP requests, long computations) directly inside the handler. Offload heavy work to a separate goroutine using a channel or
go func().Supported state response types
The followingproto.Message types can appear in the handler:
| Type | Domain |
|---|---|
*pb.SensorStateResponse | sensor |
*pb.BinarySensorStateResponse | binary_sensor |
*pb.SwitchStateResponse | switch |
*pb.LightStateResponse | light |
*pb.CoverStateResponse | cover |
*pb.FanStateResponse | fan |
*pb.ClimateStateResponse | climate |
*pb.NumberStateResponse | number |
*pb.SelectStateResponse | select |
*pb.SirenStateResponse | siren |
*pb.LockStateResponse | lock |
*pb.MediaPlayerStateResponse | media_player |
*pb.TextSensorStateResponse | text_sensor |
*pb.CameraImageResponse | camera |
*pb.WaterHeaterStateResponse | water_heater |