httpclient

package
v1.6.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

httpclient implements an API client for the Home Assistant REST API https://developers.home-assistant.io/docs/api/rest/

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallResponse

type CallResponse struct {
	ChangedStates   []*State       `json:"changed_states"`
	ServiceResponse map[string]any `json:"service_response"`
}

CallResponse is the states which changed when calling a service, and the service's response data

type CallServiceRequest

type CallServiceRequest struct {
	Domain  string         `json:"domain" help:"The service domain (e.g. light, switch, climate, media_player)."`
	Service string         `json:"service" help:"The service to call (e.g. turn_on, turn_off, toggle, set_temperature)."`
	Data    map[string]any `json:"data,omitempty" help:"Service data including entity_id and any service-specific fields."`
}

CallServiceRequest is the input for calling a service

type Client

type Client struct {
	*client.Client
}

func New

func New(endpoint, apiKey string, opts ...client.ClientOpt) (*Client, error)

New returns a client for the Home Assistant server at the endpoint, with a long-lived access token. The endpoint is the server's URL, such as "http://homeassistant.local:8123", or its API URL, which ends in "/api".

func (*Client) Call

func (c *Client) Call(ctx context.Context, domain, service string, serviceData map[string]any) ([]*State, error)

Call calls a service of a domain. The service data is sent as the request body, and typically includes "entity_id" and any service-specific fields. Returns the states which changed while the service was being executed.

func (*Client) CallWithResponse

func (c *Client) CallWithResponse(ctx context.Context, domain, service string, serviceData map[string]any) (*CallResponse, error)

CallWithResponse calls a service of a domain, and returns the states which changed and the service's response data, for services which return data, such as weather.get_forecasts

func (*Client) Config

func (c *Client) Config(ctx context.Context) (*Config, error)

Config returns the server's configuration

func (*Client) DeleteState

func (c *Client) DeleteState(ctx context.Context, entityId string) error

DeleteState removes an entity

func (*Client) Domains

func (c *Client) Domains(ctx context.Context) ([]*Domain, error)

Domains returns all domains and their services

func (*Client) Events

func (c *Client) Events(ctx context.Context) ([]Event, error)

Events returns the events, and the number of listeners for each

func (*Client) FireEvent

func (c *Client) FireEvent(ctx context.Context, eventType string, eventData map[string]any) (string, error)

FireEvent fires an event of the type, with optional event data, and returns a confirmation message

func (*Client) Health

func (c *Client) Health(ctx context.Context) (string, error)

Health returns "API running." if the Home Assistant API is reachable

func (*Client) Services

func (c *Client) Services(ctx context.Context, domain string) ([]*Service, error)

Services returns the services which can be called for a domain, in order of name. Returns ErrNotFound if there is no domain with the name.

func (*Client) SetState

func (c *Client) SetState(ctx context.Context, entityId, state string, attributes map[string]any) (*State, error)

SetState creates or updates the state of an entity, with optional attributes, and returns the state. This sets the representation within Home Assistant, and doesn't communicate with the device; use Call to actuate a device.

func (*Client) State

func (c *Client) State(ctx context.Context, entityId string) (*State, error)

State returns the state of an entity

func (*Client) States

func (c *Client) States(ctx context.Context) ([]*State, error)

States returns all the entities and their state

func (*Client) Template

func (c *Client) Template(ctx context.Context, template string) (string, error)

Template renders a Jinja2 template, and returns the result as text See https://www.home-assistant.io/docs/configuration/templating

type Config

type Config struct {
	Components    []string       `json:"components"`
	ConfigDir     string         `json:"config_dir"`
	Elevation     float64        `json:"elevation"`
	Latitude      float64        `json:"latitude"`
	Longitude     float64        `json:"longitude"`
	LocationName  string         `json:"location_name"`
	TimeZone      string         `json:"time_zone"`
	UnitSystem    map[string]any `json:"unit_system"`
	Version       string         `json:"version"`
	ExternalDirs  []string       `json:"whitelist_external_dirs,omitempty"`
	AllowlistDirs []string       `json:"allowlist_external_dirs,omitempty"`
	AllowlistURLs []string       `json:"allowlist_external_urls,omitempty"`
	Currency      string         `json:"currency,omitempty"`
	Country       string         `json:"country,omitempty"`
	Language      string         `json:"language,omitempty"`
	SafeMode      bool           `json:"safe_mode,omitempty"`
	State         string         `json:"state,omitempty"`
	InternalURL   string         `json:"internal_url,omitempty"`
	ExternalURL   string         `json:"external_url,omitempty"`
	RecoveryMode  bool           `json:"recovery_mode,omitempty"`
}

