-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The set light command now supports setting the brightness value (#16)
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
Showing
25 changed files
with
2,200 additions
and
562 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.