Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SelectionOptions type and refactor GetThermostat to use them #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions ecobee/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (
"github.com/golang/glog"
)

const thermostatAPIURL = `https://api.ecobee.com/1/thermostat`
const thermostatSummaryURL = `https://api.ecobee.com/1/thermostatSummary`
const (
thermostatAPIURL = `https://api.ecobee.com/1/thermostat`
thermostatSummaryURL = `https://api.ecobee.com/1/thermostatSummary`
)

func (c *Client) UpdateThermostat(utr UpdateThermostatRequest) error {
j, err := json.Marshal(&utr)
Expand Down Expand Up @@ -65,10 +67,7 @@ func (c *Client) UpdateThermostat(utr UpdateThermostatRequest) error {
return fmt.Errorf("API error: %s", s.Status.Message)
}

func (c *Client) GetThermostat(thermostatID string) (*Thermostat, error) {
// TODO: Consider factoring the generation of Selection out into
// something else to make it more convenient to toggle the IncludeX
// flags?
func (c *Client) GetThermostat(thermostatID string, opts ...SelectionOption) (*Thermostat, error) {
s := Selection{
SelectionType: "thermostats",
SelectionMatch: thermostatID,
Expand All @@ -82,6 +81,11 @@ func (c *Client) GetThermostat(thermostatID string) (*Thermostat, error) {
IncludeSensors: true,
IncludeWeather: false,
}

for _, optfn := range opts {
cvvs marked this conversation as resolved.
Show resolved Hide resolved
optfn(s)
}

thermostats, err := c.GetThermostats(s)
if err != nil {
return nil, err
Expand Down Expand Up @@ -139,7 +143,7 @@ func (c *Client) GetThermostatSummary(selection Selection) (map[string]Thermosta

glog.V(1).Infof("GetThermostatSummary response: %#v", r)

var tsm = make(ThermostatSummaryMap, r.ThermostatCount)
tsm := make(ThermostatSummaryMap, r.ThermostatCount)

for i := 0; i < r.ThermostatCount; i++ {
rl := strings.Split(r.RevisionList[i], ":")
Expand Down Expand Up @@ -174,7 +178,6 @@ func (c *Client) GetThermostatSummary(selection Selection) (map[string]Thermosta
}

func (c *Client) get(endpoint string, rawRequest []byte) ([]byte, error) {

glog.V(2).Infof("get(%s?json=%s)", endpoint, rawRequest)
request := url.QueryEscape(string(rawRequest))
resp, err := c.Get(fmt.Sprintf("%s?json=%s", endpoint, request))
Expand Down Expand Up @@ -223,7 +226,6 @@ func buildEquipmentStatus(input string) (EquipmentStatus, error) {
}

func (es *EquipmentStatus) Set(field string, state bool) {

switch field {
case "heatPump":
es.HeatPump = state
Expand Down Expand Up @@ -256,5 +258,4 @@ func (es *EquipmentStatus) Set(field string, state bool) {
case "auxHotWater":
es.AuxHotWater = state
}

}
135 changes: 135 additions & 0 deletions ecobee/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package ecobee

type SelectionOption func(s Selection)

func IncludeAlerts(value bool) SelectionOption {
return func(s Selection) {
s.IncludeAlerts = value
}
}

func IncludeAudio(value bool) SelectionOption {
return func(s Selection) {
s.IncludeAudio = value
}
}

func IncludeDevice(value bool) SelectionOption {
return func(s Selection) {
s.IncludeDevice = value
}
}

func IncludeElectricity(value bool) SelectionOption {
return func(s Selection) {
s.IncludeElectricity = value
}
}

func IncludeEquipmentStatus(value bool) SelectionOption {
return func(s Selection) {
s.IncludeElectricity = value
}
}

func IncludeEvents(value bool) SelectionOption {
return func(s Selection) {
s.IncludeEvents = value
}
}

func IncludeExtendedRuntime(value bool) SelectionOption {
return func(s Selection) {
s.IncludeExtendedRuntime = value
}
}

func IncludeHouseDetails(value bool) SelectionOption {
return func(s Selection) {
s.IncludeHouseDetails = value
}
}

func IncludeLocation(value bool) SelectionOption {
return func(s Selection) {
s.IncludeLocation = value
}
}

func IncludeManagement(value bool) SelectionOption {
return func(s Selection) {
s.IncludeManagement = value
}
}

func IncludeNotificationSettings(value bool) SelectionOption {
return func(s Selection) {
s.IncludeManagement = value
}
}

func IncludeOemCfg(value bool) SelectionOption {
return func(s Selection) {
s.IncludeOemCfg = value
}
}

func IncludePrivacy(value bool) SelectionOption {
return func(s Selection) {
s.IncludePrivacy = value
}
}

func IncludeProgram(value bool) SelectionOption {
return func(s Selection) {
s.IncludeProgram = value
}
}

func IncludeRuntime(value bool) SelectionOption {
return func(s Selection) {
s.IncludeRuntime = value
}
}

func IncludeSecuritySettings(value bool) SelectionOption {
return func(s Selection) {
s.IncludeSecuritySettings = value
}
}

func IncludeSensors(value bool) SelectionOption {
return func(s Selection) {
s.IncludeSensors = value
}
}

func IncludeSettings(value bool) SelectionOption {
return func(s Selection) {
s.IncludeSettings = value
}
}

func IncludeTechnician(value bool) SelectionOption {
return func(s Selection) {
s.IncludeSettings = value
}
}

func IncludeUtility(value bool) SelectionOption {
return func(s Selection) {
s.IncludeUtility = value
}
}

func IncludeVersion(value bool) SelectionOption {
return func(s Selection) {
s.IncludeVersion = value
}
}

func IncludeWeather(value bool) SelectionOption {
return func(s Selection) {
s.IncludeWeather = value
}
}