hue

package module
v0.0.0-...-070b444 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2021 License: MIT Imports: 23 Imported by: 1

README

go-hue

*** Work In Progress ***

GitHub code size in bytes GitHub go.mod Go version GitHub closed pull requests GitHub pull requests GitHub issues GitHub contributors

go-hue is a Go client library for accessing the Philips Hue API

Install

go get github.com/firstthumb/go-hue

Authentication

Philips Hue uses local and remote authorization. First you need to create user.

Usage

Import the package into your project.

import "github.com/firstthumb/go-hue"

Use existing user and access Hue services. For example:

// Discover your network and finds the first bridge
host, _ := hue.Discover()
client := hue.NewClient(host, "<YOUR USER TOKEN>", nil)
lights, resp, err := client.Light.GetAll(context.Background())

Or create user. Don't forget to save the clientId

// You must press Philips Hue bridge button before
host, _ := hue.Discover()
client, _ := hue.CreateUser(host, "<CLIENT_NAME>", nil)
client.GetClientID() // Save clientID for next time
lights, resp, err := client.Light.GetAll(context.Background())

Supports remote API

// Create your clientId and clientSecret at https://developers.meethue.com/my-apps/
// set your environment variables HUE_CLIENT_ID, HUE_CLIENT_SECRET and HUE_APP_ID
// use the same callback url defined in your app
auth := hue.NewAuthenticator("http://localhost:8181/callback")
client, err := auth.Authenticate()
if err != nil {
  panic(err)
}
	
username, err := client.CreateRemoteUser()
if err != nil {
  panic(err)
}
	
client.Login(username)
result, _, _ := client.Light.GetAll(context.Background())
lights, _ := json.Marshal(result)
fmt.Println(string(lights))

More Examples

Coverage

Currently the following services are supported:

  • Remote API
    • Remote Login
  • Lights API
    • Get all lights
    • Get new lights
    • Search for new lights
    • Get light attributes and state
    • Set light attributes (rename)
    • Set light state
    • Delete lights
  • Groups API
    • Get all groups
    • Create group
    • Get group attributes
    • Set group attributes
    • Set group state
    • Delete group
  • Schedules API
  • Scenes API

Show your support

Give a ⭐️ if this project helped you!

Documentation

Index

Constants

View Source
const (
	AuthURL  = "https://api.meethue.com/oauth2/auth"
	TokenURL = "https://api.meethue.com/oauth2/token"
	ApiURL   = "https://api.meethue.com/bridge/"
)

Variables

This section is empty.

Functions

func Bool

func Bool(v bool) *bool

func Discover

func Discover() (string, error)

Discover gets hue bridge host address

func Int

func Int(v int) *int

func Int64

func Int64(v int64) *int64

func Slice

func Slice(v []string) *[]string

func String

func String(v string) *string

func UInt8

func UInt8(v uint8) *uint8

Types

type ApiError

type ApiError struct {
	Type        int    `json:"type"`
	Address     string `json:"address"`
	Description string `json:"description"`
}

ErrorResponse that Hue returns

type ApiResponse

type ApiResponse struct {
	Success map[string]interface{} `json:"success,omitempty"`
	Error   *ApiError              `json:"error,omitempty"`
}

ApiResponse that Hue returns

type Authenticator

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

func NewAuthenticator

func NewAuthenticator(redirectURL string) Authenticator

func (*Authenticator) AuthURL

func (a *Authenticator) AuthURL(state string) string

func (*Authenticator) AuthURLWithOpts

func (a *Authenticator) AuthURLWithOpts(state string, opts ...oauth2.AuthCodeOption) string

func (*Authenticator) Authenticate

func (a *Authenticator) Authenticate() (*Client, error)

func (*Authenticator) Exchange

func (a *Authenticator) Exchange(code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)

func (*Authenticator) GetToken

func (a *Authenticator) GetToken() *oauth2.Token

func (*Authenticator) NewClient

func (a *Authenticator) NewClient(token *oauth2.Token) *Client

func (*Authenticator) SetAuthInfo

func (a *Authenticator) SetAuthInfo(clientID, secretKey string)

func (*Authenticator) Token

func (a *Authenticator) Token(state string, r *http.Request) (*oauth2.Token, error)

type Capabilities

