Skip to content

Commit

Permalink
#44 Set color temperature in Mirek for lights and rooms (#48)
Browse files Browse the repository at this point in the history
It is now possible to set the color temperature in Mirek for lights and
rooms.

### Examples: 

Set color temperature (in Mirek) of a single room:
```
openhue set light Studio --on --temperature 250
```
Set color temperature (in Mirek) of a single light:
```
openhue set light Desktop --on -t 490
```
Closes #44
  • Loading branch information
thibauult authored Mar 22, 2024
1 parent ae264a3 commit b3e5ada
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 111 deletions.
27 changes: 19 additions & 8 deletions cmd/set/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
//

type CmdSetLightFlags struct {
On bool
Off bool
Brightness float32
Rgb string
X float32
Y float32
ColorName string
On bool
Off bool
Brightness float32
Rgb string
X float32
Y float32
ColorName string
Temperature int
}

func (flags *CmdSetLightFlags) initCmd(cmd *cobra.Command) {
Expand Down Expand Up @@ -48,8 +49,11 @@ func (flags *CmdSetLightFlags) initCmd(cmd *cobra.Command) {
return color.GetSupportColorList(), cobra.ShellCompDirectiveDefault
})

// temperature
cmd.Flags().IntVarP(&flags.Temperature, "temperature", "t", -1, "Color temperature in Mirek [min=153, max=500]")

// exclusions
cmd.MarkFlagsMutuallyExclusive("color", "rgb", "cie-x")
cmd.MarkFlagsMutuallyExclusive("color", "rgb", "cie-x", "temperature")
}

// toSetLightOptions makes sure provided values for LightOptions are valid
Expand All @@ -72,6 +76,13 @@ func (flags *CmdSetLightFlags) toSetLightOptions() (*openhue.SetLightOptions, er
o.Brightness = flags.Brightness
}

// validate the temperature flag
if flags.Temperature > 500 || (flags.Temperature != -1 && flags.Temperature < 153) {
return nil, fmt.Errorf("--temperature flag must be greater than 153 and lower than 500, current value is %d", flags.Temperature)
} else {
o.Temperature = flags.Temperature
}

// color in RGB
if flags.Rgb != "" {
rgb, err := color.NewRGBFomHex(flags.Rgb)
Expand Down
3 changes: 3 additions & 0 deletions cmd/set/set_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ openhue set light 15f51223-1e83-4e48-9158-0c20dbd5734e --on -x 0.675 -y 0.322
# Set color (by name) of a single light
openhue set light 15f51223-1e83-4e48-9158-0c20dbd5734e --on --color powder_blue
# Set color temperature (in Mirek) of a single light
openhue set light MyLight --on -t 250
`
)

Expand Down
3 changes: 3 additions & 0 deletions cmd/set/set_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ openhue set room 15f51223-1e83-4e48-9158-0c20dbd5734e --on -x 0.675 -y 0.322
# Set color (by name) of a single room
openhue set room 15f51223-1e83-4e48-9158-0c20dbd5734e --on --color powder_blue
# Set color temperature (in Mirek) of a single room
openhue set light Studio --on -t 250
`
)

Expand Down
143 changes: 46 additions & 97 deletions openhue/gen/openhue.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions openhue/home_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,18 @@ type Device struct {
//

type SetLightOptions struct {
Status LightStatus
Brightness float32
Color color.XY
Status LightStatus
Brightness float32
Color color.XY
Temperature int
}

func NewSetLightOptions() *SetLightOptions {
return &SetLightOptions{
Status: LightStatusUndefined,
Brightness: -1,
Color: color.UndefinedColor,
Status: LightStatusUndefined,
Brightness: -1,
Color: color.UndefinedColor,
Temperature: -1,
}
}

Expand Down Expand Up @@ -135,6 +137,12 @@ func (light *Light) Set(o *SetLightOptions) {
}
}

if o.Temperature >= 153 && o.Temperature <= 500 {
request.ColorTemperature = &gen.ColorTemperature{
Mirek: &o.Temperature,
}
}

if o.Color != color.UndefinedColor {
request.Color = &gen.Color{
Xy: &gen.GamutPosition{
Expand Down Expand Up @@ -173,6 +181,12 @@ func (groupedLight *GroupedLight) Set(o *SetLightOptions) {
}
}

if o.Temperature >= 153 && o.Temperature <= 500 {
request.ColorTemperature = &gen.ColorTemperature{
Mirek: &o.Temperature,
}
}

if o.Color != color.UndefinedColor {
request.Color = &gen.Color{
Xy: &gen.GamutPosition{
Expand Down

0 comments on commit b3e5ada

Please sign in to comment.