Skip to content

Commit

Permalink
The set light command now supports setting the brightness value (#16)
Browse files Browse the repository at this point in the history
Examples: 

```shell
# Turn on a light
openhue set light 15f51223-1e83-4e48-9158-0c20dbd5734e --on

# Turn on multiple lights
openhue set light 83111103-a3eb-40c5-b22a-02deedd21fcb 8f0a7b52-df25-4bc7-b94d-0dd1a88068ff --on

# Turn off a light
openhue set light 15f51223-1e83-4e48-9158-0c20dbd5734e --off

# Set brightness of a single light
openhue set light 15f51223-1e83-4e48-9158-0c20dbd5734e --on --brightness 42.65
```
  • Loading branch information
thibauult authored Nov 25, 2023
1 parent 699f043 commit aea9e79
Show file tree
Hide file tree
Showing 25 changed files with 2,200 additions and 562 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
with:
go-version: '1.21'

- name: Run the tests
run: make test

- name: Install GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ tidy: ## Tidy makes sure go.mod matches the source code in the module
@$(GO) mod tidy
@echo "\n${GREEN}${BOLD}go.mod successfully cleaned 🧽${RESET}"

.PHONY: test
test: ## Tidy makes sure go.mod matches the source code in the module
@$(GO) test ./...
@echo "\n${GREEN}${BOLD}all tests successfully passed ✅ ${RESET}"


.PHONY: clean
clean: ##
@rm -rf ./dist
Expand Down
53 changes: 0 additions & 53 deletions cmd/auth.go

This file was deleted.

44 changes: 0 additions & 44 deletions cmd/discover.go

This file was deleted.

61 changes: 0 additions & 61 deletions cmd/get.go

This file was deleted.

67 changes: 67 additions & 0 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package get

import (
"github.com/spf13/cobra"
"golang.org/x/net/context"
"openhue-cli/openhue"
"openhue-cli/util"
)

type GetFlags struct {
Json bool
}

var GetConfig GetFlags

// NewCmdGet returns an initialized Command instance for 'get' sub command
func NewCmdGet(api *openhue.ClientWithResponses) *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Display one or many resources",
GroupID: "hue",
Long: `
Retrieve information for any kind of resources exposed by your Hue Bridge: lights, rooms, scenes, etc.
`,
Run: func(cmd *cobra.Command, args []string) {
resp, err := api.GetResourcesWithResponse(context.Background())
cobra.CheckErr(err)
resources := *(*resp.JSON200).Data

typeFlag := cmd.Flag("type").Value.String()

if len(typeFlag) > 0 {
// filter resources by type
n := 0
for _, r := range resources {
if *r.Type == openhue.ResourceGetType(typeFlag) {
resources[n] = r
n++
}
}
resources = resources[:n]
}

if GetConfig.Json {
util.PrintJson(resources)
} else {
util.PrintTable(resources, PrintResource, "Resource ID", "Resource Type")
}
},
}

// local flags
cmd.Flags().StringP("type", "t", "", "Filter by resource type (light, scene, room...)")

// persistence flags
cmd.PersistentFlags().BoolVar(&GetConfig.Json, "json", false, "Format output as JSON")

// sub commands
cmd.AddCommand(NewCmdGetLight(api))
cmd.AddCommand(NewCmdGetRoom(api))

return cmd
}

func PrintResource(resource openhue.ResourceGet) string {
return *resource.Id + "\t" + string(*resource.Type)
}
102 changes: 102 additions & 0 deletions cmd/get/get_light.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package get

import (
"fmt"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"openhue-cli/openhue"
"openhue-cli/util"
"os"
)

const (
docLongGetLight = `
Fetches and displays all available lights
`
docExampleGetLight = `
# List all lights as a table
openhue get light
# List all lights as JSON
openhue get light --json
# Get details for a single light
openhue get light aa31ba26-98a7-4830-8ae9-1b7caa8b5700 --json
`
)

type LightOptions struct {
LightId string
}

func NewGetLightOptions() *LightOptions {
return &LightOptions{}
}

// NewCmdGetLight returns initialized Command instance for the 'get light' sub command
func NewCmdGetLight(api *openhue.ClientWithResponses) *cobra.Command {

o := NewGetLightOptions()

cmd := &cobra.Command{
Use: "light [lightId]",
Short: "Get light",
Long: docLongGetLight,
Example: docExampleGetLight,
Args: cobra.MatchAll(cobra.RangeArgs(0, 1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
o.PrepareGetRoomCmd(args)
o.RunGetLightCmd(api)
},
}

return cmd
}

func (o *LightOptions) PrepareGetRoomCmd(args []string) {
if len(args) > 0 {
o.LightId = args[0]
}
}

func (o *LightOptions) RunGetLightCmd(api *openhue.ClientWithResponses) {
var lights *[]openhue.LightGet

if len(o.LightId) > 0 {
resp, err := api.GetLightWithResponse(context.Background(), o.LightId)
cobra.CheckErr(err)

if resp.JSON200 == nil {
fmt.Println("\nNot light found with ID", o.LightId)
os.Exit(0)
}

lights = (*resp.JSON200).Data
} else {
resp, err := api.GetLightsWithResponse(context.Background())
cobra.CheckErr(err)
lights = (*resp.JSON200).Data
}

if !GetConfig.Json {
util.PrintTable(*lights, PrintLight, "ID", "Name", "Type", "Status", "Brightness")
} else {
util.PrintJsonArray(*lights)
}
}

func PrintLight(light openhue.LightGet) string {

status := "[ ]"
brightness := "N/A"

if *light.On.On {
status = "[on]"
}

if light.Dimming != nil {
brightness = fmt.Sprint(*light.Dimming.Brightness) + "%"
}

return *light.Id + "\t" + *light.Metadata.Name + "\t" + string(*light.Metadata.Archetype) + "\t" + status + "\t" + brightness
}
Loading

0 comments on commit aea9e79

Please sign in to comment.