type Capabilities struct {
	Certified bool      `json:"certified"`
	Control   Control   `json:"control"`
	Streaming Streaming `json:"streaming"`
}

type Client

type Client struct {
	Lights *LightService
	Groups *GroupService
	// contains filtered or unexported fields
}

func CreateUser

func CreateUser(host, deviceType string, opts *ClientOptions) (*Client, error)

CreateUser creates local user on the bridge and returns authenticated client instance Don't forget to press bridge button otherwise it will fail

func NewClient

func NewClient(host, clientId string, opts *ClientOptions) *Client

func (*Client) AddWhitelistIdentifier

func (c *Client) AddWhitelistIdentifier() (string, error)

func (*Client) CreateRemoteUser

func (c *Client) CreateRemoteUser() (string, error)

func (*Client) EnableLinkButton

func (c *Client) EnableLinkButton() error

func (*Client) GetClientID

func (c *Client) GetClientID() string

GetClientID returns clientID of current client

func (*Client) GetHost

func (c *Client) GetHost() string

GetHost returns ip address of hue bridge

func (*Client) Login

func (c *Client) Login(username string) error

type ClientOptions

type ClientOptions struct {
	HttpClient *http.Client
	LogLevel   logrus.Level
}

type Config

type Config struct {
	Archetype string  `json:"archetype"`
	Function  string  `json:"function"`
	Direction string  `json:"direction"`
	Startup   Startup `json:"startup"`
}

type Control

type Control struct {
	Mindimlevel    int         `json:"mindimlevel"`
	Maxlumen       int         `json:"maxlumen"`
	Colorgamuttype string      `json:"colorgamuttype"`
	Colorgamut     [][]float64 `json:"colorgamut"`
	Ct             Ct          `json:"ct"`
}

type Ct

type Ct struct {
	Min int `json:"min"`
	Max int `json:"max"`
}

type Group

type Group struct {
	ID     int
	Name   string      `json:"name"`   // A unique, editable name given to the group.
	Lights []string    `json:"lights"` // The IDs of the lights that are in the group.
	Type   string      `json:"type"`   // If not provided upon creation “LightGroup” is used. Can be “LightGroup”, “Room” or either “Luminaire” or “LightSource” if a Multisource Luminaire is present in the system.
	Action GroupAction `json:"action"` // The light state of one of the lamps in the group.
}

Group struct that represents Philips Hue Group

0 (Zero) A special group containing all lights in the system, and is not returned by the ‘get all groups’ command. This group is not visible, and cannot be created, modified or deleted using the API.

func (*Group) GetBrightness

func (g *Group) GetBrightness() int

GetBrightness returns which is a scale from 0 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 0 is not off.e.g. “brightness”: 60 will set the light to a specific brightness.

func (*Group) GetLights

func (g *Group) GetLights() []string

GetLights returns the ordered set of light ids from the lights which are in the group. This resource shall contain an array of at least one element with the exception of the “Room” type: The Room type may contain an empty lights array. Each element can appear only once. Order of lights on creation is preserved. A light id must be an existing light resource in /lights. If an invalid lights resource is given, error 7 shall be returned and the group is not created. There shall be no change in the lights. Light id can be null if a group has been automatically create by the bridge and a light source is not yet available

func (*Group) GetName

func (g *Group) GetName() string

GetName returns human readable name of the group. If name is not specified one is generated for you (default name is “Group”)

func (*Group) GetType

func (g *Group) GetType() string

GetType returns type of the Group. If not provided on creation a “LightGroup” is created. Supported types: LightGroup 1.4 Default Luminaire 1.4 multisource luminaire LightSource 1.4 multisource luminaire Room 1.11 Represents a room Entertainment 1.22 Represents an entertainment setup Zone 1.30 Represents a zone

func (*Group) IsOn

func (g *Group) IsOn() bool

IsOn returns On/Off state of the light. On=true, Off=false

type GroupAction

