Documentation
¶
Overview ¶
httpclient implements an API client for the Home Assistant REST API https://developers.home-assistant.io/docs/api/rest/
Index ¶
- type CallResponse
- type CallServiceRequest
- type Client
- func (c *Client) Call(ctx context.Context, domain, service string, serviceData map[string]any) ([]*State, error)
- func (c *Client) CallWithResponse(ctx context.Context, domain, service string, serviceData map[string]any) (*CallResponse, error)
- func (c *Client) Config(ctx context.Context) (*Config, error)
- func (c *Client) DeleteState(ctx context.Context, entityId string) error
- func (c *Client) Domains(ctx context.Context) ([]*Domain, error)
- func (c *Client) Events(ctx context.Context) ([]Event, error)
- func (c *Client) FireEvent(ctx context.Context, eventType string, eventData map[string]any) (string, error)
- func (c *Client) Health(ctx context.Context) (string, error)
- func (c *Client) Services(ctx context.Context, domain string) ([]*Service, error)
- func (c *Client) SetState(ctx context.Context, entityId, state string, attributes map[string]any) (*State, error)
- func (c *Client) State(ctx context.Context, entityId string) (*State, error)
- func (c *Client) States(ctx context.Context) ([]*State, error)
- func (c *Client) Template(ctx context.Context, template string) (string, error)
- type Config
- type Domain
- type Event
- type Field
- type FireEventRequest
- type GetServicesRequest
- type GetStateRequest
- type GetStatesRequest
- type RenderTemplateRequest
- type Selector
- type Service
- type SetStateRequest
- type State
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 ¶
func New ¶
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) DeleteState ¶
DeleteState removes an entity
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) Services ¶
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.
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
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
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
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
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 ¶
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 ¶
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) UnitOfMeasurement ¶
UnitOfMeasurement is the unit of the state, or empty if it has none