From 50db2d2c16ff2fb46def55b067568b424e576dc7 Mon Sep 17 00:00:00 2001 From: cvvs Date: Sun, 21 Apr 2024 09:09:24 -0500 Subject: [PATCH 1/4] add selection options --- ecobee/options.go | 135 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 ecobee/options.go diff --git a/ecobee/options.go b/ecobee/options.go new file mode 100644 index 0000000..08dde89 --- /dev/null +++ b/ecobee/options.go @@ -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 + } +} From 29c48f82188f762dddc6f55bef4d4300a69dcfab Mon Sep 17 00:00:00 2001 From: cvvs Date: Sun, 21 Apr 2024 09:11:07 -0500 Subject: [PATCH 2/4] refactor GetThermostat to use opts --- ecobee/functions.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ecobee/functions.go b/ecobee/functions.go index a5d124e..6eb99a8 100644 --- a/ecobee/functions.go +++ b/ecobee/functions.go @@ -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) @@ -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, @@ -82,6 +81,11 @@ func (c *Client) GetThermostat(thermostatID string) (*Thermostat, error) { IncludeSensors: true, IncludeWeather: false, } + + for _, optfn := range opts { + optfn(s) + } + thermostats, err := c.GetThermostats(s) if err != nil { return nil, err @@ -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], ":") @@ -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)) @@ -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 @@ -256,5 +258,4 @@ func (es *EquipmentStatus) Set(field string, state bool) { case "auxHotWater": es.AuxHotWater = state } - } From 24ca82d5b39dd669b28fb7c7489d0383f7aab57f Mon Sep 17 00:00:00 2001 From: cvvs Date: Wed, 26 Jun 2024 17:40:44 -0500 Subject: [PATCH 3/4] Fixes from review * Correct gofmt changes * Add the With prefection to option funcs --- ecobee/functions.go | 16 +++++++++------- ecobee/options.go | 44 ++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/ecobee/functions.go b/ecobee/functions.go index 6eb99a8..4a6af75 100644 --- a/ecobee/functions.go +++ b/ecobee/functions.go @@ -26,10 +26,9 @@ import ( "github.com/golang/glog" ) -const ( - thermostatAPIURL = `https://api.ecobee.com/1/thermostat` - thermostatSummaryURL = `https://api.ecobee.com/1/thermostatSummary` -) +const thermostatAPIURL = `https://api.ecobee.com/1/thermostat` +const thermostatSummaryURL = `https://api.ecobee.com/1/thermostatSummary` + func (c *Client) UpdateThermostat(utr UpdateThermostatRequest) error { j, err := json.Marshal(&utr) @@ -82,8 +81,8 @@ func (c *Client) GetThermostat(thermostatID string, opts ...SelectionOption) (*T IncludeWeather: false, } - for _, optfn := range opts { - optfn(s) + for _, o := range opts { + o(s) } thermostats, err := c.GetThermostats(s) @@ -143,7 +142,7 @@ func (c *Client) GetThermostatSummary(selection Selection) (map[string]Thermosta glog.V(1).Infof("GetThermostatSummary response: %#v", r) - tsm := make(ThermostatSummaryMap, r.ThermostatCount) + var tsm = make(ThermostatSummaryMap, r.ThermostatCount) for i := 0; i < r.ThermostatCount; i++ { rl := strings.Split(r.RevisionList[i], ":") @@ -178,6 +177,7 @@ 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)) @@ -226,6 +226,7 @@ func buildEquipmentStatus(input string) (EquipmentStatus, error) { } func (es *EquipmentStatus) Set(field string, state bool) { + switch field { case "heatPump": es.HeatPump = state @@ -258,4 +259,5 @@ func (es *EquipmentStatus) Set(field string, state bool) { case "auxHotWater": es.AuxHotWater = state } + } diff --git a/ecobee/options.go b/ecobee/options.go index 08dde89..d783b16 100644 --- a/ecobee/options.go +++ b/ecobee/options.go @@ -2,133 +2,133 @@ package ecobee type SelectionOption func(s Selection) -func IncludeAlerts(value bool) SelectionOption { +func WithIncludeAlerts(value bool) SelectionOption { return func(s Selection) { s.IncludeAlerts = value } } -func IncludeAudio(value bool) SelectionOption { +func WithIncludeAudio(value bool) SelectionOption { return func(s Selection) { s.IncludeAudio = value } } -func IncludeDevice(value bool) SelectionOption { +func WithIncludeDevice(value bool) SelectionOption { return func(s Selection) { s.IncludeDevice = value } } -func IncludeElectricity(value bool) SelectionOption { +func WithIncludeElectricity(value bool) SelectionOption { return func(s Selection) { s.IncludeElectricity = value } } -func IncludeEquipmentStatus(value bool) SelectionOption { +func WithIncludeEquipmentStatus(value bool) SelectionOption { return func(s Selection) { s.IncludeElectricity = value } } -func IncludeEvents(value bool) SelectionOption { +func WithIncludeEvents(value bool) SelectionOption { return func(s Selection) { s.IncludeEvents = value } } -func IncludeExtendedRuntime(value bool) SelectionOption { +func WithIncludeExtendedRuntime(value bool) SelectionOption { return func(s Selection) { s.IncludeExtendedRuntime = value } } -func IncludeHouseDetails(value bool) SelectionOption { +func WithIncludeHouseDetails(value bool) SelectionOption { return func(s Selection) { s.IncludeHouseDetails = value } } -func IncludeLocation(value bool) SelectionOption { +func WithIncludeLocation(value bool) SelectionOption { return func(s Selection) { s.IncludeLocation = value } } -func IncludeManagement(value bool) SelectionOption { +func WithIncludeManagement(value bool) SelectionOption { return func(s Selection) { s.IncludeManagement = value } } -func IncludeNotificationSettings(value bool) SelectionOption { +func WithIncludeNotificationSettings(value bool) SelectionOption { return func(s Selection) { s.IncludeManagement = value } } -func IncludeOemCfg(value bool) SelectionOption { +func WithIncludeOemCfg(value bool) SelectionOption { return func(s Selection) { s.IncludeOemCfg = value } } -func IncludePrivacy(value bool) SelectionOption { +func WithIncludePrivacy(value bool) SelectionOption { return func(s Selection) { s.IncludePrivacy = value } } -func IncludeProgram(value bool) SelectionOption { +func WithIncludeProgram(value bool) SelectionOption { return func(s Selection) { s.IncludeProgram = value } } -func IncludeRuntime(value bool) SelectionOption { +func WithIncludeRuntime(value bool) SelectionOption { return func(s Selection) { s.IncludeRuntime = value } } -func IncludeSecuritySettings(value bool) SelectionOption { +func WithIncludeSecuritySettings(value bool) SelectionOption { return func(s Selection) { s.IncludeSecuritySettings = value } } -func IncludeSensors(value bool) SelectionOption { +func WithIncludeSensors(value bool) SelectionOption { return func(s Selection) { s.IncludeSensors = value } } -func IncludeSettings(value bool) SelectionOption { +func WithIncludeSettings(value bool) SelectionOption { return func(s Selection) { s.IncludeSettings = value } } -func IncludeTechnician(value bool) SelectionOption { +func WithIncludeTechnician(value bool) SelectionOption { return func(s Selection) { s.IncludeSettings = value } } -func IncludeUtility(value bool) SelectionOption { +func WithIncludeUtility(value bool) SelectionOption { return func(s Selection) { s.IncludeUtility = value } } -func IncludeVersion(value bool) SelectionOption { +func WithIncludeVersion(value bool) SelectionOption { return func(s Selection) { s.IncludeVersion = value } } -func IncludeWeather(value bool) SelectionOption { +func WithIncludeWeather(value bool) SelectionOption { return func(s Selection) { s.IncludeWeather = value } From 08fe82ef3828ac670df58e5e68d0607dbe133d84 Mon Sep 17 00:00:00 2001 From: cvvs Date: Wed, 26 Jun 2024 17:48:14 -0500 Subject: [PATCH 4/4] a few more format fixes --- ecobee/functions.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ecobee/functions.go b/ecobee/functions.go index 4a6af75..68f17e2 100644 --- a/ecobee/functions.go +++ b/ecobee/functions.go @@ -26,10 +26,9 @@ import ( "github.com/golang/glog" ) -const thermostatAPIURL = `https://api.ecobee.com/1/thermostat` +const thermostatAPIURL = `https://api.ecobee.com/1/thermostat` const thermostatSummaryURL = `https://api.ecobee.com/1/thermostatSummary` - func (c *Client) UpdateThermostat(utr UpdateThermostatRequest) error { j, err := json.Marshal(&utr) if err != nil { @@ -226,7 +225,7 @@ func buildEquipmentStatus(input string) (EquipmentStatus, error) { } func (es *EquipmentStatus) Set(field string, state bool) { - + switch field { case "heatPump": es.HeatPump = state