type GroupAction struct {
	On        bool      `json:"on"`     // On/Off state of the light. On=true, Off=false
	Bri       int       `json:"bri"`    // Brightness is a scale from 0 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 0 is not off.e.g. “brightness”: 60 will set the light to a specific brightness.
	Hue       int       `json:"hue"`    // The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue.e.g. “hue”: 50000 will set the light to a specific hue.
	Sat       int       `json:"sat"`    // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white).
	Effect    string    `json:"effect"` // The dynamic effect of the light, currently “none” and “colorloop” are supported. Other values will generate an error of type 7.Setting the effect to colorloop will cycle through all hues using the current brightness and saturation settings.
	XY        []float64 `json:"xy"`     // The x and y coordinates of a color in CIE color spaceThe first entry is the x coordinate and the second entry is the y coordinate. Both x and y must be between 0 and 1. If the specified coordinates are not in the CIE color space, the closest color to the coordinates will be chosen.
	Ct        int       `json:"ct"`     // The Mired Color temperature of the light. 2012 connected lights are capable of 153 (6500K) to 500 (2000K).
	Alert     string    `json:"alert"`
	Colormode string    `json:"colormode"`
}

GroupAction is used to execute actions on all lights in a group.

type GroupService

type GroupService service

GroupService has functions for groups

func (*GroupService) CreateGroup

func (s *GroupService) CreateGroup(ctx context.Context, name string, lights []string) (string, *Response, error)

CreateGroup creates light group and returns id of the created group

func (*GroupService) CreateRoom

func (s *GroupService) CreateRoom(ctx context.Context, name string, lights []string) (string, *Response, error)

CreateGroup creates light room and returns id of the room

func (*GroupService) Delete

func (s *GroupService) Delete(ctx context.Context, id string) (*Response, error)

Delete removes the group

func (*GroupService) Get

func (s *GroupService) Get(ctx context.Context, id string) (*Group, *Response, error)

Get returns the group by id

func (*GroupService) GetAll

func (s *GroupService) GetAll(ctx context.Context) ([]Group, *Response, error)

GetAll returns all groups

func (*GroupService) SetState

func (s *GroupService) SetState(ctx context.Context, id string, payload SetStateParams) ([]ApiResponse, *Response, error)

SetState updates state of the group

func (*GroupService) TurnOff

func (s *GroupService) TurnOff(ctx context.Context, id string) error

func (*GroupService) TurnOffAll

func (s *GroupService) TurnOffAll(ctx context.Context, ids ...string)

func (*GroupService) TurnOn

func (s *GroupService) TurnOn(ctx context.Context, id string) error

func (*GroupService) TurnOnAll

func (s *GroupService) TurnOnAll(ctx context.Context, ids ...string)

func (*GroupService) Update

func (s *GroupService) Update(ctx context.Context, id string, name *string, lights []string, class *string) (bool, *Response, error)

Update updates group by id

type Light

type Light struct {
	ID               int          `json:"-"`
	State            State        `json:"state,omitempty"`
	SWUpdate         SWUpdate     `json:"swupdate,omitempty"`
	Type             string       `json:"type,omitempty"`
	Name             string       `json:"name"`
	ModelId          string       `json:"modelid,omitempty"`
	ManufacturerName string       `json:"manufacturername,omitempty"`
	ProductName      string       `json:"productname,omitempty"`
	Capabilities     Capabilities `json:"capabilities,omitempty"`
	Config           Config       `json:"config,omitempty"`
	UniqueId         string       `json:"uniqueid,omitempty"`
	SWVersion        string       `json:"swversion,omitempty"`
	SWConfigId       string       `json:"swconfigid,omitempty"`
	ProductId        string       `json:"productid,omitempty"`
}

func (*Light) GetAlert

func (l *Light) GetAlert() string

func (*Light) GetBri

func (l *Light) GetBri() uint8

func (*Light) GetCT

func (l *Light) GetCT() uint16

func (*Light) GetColorMode

func (l *Light) GetColorMode() string

func (*Light) GetEffect

func (l *Light) GetEffect() string

func (*Light) GetID

func (l *Light) GetID() int

func (*Light) GetMode

func (l *Light) GetMode() string

func (*Light) GetName

func (l *Light) GetName() string

func (*Light) GetSat

func (l *Light) GetSat() uint8

func (*Light) GetTransitionTime

func (l *Light) GetTransitionTime() uint16

func (*Light) GetType

func (l *Light) GetType() string

func (*Light) GetXY

func (l *Light) GetXY() []float32

func (*Light) IsOn

func (l *Light) IsOn() bool

