go_hasocket

package module
v0.0.0-...-fd70e87 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-hasocket

Wrapper around the Home Assistant WebSocket API. Giving you the ability to easily call services and listen to events.

Getting started

Installation
go get github.com/moonen-home-automation/go-hasocket
Instantiate an app instance
package main

import (
	gohasocket "github.com/moonen-home-automation/go-hasocket"
	"log"
)

func main() {
	app, err := gohasocket.NewApp("ws://myhassinstance:8123/api/websocket", "supersecrettoken")
	if err != nil {
		log.Fatal(err)
	}
	
	// Code here
}
Calling services
Function without return value
package main

import (
	gohasocket "github.com/moonen-home-automation/go-hasocket"
	"github.com/moonen-home-automation/go-hasocket/pkg/services"
	"log"
)

func main() {
	app, err := gohasocket.NewApp("ws://myhassinstance:8123/api/websocket", "supersecrettoken")
	if err != nil {
		log.Fatal(err)
	}

	sr := services.NewServiceRequest()
	sr.Domain = "light"
	sr.Service = "toggle"
	sr.Target = services.ServiceTarget{
		EntityId: "light.desklamp",
	}
	_, err = app.CallService(sr)
	if err != nil {
		log.Fatal(err)
	}
}
Function with return value
package main

import (
	"fmt"
	gohasocket "github.com/moonen-home-automation/go-hasocket"
	"github.com/moonen-home-automation/go-hasocket/pkg/services"
	"log"
	"os"
)

func main() {
	app, err := gohasocket.NewApp("ws://myhassinstance:8123/api/websocket", "supersecrettoken")
	if err != nil {
		log.Fatal(err)
	}

	sr := services.NewServiceRequest()
	sr.Domain = "todo"
	sr.Service = "get_items"
	sr.Target = services.ServiceTarget{
		EntityId: "todo.daily",
	}
	sr.ReturnResponse = true
	res, err := app.CallService(sr)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.Result.Response)
}
Listening for events
package main

import (
	"fmt"
	gohasocket "github.com/moonen-home-automation/go-hasocket"
	"github.com/moonen-home-automation/go-hasocket/pkg/services"
	"log"
	"os"
)

func main() {
	app, err := gohasocket.NewApp("ws://myhassinstance:8123/api/websocket", "supersecrettoken")
	if err != nil {
		log.Fatal(err)
	}

	el, err := app.RegisterListener("test")
	if err != nil {
		log.Fatal(err)
	}

	err = el.Register()
	if err != nil {
		log.Fatal(err)
	}

	elChan := make(chan events.EventData, 10)
	go el.Listen(elChan)
	defer func() {
		err := el.Close()
		if err != nil {
			log.Fatal(err)
		}
	}()

	for {
		msg, ok := <-elChan
		if !ok {
			continue
		}

		fmt.Println(string(msg.RawEventJSON))
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidArgs = errors.New("invalid arguments provided")

Functions

This section is empty.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

func GetApp

func GetApp() (*App, error)

func NewApp

func NewApp(uri, token string) (*App, error)

func (*App) CallService

func (a *App) CallService(sr services.ServiceRequest) (services.ServiceResult, error)

func (*App) RegisterListener

func (a *App) RegisterListener(eventType string) (events.EventListener, error)

Directories

Path Synopsis
websocket
Package websocket /*
Package websocket /*
pkg

Jump to

Keyboard shortcuts

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