Skip to content

Commit

Permalink
feat: change brightness with + and - (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
xx4h authored Oct 14, 2024
1 parent 83a1b84 commit 7669993
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
13 changes: 8 additions & 5 deletions cmd/brightness.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
)

var (
brightnessRange = append([]string{"min", "mid", "max"}, util.MakeRangeString(1, 99)...)
brightnessRange = append([]string{"+", "-", "min", "mid", "max"}, util.MakeRangeString(1, 99)...)
)

func newBrightnessCmd(h *pkg.Hctl) *cobra.Command {
cmd := &cobra.Command{
Use: "brightness [min|max|1-99]",
Use: "brightness [+|-|min|max|1-99]",
Short: "Change brightness",
Aliases: []string{"b", "br", "bright"},
Args: cobra.MatchAll(cobra.ExactArgs(2)),
Expand All @@ -44,10 +44,13 @@ func newBrightnessCmd(h *pkg.Hctl) *cobra.Command {
}
return nil, cobra.ShellCompDirectiveDefault
},
PersistentPreRunE: func(_ *cobra.Command, args []string) error {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := validateBrightness(args[1]); err != nil {
return err
}
if err := cmd.Root().PersistentPreRunE(cmd, args); err != nil {
return err
}
return nil
},
Run: func(_ *cobra.Command, args []string) {
Expand All @@ -66,8 +69,8 @@ func newBrightnessCmd(h *pkg.Hctl) *cobra.Command {
}

func validateBrightness(brightness string) error {
if !slices.Contains(brightnessRange, brightness) && brightness != "min" && brightness != "max" {
return fmt.Errorf("brightness needs to be 1-99, or min/max")
if !slices.Contains(brightnessRange, brightness) {
return fmt.Errorf("brightness needs to be 1-99, or +/-/min/max")
}
return nil
}
10 changes: 8 additions & 2 deletions cmd/on.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newOnCmd(h *pkg.Hctl) *cobra.Command {
var brightness string

cmd := &cobra.Command{
Use: "on [-b|--brightness min|max|1-99]",
Use: "on [-b|--brightness +|-|min|max|1-99]",
Short: "Switch or turn on a light or switch",
Args: cobra.MatchAll(cobra.ExactArgs(1)),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand All @@ -35,10 +35,16 @@ func newOnCmd(h *pkg.Hctl) *cobra.Command {
}
return compListStates(toComplete, args, []string{"turn_on"}, nil, "off", h)
},
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if brightness == "" {
return nil
}
if err := validateBrightness(brightness); err != nil {
return err
}
if err := cmd.Root().PersistentPreRunE(cmd, args); err != nil {
return err
}
return nil
},
Run: func(_ *cobra.Command, args []string) {
Expand Down
16 changes: 16 additions & 0 deletions pkg/rest/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package rest

import (
"encoding/json"
"fmt"
"slices"
"strings"

Expand Down Expand Up @@ -54,6 +55,21 @@ func (h *Hass) GetStates() ([]HassState, error) {
return states, nil
}

func (h *Hass) GetState(domain, name string) (HassState, error) {
states, err := h.GetStates()
if err != nil {
return HassState{}, err
}

for i := range states {
d, n := splitDomainAndName(states[i].EntityID)
if domain == d && name == n {
return states[i], nil
}
}
return HassState{}, fmt.Errorf("no such state %s.%s", domain, name)
}

func (h *Hass) GetFilteredStates(domains []string) ([]HassState, error) {
states, err := h.GetStates()
if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions pkg/rest/turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package rest

import (
"fmt"
"strconv"
)

func (h *Hass) turn(state, domain, device, brightness string) error {
Expand Down Expand Up @@ -54,9 +55,51 @@ func (h *Hass) TurnOn(args ...string) (string, string, string, error) {
return obj, "on", sub, h.turn("on", sub, obj, "")
}

func (h *Hass) brightStep(domain, device, updown string) (string, error) {
state, err := h.GetState(domain, device)
if err != nil {
return "", err
}

curany, ok := state.Attributes["brightness"]
if !ok {
return "", fmt.Errorf("state `%s.%s` has no attribute `brightness`", domain, device)
}

i, err := strconv.Atoi(fmt.Sprintf("%.0f", curany))
if err != nil {
return "", err
}

diff := i % 10
switch updown {
case "+":
b := i + (10 - diff)
if b == 100 {
b = 99
}
return fmt.Sprintf("%d", b), nil
case "-":
b := i - diff
if diff == 0 {
b = i - 10
}
if b == 0 {
b = 1
}
return fmt.Sprintf("%d", b), nil
default:
return "", fmt.Errorf("no such brightStep: %s", updown)
}
}

func (h *Hass) TurnLightOnBrightness(device, brightness string) (string, string, string, error) {
domain, device, err := h.entityArgHandler([]string{device}, "turn_on")
switch brightness {
case "-":
brightness, err = h.brightStep(domain, device, "-")
case "+":
brightness, err = h.brightStep(domain, device, "+")
case "min":
brightness = "1"
case "mid":
Expand Down

0 comments on commit 7669993

Please sign in to comment.