1- package main
1+ package api
22
33import (
44 "encoding/json"
@@ -9,33 +9,8 @@ import (
99 "net/url"
1010)
1111
12- type thermostatInfo struct {
13- Name string `json:"name"`
14- Mode int `json:"mode"`
15- State int `json:"state"`
16- Fan int `json:"fan"`
17- FanState int `json:"fanstate"`
18- TempUnits int `json:"tempunits"`
19- Schedule int `json:"schedule"`
20- SchedulePart int `json:"schedulepart"`
21- Away int `json:"away"`
22- Holiday int `json:"holiday"`
23- Override int `json:"override"`
24- OverrideTime int `json:"overridetime"`
25- ForceUnoccupied int `json:"forceunocc"`
26- SpaceTemp int `json:"spacetemp"`
27- HeatTemp int `json:"heattemp"`
28- CoolTemp int `json:"cooltemp"`
29- CoolTempMin int `json:"cooltempmin"`
30- CoolTempMax int `json:"cooltempmax"`
31- HeatTempMin int `json:"heattempmin"`
32- HeatTempMax int `json:"heattempmax"`
33- SetPointDelta int `json:"setpointdelta"`
34- Humidity int `json:"hum"`
35- AvaliableModes int `json:"avaliablemodes"`
36- }
37-
38- func convertThermostatMode (mode string ) int {
12+ // ConvertThermostatMode converts the "plain text" mode to the numeric value
13+ func ConvertThermostatMode (mode string ) int {
3914 switch mode {
4015 case "off" :
4116 return 0
@@ -50,7 +25,8 @@ func convertThermostatMode(mode string) int {
5025 }
5126}
5227
53- func convertFanMode (mode string ) int {
28+ // ConvertFanMode converts the "plain text" mode to the numeric value
29+ func ConvertFanMode (mode string ) int {
5430 switch mode {
5531 case "auto" :
5632 return 0
@@ -61,19 +37,21 @@ func convertFanMode(mode string) int {
6137 }
6238}
6339
64- func getThermostatInfo (ipaddress string ) thermostatInfo {
40+ // GetThermostatInfo pulls the full info from the thermostat
41+ func GetThermostatInfo (ipaddress string ) ThermostatInfo {
6542 resp , err := http .Get (fmt .Sprintf ("http://%s/query/info" , ipaddress ))
6643 if err != nil {
6744 log .Fatalln (err )
6845 }
6946 defer resp .Body .Close ()
7047 respBytes , _ := ioutil .ReadAll (resp .Body )
71- var thermostatResponse thermostatInfo
48+ var thermostatResponse ThermostatInfo
7249 json .Unmarshal (respBytes , & thermostatResponse )
7350 return thermostatResponse
7451}
7552
76- func setThermostatMode (ipaddress string , mode int , currentInfo thermostatInfo ) bool {
53+ // SetThermostatMode sets the thermostat mode
54+ func SetThermostatMode (ipaddress string , mode int , currentInfo ThermostatInfo ) bool {
7755 data := url.Values {}
7856 data .Set ("heattemp" , fmt .Sprintf ("%d" , currentInfo .HeatTemp ))
7957 data .Set ("cooltemp" , fmt .Sprintf ("%d" , currentInfo .CoolTemp ))
@@ -82,15 +60,17 @@ func setThermostatMode(ipaddress string, mode int, currentInfo thermostatInfo) b
8260 return reqErr == nil
8361}
8462
85- func setCoolTemp (ipaddress string , coolTemp int , currentInfo thermostatInfo ) bool {
63+ // SetCoolTemp sets the "cool to" temp on the thermostat
64+ func SetCoolTemp (ipaddress string , coolTemp int , currentInfo ThermostatInfo ) bool {
8665 data := url.Values {}
8766 data .Set ("heattemp" , fmt .Sprintf ("%d" , currentInfo .HeatTemp ))
8867 data .Set ("cooltemp" , fmt .Sprintf ("%d" , coolTemp ))
8968 _ , reqErr := http .PostForm (fmt .Sprintf ("http://%s/control" , ipaddress ), url .Values (data ))
9069 return reqErr == nil
9170}
9271
93- func setFanMode (ipaddress string , fanMode int , currentInfo thermostatInfo ) bool {
72+ // SetFanMode sets whether the fan is "auto" or "on"
73+ func SetFanMode (ipaddress string , fanMode int , currentInfo ThermostatInfo ) bool {
9474 data := url.Values {}
9575 data .Set ("heattemp" , fmt .Sprintf ("%d" , currentInfo .HeatTemp ))
9676 data .Set ("cooltemp" , fmt .Sprintf ("%d" , currentInfo .CoolTemp ))
0 commit comments