Config is the server's configuration

func (Config) String

func (c Config) String() string

type Domain

type Domain struct {
	Domain   string              `json:"domain"`
	Services map[string]*Service `json:"services,omitempty"`
}

Domain is a domain, such as "light", and its services, by name

func (Domain) String

func (d Domain) String() string

type Event

type Event struct {
	Event     string `json:"event"`
	Listeners uint   `json:"listener_count"`
}

Event is an event type, and the number of its listeners

type Field

type Field struct {
	Required bool                `json:"required,omitempty"`
	Example  any                 `json:"example,omitempty"`
	Selector map[string]Selector `json:"selector,omitempty"`
}

Field is a field of the data for a service

func (Field) String

func (f Field) String() string

type FireEventRequest

type FireEventRequest struct {
	EventType string         `json:"event_type" help:"The event type to fire (e.g. my_custom_event)."`
	EventData map[string]any `json:"event_data,omitempty" help:"Optional data to include with the event."`
}

FireEventRequest is the input for firing an event

type GetServicesRequest

type GetServicesRequest struct {
	Domain string `json:"domain" help:"The domain to list services for (e.g. light, switch, climate)."`
}

GetServicesRequest is the input for listing the services of a domain

type GetStateRequest

type GetStateRequest struct {
	EntityId string `json:"entity_id" help:"The entity ID to query (e.g. light.living_room)."`
}

GetStateRequest is the input for getting the state of an entity

type GetStatesRequest

type GetStatesRequest struct {
	Domain string `` /* 128-byte string literal not displayed */
	Q      string `` /* 133-byte string literal not displayed */
	Offset uint64 `json:"offset,omitempty" help:"How many entities to skip, to get the next page" optional:""`
	Limit  uint64 `json:"limit,omitempty" help:"Most entities to return, which is 50 when it isn't given" min:"1" max:"200" optional:""`
}

GetStatesRequest is the input for listing the entities and their states

type RenderTemplateRequest

type RenderTemplateRequest struct {
	Template string `json:"template" help:"The Jinja2 template string to render (e.g. '{{ states(\"sensor.temperature\") }}')."`
}

RenderTemplateRequest is the input for rendering a template

type Selector

type Selector struct {
	Text              string  `json:"text,omitempty"`
	Mode              string  `json:"mode,omitempty"`
	Min               float32 `json:"min,omitempty"`
	Max               float32 `json:"max,omitempty"`
	UnitOfMeasurement string  `json:"unit_of_measurement,omitempty"`
}

Selector describes the values of a field

type Service

type Service struct {
	Call        string           `json:"call,omitempty"`
	Name        string           `json:"name,omitempty"`
	Description string           `json:"description,omitempty"`
	Fields      map[string]Field `json:"fields,omitempty"`
}

Service is a service which can be called for a domain

func (Service) String

func (s Service) String() string

type SetStateRequest

type SetStateRequest struct {
	EntityId   string         `json:"entity_id" help:"The entity ID to set (e.g. sensor.my_sensor)."`
	State      string         `json:"state" help:"The state value to set."`
	Attributes map[string]any `json:"attributes,omitempty" help:"Optional attributes such as unit_of_measurement, friendly_name, etc."`
}

SetStateRequest is the input for setting the state of an entity

type State

type State struct {
	Entity       string         `json:"entity_id"`
	LastChanged  time.Time      `json:"last_changed"`
	LastReported time.Time      `json:"last_reported"`
	LastUpdated  time.Time      `json:"last_updated"`
	State        string         `json:"state"`
	Attributes   map[string]any `json:"attributes"`
	Context      struct {
		Id       string `json:"id,omitempty"`
		ParentId string `json:"parent_id,omitempty"`
		UserId   string `json:"user_id,omitempty"`
	} `json:"context"`
}

State is the state of an entity

func (State) Class

func (s State) Class() string

Class is how the state should be interpreted, which is the entity's device class, or its domain if it has none

func (State) Domain

func (s State) Domain() string

Domain is the domain of the entity, such as "light" for "light.kitchen", which determines the services which can be called for it

func (State) Name

func (s State) Name() string

Name is the friendly name of the entity, or its ID if it has none

func (State) String

func (s State) String() string

func (State) UnitOfMeasurement

func (s State) UnitOfMeasurement() string

UnitOfMeasurement is the unit of the state, or empty if it has none

func (State) Value

func (s State) Value() string

Value is the state of the entity, or empty if it's unavailable or unknown

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL