hass

package module
Version: v0.0.0-...-59bea27 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2018 License: MIT Imports: 8 Imported by: 0

README

go-hass

Incomplete API for interfacing with Home Assistant in Go.

Example use

package main

import (
	"fmt"

	"github.com/pawal/go-hass"
)

func main() {
	h := hass.NewAccess("http://localhost:8123", "")
	err := h.CheckAPI()
	if err != nil {
		panic(err)
	}
	fmt.Println("API ok")

    // Get a filtered list of devices
	list, err := h.FilterStates("switch", "lock", "light")
	if err != nil {
		panic(err)
	}
    // Print that list of devices
	for d := range list {
		fmt.Printf("%s (%s): %s\n", list[d].EntityID,
			list[d].Attributes.FriendlyName,
			list[d].State)
	}

    // Get the state of a device
	s, err := h.GetState("group.kitchen")
	if err != nil {
		panic(err)
	}
	fmt.Printf("Group kitchen state: %s\n", s.State)

    // Create and interact with a device object
    dev := h.GetDevice(s)
    	fmt.Println("DEV: " + dev.EntityID())
	dev.On()
}

License

MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Access

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

func NewAccess

func NewAccess(host, password string) *Access

NewAccess returns a new *Access to be used to interface with the Home Assistant system.

func (*Access) Bootstrap

func (a *Access) Bootstrap() (Bootstrap, error)

Bootstrap returns the bootstrap information of the system (obsolete)

func (*Access) CallService

func (a *Access) CallService(domain, service string, entityID string) error

CallService calls a service with a domain, service, and entity id.

func (*Access) ChangeState

func (a *Access) ChangeState(id, state string) (s State, err error)

ChangeState changes the state of a device

func (*Access) CheckAPI

func (a *Access) CheckAPI() error

CheckAPI checks whether or not the API is running. It returns an error if it is not running.

func (*Access) FilterStates

func (a *Access) FilterStates(domains ...string) (s States, err error)

FilterStates returns a list of states filtered by the list of domains

func (*Access) FireEvent

func (a *Access) FireEvent(eventType string, eventData interface{}) error

FireEvent fires an event.

func (*Access) GetDevice

func (a *Access) GetDevice(state State) Device

GetDevice returns a Device object from an State object

func (*Access) GetState

func (a *Access) GetState(id string) (s State, err error)

GetState retrieves one stateobject for the entity id

func (*Access) ListStates

func (a *Access) ListStates() (s States, err error)

ListStates gets an array of state objects

func (*Access) ListenEvents

func (a *Access) ListenEvents() (*EventListener, error)

func (*Access) NewLight

func (a *Access) NewLight(id string) (light *Light)

NewLight creates a new Light instance

func (*Access) NewLock

func (a *Access) NewLock(id string) (lock *Lock)

NewLock creates a new Lock instance

func (*Access) NewSwitch

func (a *Access) NewSwitch(id string) (s *Switch)

NewSwitch creates a new Switch instance

type Bootstrap

type Bootstrap struct {
	Config struct {
		Components      []string `json:"components"`
		Latitude        float64  `json:"latitude"`
		Longitude       float64  `json:"longitude"`
		LocationName    string   `json:"location_name"`
		TemperatureUnit string   `json:"temperature_unit"`
		Timezone        string   `json:"time_zone"`
		Version         string   `json:"version"`
	} `json:"config"`
	Events []struct {
		Event         string `json:"event"`
		ListenerCount int    `json:"listener_count"`
	} `json:"events"`
	Services []struct {
		Domain   string `json:"domain"`
		Services map[string]struct {
			Description string      `json:"description"`
			Fields      interface{} `json:"fields"`
		} `json:"services"`
	} `json:"services"`
	States []struct {
		Attributes struct {
			Auto         bool     `json:"auto"`
			EntityID     []string `json:"entity_id"`
			FriendlyName string   `json:"friendly_name"`
			Hidden       bool     `json:"hidden"`
			Order        int      `json:"order"`
		} `json:"attributes"`
		EntityID    string    `json:"entity_id"`
		LastChanged time.Time `json:"last_changed"`
		LastUpdated time.Time `json:"last_updated"`
		State       string    `json:"state"`
	} `json:"states"`
}

Bootstrap is an obsolete hass struct, seems to be removed

type Device

type Device interface {
	On() error
	Off() error
	Toggle() error
	EntityID() string
	Domain() string
}

Device is a generic interface for interacting with devices

type EventListener

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

func (*EventListener) Close

func (e *EventListener) Close() error

Close closes the event listener library

func (*EventListener) NextStateChanged

func (e *EventListener) NextStateChanged() (StateChangedEvent, error)

NextStateChanged waits and returns for the next state_changed event.

type Light

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

Light describes a Light class

func (*Light) Domain

func (l *Light) Domain() string

Domain returns the Home Assistant domain for the device

func (*Light) EntityID

func (l *Light) EntityID() string

EntityID returns the id of the device object

func (*Light) Off

func (l *Light) Off() (err error)

Off turns off a light

func (*Light) On

func (l *Light) On() (err error)

On turns on a light

func (*Light) Toggle

func (l *Light) Toggle() (err error)

Toggle toggles a switch

type Lock

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

Lock describes a Lock class

func (*Lock) Domain

func (l *Lock) Domain() string

Domain returns the Home Assistant domain for the device

func (*Lock) EntityID

func (l *Lock) EntityID() string

EntityID returns the id of the device object

func (*Lock) Off

func (l *Lock) Off() (err error)

Off unlocks a lock

func (*Lock) On

func (l *Lock) On() (err error)

On locks a lock

func (*Lock) Toggle

func (l *Lock) Toggle() (err error)

Toggle is supposed to toggle the lock, but is not implemented

type State

type State struct {
	Attributes struct {
		Auto         bool   `json:"auto"`
		FriendlyName string `json:"friendly_name"`
		Hidden       bool   `json:"hidden"`
		Order        int    `json:"order"`
		AssumedState bool   `json:"assumed_state"`
	} `json:"attributes"`
	EntityID    string    `json:"entity_id"`
	LastChanged time.Time `json:"last_changed"`
	LastUpdated time.Time `json:"last_updated"`
	State       string    `json:"state"`
}

State is the struct for an object state

type StateChange

type StateChange struct {
	EntityID string `json:"entityid"`
	State    string `json:"state"`
}

StateChange is used for changing state on an entity

type StateChangedEvent

type StateChangedEvent struct {
	Origin    string    `json:"origin"`
	EventType string    `json:"event_type"`
	TimeFired time.Time `json:"time_fired"`
	Data      struct {
		OldState struct {
			EntityID    string    `json:"entity_id"`
			State       string    `json:"state"`
			LastChanged time.Time `json:"last_changed"`
			LastUpdated time.Time `json:"last_updated"`
			Attributes  struct {
				EntityID     []string `json:"entity_id"`
				Order        int      `json:"order"`
				Hidden       bool     `json:"hidden"`
				FriendlyName string   `json:"friendly_name"`
				Auto         bool     `json:"auto"`
			} `json:"attributes"`
		} `json:"old_state"`
		EntityID string `json:"entity_id"`
		NewState struct {
			EntityID    string    `json:"entity_id"`
			State       string    `json:"state"`
			LastChanged time.Time `json:"last_changed"`
			LastUpdated time.Time `json:"last_updated"`
			Attributes  struct {
				EntityID     []string `json:"entity_id"`
				Order        int      `json:"order"`
				Hidden       bool     `json:"hidden"`
				FriendlyName string   `json:"friendly_name"`
				Auto         bool     `json:"auto"`
			} `json:"attributes"`
		} `json:"new_state"`
	} `json:"data"`
}

type States

type States []State

States is an array of State objects

type Switch

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

Switch describes a Switch class

func (*Switch) Domain

func (s *Switch) Domain() string

Domain returns the Home Assistant domain for the device

func (*Switch) EntityID

func (s *Switch) EntityID() string

EntityID returns the id of the device object

func (*Switch) Off

func (s *Switch) Off() (err error)

Off turns off a switch

func (*Switch) On

func (s *Switch) On() (err error)

On turns on a switch

func (*Switch) Toggle

func (s *Switch) Toggle() (err error)

Toggle toggles a switch

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL