Categorygithub.com/moonen-home-automation/go-hasocket
modulepackage
0.0.0-20240625072131-fd70e8730e16
Repository: https://github.com/moonen-home-automation/go-hasocket.git
Documentation: pkg.go.dev

# 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))
	}
}

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author

# Variables

No description provided by the author

# Structs

No description provided by the author