func (*Light) IsReachable

func (l *Light) IsReachable() bool

type LightService

type LightService service

LightService has functions for groups

func (*LightService) Delete

func (s *LightService) Delete(ctx context.Context, id string) (*Response, error)

Delete a light from the bridge.

func (*LightService) Get

func (s *LightService) Get(ctx context.Context, id string) (*Light, *Response, error)

Get returns light by id

func (*LightService) GetAll

func (s *LightService) GetAll(ctx context.Context) ([]Light, *Response, error)

GetAll returns a list of all lights that have been discovered by the bridge.

func (*LightService) GetNew

func (s *LightService) GetNew(ctx context.Context) (map[string]string, *Response, error)

GetNew returns a list of lights that were discovered the last time a search for new lights was performed.

func (*LightService) Rename

func (s *LightService) Rename(ctx context.Context, id, name string) (*Response, error)

Rename lights

func (*LightService) Search

func (s *LightService) Search(ctx context.Context) (*Response, error)

Search starts searching for new lights The bridge will open the network for 40s.

func (*LightService) SetColor

func (s *LightService) SetColor(ctx context.Context, id string, clr color.Color) error

SetColor changes the color of lamp with color

func (*LightService) SetColorHex

func (s *LightService) SetColorHex(ctx context.Context, id string, hex string) error

SetColorHex changes the color of lamp with hex color code

func (*LightService) SetState

func (s *LightService) SetState(ctx context.Context, id string, payload SetStateParams) ([]ApiResponse, *Response, error)

SetState allows the user to turn the light on and off, modify the hue and effects.

func (*LightService) TurnOff

func (s *LightService) TurnOff(ctx context.Context, id string) error

TurnOff sets on status as false

func (*LightService) TurnOffAll

func (s *LightService) TurnOffAll(ctx context.Context, ids ...string)

TurnOffAll sets on status as false

func (*LightService) TurnOn

func (s *LightService) TurnOn(ctx context.Context, id string) error

TurnOn sets on status as true

func (*LightService) TurnOnAll

func (s *LightService) TurnOnAll(ctx context.Context, ids ...string)

TurnOnAll sets on status as true

type Response

type Response struct {
	*http.Response
}

type SWUpdate

type SWUpdate struct {
	State       string `json:"state"`
	Lastinstall string `json:"lastinstall"`
}

type SetStateParams

type SetStateParams struct {
	On             *bool     `json:"on,omitempty"`
	Bri            *uint8    `json:"bri,omitempty"`
	Hue            *uint16   `json:"hue,omitempty"`
	Sat            *uint8    `json:"sat,omitempty"`
	Effect         *string   `json:"effect,omitempty"`
	XY             []float64 `json:"xy,omitempty"`
	CT             *uint16   `json:"ct,omitempty"`
	Alert          *string   `json:"alert,omitempty"`
	TransitionTime *uint16   `json:"transitiontime,omitempty"`
	BriInc         *uint8    `json:"bri_inc,omitempty"`
	SatInc         *uint8    `json:"sat_inc,omitempty"`
	HueInc         *uint16   `json:"hue_inc,omitempty"`
	CtInc          *uint16   `json:"ct_inc,omitempty"`
	XYInc          []float32 `json:"xy_inc,omitempty"`
	Scene          *string   `json:"scene,omitempty"`
}

type Startup

type Startup struct {
	Mode       string `json:"mode"`
	Configured bool   `json:"configured"`
}

type State

type State struct {
	On             bool      `json:"on"`
	Hue            uint16    `json:"hue,omitempty"`
	Effect         string    `json:"effect,omitempty"`
	Bri            uint8     `json:"bri,omitempty"`
	Sat            uint8     `json:"sat,omitempty"`
	CT             uint16    `json:"ct,omitempty"`
	XY             []float32 `json:"xy,omitempty"`
	Alert          string    `json:"alert,omitempty"`
	TransitionTime uint16    `json:"transitiontime,omitempty"`
	Reachable      bool      `json:"reachable,omitempty"`
	ColorMode      string    `json:"colormode,omitempty"`
	Mode           string    `json:"mode,omitempty"`
}

type Streaming

type Streaming struct {
	Renderer bool `json:"renderer"`
	Proxy    bool `json:"proxy"`
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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