device_tracker

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

Overview

Package devicetracker is Home Assistant's device_tracker domain: its types and its MQTT discovery config.

Entity controls one device_tracker entity over HTTP or MQTT. Device serves one over MQTT.

No method for device_tracker.see, which target no entity: call api.Client.CallService.

Home Assistant documents the domain here:

Integration: https://www.home-assistant.io/integrations/device_tracker/

MQTT discovery: https://www.home-assistant.io/integrations/device_tracker.mqtt/

Entity model (developers): https://contributors.home-assistant.io/core/entities/device-tracker/

Control

// client is an *api.Client or a connected *mqtt.Client
target := devicetracker.Get(client, "my_device_tracker")
state, err := target.State(ctx)

Serve

// mqttClient is a connected *mqtt.Client
device, err := devicetracker.NewDevice(ctx, mqttClient, "my-device-tracker",
	devicetracker.WithName("My device tracker"),
)

Commands

Services without a method

Call these with the REST or WebSocket client.

ServiceWhy
device_tracker.seeit targets no entity

Control

type Entity struct

Entity controls one device_tracker 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 device_tracker entity with the given id: an entity id over HTTP (the "device_tracker." 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 device_tracker entity the transport can see: all of Home Assistant's over HTTP, the discovered ones over MQTT.

type State = entity.State[Attributes]

State is a snapshot of one device_tracker entity.

Serve over MQTT

type Device struct

Device is one device_tracker entity served by this process over MQTT. Close, Delete and ReportAvailability come from the embedded mqtt.Device.

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 device_tracker 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>/device_tracker/<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) ReportState(ctx context.Context, state string, opts ...mqtt.StateOption) error

ReportState reports the state 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.

State and values

type Attributes struct

Attributes are the state attributes of a device_tracker entity, under the names Home Assistant publishes them with.

See: https://www.home-assistant.io/integrations/device_tracker/

FieldTypeWire keyDescription
BatteryLevel*intbattery_level
LocationAccuracyfloat64gps_accuracy

GPS accuracy.

Hostname*stringhost_name
InZones*[]stringin_zones
IpAddress*stringip
Latitude*float64latitude

Latitude.

Longitude*float64longitude

Longitude.

MacAddress*stringmac
SourceTypeSourceTypesource_type

Source. Over MQTT it comes from the source_type option.

TrackingTypeTrackingTypetracking_type

Tracking type.

SourceType

type SourceType string

SourceType is core's SourceType.

ConstantValue
SourceTypeUnknown"Unknown"
SourceTypeGps"gps"
SourceTypeRouter"router"
SourceTypeBluetooth"bluetooth"
SourceTypeBluetoothLe"bluetooth_le"
func SourceTypeFromString(s string) SourceType

SourceTypeFromString returns the SourceType spelled s, by its value or by the name of core's member, and SourceTypeUnknown for anything else.

func (v *SourceType) UnmarshalJSON(data []byte) error

UnmarshalJSON reads any spelling SourceTypeFromString 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"
StateValueHome"home"
StateValueNotHome"not_home"
func StateValueFromString(s string) StateValue

StateValueFromString returns s as a StateValue.

func (v *StateValue) UnmarshalJSON(data []byte) error

UnmarshalJSON reads any spelling StateValueFromString accepts.

TrackingType

type TrackingType string

TrackingType is core's TrackingType.

ConstantValue
TrackingTypeUnknown"Unknown"
TrackingTypeConnection"connection"
TrackingTypePosition"position"
func TrackingTypeFromString(s string) TrackingType

TrackingTypeFromString returns the TrackingType spelled s, by its value or by the name of core's member, and TrackingTypeUnknown for anything else.

func (v *TrackingType) UnmarshalJSON(data []byte) error

UnmarshalJSON reads any spelling TrackingTypeFromString accepts.

MQTT discovery config

type Config struct

Config is the MQTT discovery config of device_tracker.

See: https://www.home-assistant.io/integrations/device_tracker.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.

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

MessageExpiryInterval*map[string]anymessage_expiry_interval

Retention time interval for published message.

Name*stringname
Origin*ha.DeviceOriginorigin
PayloadAvailable*stringpayload_available

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

Default: "online"

PayloadHome*stringpayload_home

Default: "home"

PayloadNotAvailable*stringpayload_not_available

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

Default: "offline"

PayloadNotHome*stringpayload_not_home

Default: "not_home"

PayloadReset*stringpayload_reset

Default: "None"

Qos*intqos

Default: 0

SourceType*stringsource_type

Allowed: "gps", "router", "bluetooth", "bluetooth_le". Over MQTT, Entity reports it as the "source_type" attribute.

Default: "gps"

StateTopic*stringstate_topic

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

UniqueId*stringunique_id
ValueTemplate*ha.Templatevalue_template

Only Home Assistant renders templates: with it set, Entity skips state_topic when reading state. 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).