Skip to content

Commit

Permalink
add setFanMode
Browse files Browse the repository at this point in the history
  • Loading branch information
williampiv committed Nov 19, 2021
1 parent d862b63 commit 2f432ee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ A cli to control your thermostat!
```
usage: venstar-cli [-h|--help] -i|--ip "<value>" [--set-mode
(off|heat|cool|auto)] [--set-cool-temp <integer>]
[--set-fan-mode (auto|on)]
Access Venstar Thermostat
Expand All @@ -15,4 +16,5 @@ Arguments:
thermostat
--set-mode Set the thermostat mode
--set-cool-temp Set the cool-to temperature
--set-fan-mode Set current fan mode to on or auto
```
20 changes: 20 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ func convertThermostatMode(mode string) int {
}
}

func convertFanMode(mode string) int {
switch mode {
case "auto":
return 0
case "on":
return 1
default:
return 0
}
}

func getThermostatInfo(ipaddress string) thermostatInfo {
resp, err := http.Get(fmt.Sprintf("http://%s/query/info", ipaddress))
if err != nil {
Expand Down Expand Up @@ -78,3 +89,12 @@ func setCoolTemp(ipaddress string, coolTemp int, currentInfo thermostatInfo) boo
_, reqErr := http.PostForm(fmt.Sprintf("http://%s/control", ipaddress), url.Values(data))
return reqErr == nil
}

func setFanMode(ipaddress string, fanMode int, currentInfo thermostatInfo) bool {
data := url.Values{}
data.Set("heattemp", fmt.Sprintf("%d", currentInfo.HeatTemp))
data.Set("cooltemp", fmt.Sprintf("%d", currentInfo.CoolTemp))
data.Set("fan", fmt.Sprintf("%d", fanMode))
_, reqErr := http.PostForm(fmt.Sprintf("http://%s/control", ipaddress), url.Values(data))
return reqErr == nil
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func main() {
ip := parser.String("i", "ip", &argparse.Options{Required: true, Help: "An IP (or hostname) is required to actually access a thermostat"})
setMode := parser.Selector("", "set-mode", []string{"off", "heat", "cool", "auto"}, &argparse.Options{Required: false, Help: "Set the thermostat mode"})
coolTemp := parser.Int("", "set-cool-temp", &argparse.Options{Required: false, Help: "Set the cool-to temperature"})
fanMode := parser.Selector("", "set-fan-mode", []string{"auto", "on"}, &argparse.Options{Required: false, Help: "Set current fan mode to on or auto"})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
Expand All @@ -25,5 +26,8 @@ func main() {
if *coolTemp != 0 {
setCoolTemp(*ip, *coolTemp, thermostatData)
}
if *fanMode != "" {
setFanMode(*ip, convertFanMode(*fanMode), thermostatData)
}
printThermostatInfo(thermostatData)
}

0 comments on commit 2f432ee

Please sign in to comment.