add initial hue group setup

This commit is contained in:
Wlad Meixner 2019-02-13 19:43:52 +01:00
parent 6ceda705bd
commit 9822b0e072
6 changed files with 131 additions and 5 deletions

View File

@ -1 +0,0 @@
{"Name":"191280A3-4A87-4669-96C6-064794876004","PublicKey":"oSLgMjcuMkZynqswmP9PSaNOkQvWgWvNKgOPWjLlVsw=","PrivateKey":null}

View File

@ -1 +0,0 @@
{"Name":"94:a8:80:9b:72:3d","PublicKey":"57Z/hKa1lyMTMPPwxWbYf9nGH/tVmXGBnPfMcswvh5U=","PrivateKey":"OWViYzNiZTk1YjI4ZDkzYTA5YzlkNjZiZjQ0ZmMyOWHntn+EprWXIxMw8/DFZth/2cYf+1WZcYGc98xyzC+HlQ=="}

View File

@ -1 +0,0 @@
AŽèŠ<²ù¥‚ çôè„

View File

@ -1 +0,0 @@
94:a8:80:9b:72:3d

View File

@ -1 +0,0 @@
1

131
groups.go
View File

@ -1,8 +1,21 @@
package hue
import "fmt"
const groupsEndpoint = "/groups"
// -------------------------------------------------------------
// ~ Types
// -------------------------------------------------------------
// GroupType hue group type
type GroupType int
// RoomClasses in hue
type RoomClasses int
const (
// All is a group containing all devices cannot be created
All GroupType = iota
Luminaire
Lightsource
@ -10,3 +23,121 @@ const (
Room
Entertainment
)
const (
LivingRoom RoomClasses = iota
Kitchen
Dining
Bedroom
KidsBedroom
Bathroom
Nursery
Recreation
Office
Gym
Hallway
Toilet
FrontDoor
Garage
Terrace
Garden
Driveway
Carport
Other
)
// Group hue type
type Group struct {
Name string `json:"name"`
LightIDs []string `json:"Lights"`
Type string `json:"type"`
Action LightState `json:"action"`
Class string `json:"class,omitempty"`
}
// GroupCreateResponse is returned after a create group request
type GroupCreateResponse struct {
Success struct {
ID string `json:"id"`
} `json:"success"`
}
// -------------------------------------------------------------
// ~ String conversions
// -------------------------------------------------------------
func (g GroupType) String() string {
return [...]string{"0", "Luminaire", "Lightsource", "LightGroup", "Room", "Entertainment"}[g]
}
func (r RoomClasses) String() string {
return [...]string{
"Living room",
"Kitchen",
"Dining",
"Bedroom",
"Kids bedroom",
"Bathroom",
"Nursery",
"Recreation",
"Office",
"Gym",
"Hallway",
"Toilet",
"Front door",
"Garage",
"Terrace",
"Garden",
"Driveway",
"Carport",
"Other",
}[r]
}
// -------------------------------------------------------------
// ~ Private methods
// -------------------------------------------------------------
// CreateGroup creates a new hue group. For rooms please use the CreateRoom call since it also needs a class
func (b *Bridge) CreateGroup(name string, groupType GroupType, lights []string) (string, error) {
// perform some checks
if groupType != Lightsource && groupType != LightGroup && groupType != Entertainment {
return "", fmt.Errorf("only Lightsource, LightGroup or Entertainment type groups can be created, to create a room group please use CreateRoom (As of now other groups cannot be created manually)")
}
groupConfig := &Group{
LightIDs: lights,
Name: name,
Type: groupType.String(),
}
return b.createGroup(groupConfig)
}
// CreateRoom creates a new hue room.
func (b *Bridge) CreateRoom(name string, class RoomClasses, lights []string) (string, error) {
groupConfig := &Group{
LightIDs: lights,
Name: name,
Type: Room.String(),
Class: class.String(),
}
return b.createGroup(groupConfig)
}
// -------------------------------------------------------------
// ~ Private methods
// -------------------------------------------------------------
func (b *Bridge) createGroup(group *Group) (string, error) {
res, errCreate := b.postToBridge(groupsEndpoint, group)
if errCreate != nil {
return "", errCreate
}
newGroups, ok := res.([]*GroupCreateResponse)
if ok && len(newGroups) == 1 {
return newGroups[0].Success.ID, nil
} else {
return "", fmt.Errorf("could not create group, bridge did not return new group id")
}
}