hass_ws

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: MIT Imports: 18 Imported by: 1

README

HASS-WS

Connect to Home Assistant Websocket and subscribe to events

Work in progress. API is very likely to change. Many thanks to https://github.com/W-Floyd/ha-mqtt-iot for the base idea and start generation code.

The client itself subscribes to all events. client.OnType is a struct of handlers for each type, the data you get within the handler is the whole message and new and old attributes in a specific type for the entity type. The type is generated from Home Assistant configs and only contains attributes defined there, extra attributes are put into a map[string]interface{} called Additional.
client.OnEntity is a map of entities (containing the domain so "climate.living" not "living") of functions to run when that entity state changes. Only the message is available with attributes as a map[string]interface{}.

Entities

For each entity there is a OnXXX function that can be run. The new and old attributes will have entity specific types returned.

Services

Each service has a generator that is passed to client.CallService. Methods exist on the service type to add parameters to the service.

This package contains a default list of services however this is most likely not a complete list of services available in your Home Assistant installation since the service list is dynamic based on integrations installed. Generating your own service definitions is needed to interact properly with all your specific integrations.

There is a HassWSService application that can be used to generate a services package based on your Home Assistant installation.

go install github.com/kjbreil/hass-ws/helpers/HassWSService@latest
go install github.com/campoy/jsonenums@latest
go install golang.org/x/tools/cmd/stringer@latest
HassWSService
go generate ./...
Calling a Service

To call a service you create a service and then add options to the service. I recommend using Home Assistant Developer Tools -> Services page to get a better understanding of what is needed for each call each service. There is no reporting of requirements in the service definitions so be warned, some parameters are required and others are not, it is also conditional at times. For example ClimateSetTemperature{} needs TargetTempHigh(float64) and TargetTempLow(float64) when the mode is Heat/Cool however if the mode is Heat or the mode is Cool then Temperature(float64) is needed and both TargetTempHigh(float64) and TargetTempLow(float64) are ignored.

cfg, _ := hass_ws.ParseConfig("config.yml")
c, _ := hass_ws.NewClient(cfg)
err := c.Connect()
if err != nil {
    panic(err)
}

service := services.NewClimateSetTemperature(services.Targets("climate.kitchen")).
		HvacMode(services.HvacModeheat_cool).
		TargetTempHigh(75).
		TargetTempLow(65)

c.CallService(service)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	OnMessage   func(message model.Message)
	OnUnhandled func(message model.Message)
	InitStates  bool

	OnType     model.OnTypeHandlers
	OnEntity   model.OnEntityHandlers
	OnGetState func(states []model.Result)
	// contains filtered or unexported fields
}

func NewClient

func NewClient(c *Config) (*Client, error)

func NewClientWithLogger added in v0.1.2

func NewClientWithLogger(c *Config, logger logr.Logger) (*Client, error)

func (*Client) AddSubscription

func (c *Client) AddSubscription(eventType model.EventType)

func (*Client) CallService

func (c *Client) CallService(service services.Service)

func (*Client) Close

func (c *Client) Close() error

func (*Client) Connect

func (c *Client) Connect() error

func (*Client) GetDeviceRegistry added in v0.1.2

func (c *Client) GetDeviceRegistry() *model.Message

func (*Client) GetEntityRegistry added in v0.1.2

func (c *Client) GetEntityRegistry() *model.Message

func (*Client) GetHistory added in v0.2.3

func (c *Client) GetHistory(start, end time.Time, entities ...string) (*model.Histories, error)

func (*Client) GetServices

func (c *Client) GetServices() *model.Message

func (*Client) GetStates

func (c *Client) GetStates()

GetStates requests all the current states and runs them through the handlers

func (*Client) Logger

func (c *Client) Logger() *logr.Logger

func (*Client) NextID

func (c *Client) NextID() *int

func (*Client) SetLogger added in v0.1.2

func (c *Client) SetLogger(logger logr.Logger)

func (*Client) WriteMessage

func (c *Client) WriteMessage(messageType websocket.MessageType, data []byte) error

type Config

type Config struct {
	Host  string `json:"host"`
	SSL   bool   `json:"ssl"`
	Port  int    `json:"port"`
	Token string `json:"token"`
}

func ParseConfig added in v0.1.1

func ParseConfig(filename string) (*Config, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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