From ab57dd5a08429af7f8b397c9b842aaf9a4ba9090 Mon Sep 17 00:00:00 2001 From: Mike McKay Date: Sun, 19 Nov 2023 19:26:54 +1100 Subject: [PATCH 1/3] Added energy site history --- energysite.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/energysite.go b/energysite.go index 4bac1df..7d89554 100644 --- a/energysite.go +++ b/energysite.go @@ -4,8 +4,10 @@ import ( "encoding/json" "errors" "fmt" + "net/url" "strconv" "strings" + "time" ) // this represents site_info endpoint @@ -33,14 +35,46 @@ type EnergySiteStatus struct { c *Client } +type EnergySiteHistory struct { + SerialNumber string `json:"serial_number"` + Period string `json:"period"` + TimeSeries []EnergySiteHistoryTimeSeries `json:"time_series"` + + c *Client +} + +type EnergySiteHistoryTimeSeries struct { + Timestamp time.Time `json:"timestamp"` + SolarEnergyExported float64 `json:"solar_energy_exported"` + GeneratorEnergyExported int `json:"generator_energy_exported"` + GridEnergyImported float64 `json:"grid_energy_imported"` + GridServicesEnergyImported int `json:"grid_services_energy_imported"` + GridServicesEnergyExported int `json:"grid_services_energy_exported"` + GridEnergyExportedFromSolar float64 `json:"grid_energy_exported_from_solar"` + GridEnergyExportedFromGenerator int `json:"grid_energy_exported_from_generator"` + GridEnergyExportedFromBattery float64 `json:"grid_energy_exported_from_battery"` + BatteryEnergyExported int `json:"battery_energy_exported"` + BatteryEnergyImportedFromGrid float64 `json:"battery_energy_imported_from_grid"` + BatteryEnergyImportedFromSolar float64 `json:"battery_energy_imported_from_solar"` + BatteryEnergyImportedFromGenerator int `json:"battery_energy_imported_from_generator"` + ConsumerEnergyImportedFromGrid float64 `json:"consumer_energy_imported_from_grid"` + ConsumerEnergyImportedFromSolar float64 `json:"consumer_energy_imported_from_solar"` + ConsumerEnergyImportedFromBattery float64 `json:"consumer_energy_imported_from_battery"` + ConsumerEnergyImportedFromGenerator int `json:"consumer_energy_imported_from_generator"` +} + type SiteInfoResponse struct { Response *EnergySite `json:"response"` } -type SiteStatutsResponse struct { +type SiteStatusResponse struct { Response *EnergySiteStatus `json:"response"` } +type SiteHistoryResponse struct { + Response *EnergySiteHistory `json:"response"` +} + // SiteCommandResponse is the response from the Tesla API after POSTing a command. type SiteCommandResponse struct { Response struct { @@ -61,7 +95,7 @@ func (c *Client) EnergySite(productID int64) (*EnergySite, error) { } func (s *EnergySite) EnergySiteStatus() (*EnergySiteStatus, error) { - siteStatusResponse := &SiteStatutsResponse{} + siteStatusResponse := &SiteStatusResponse{} if err := s.c.getJSON(s.statusPath(), siteStatusResponse); err != nil { return nil, err } @@ -69,6 +103,24 @@ func (s *EnergySite) EnergySiteStatus() (*EnergySiteStatus, error) { return siteStatusResponse.Response, nil } +type HistoryPeriod string + +const ( + HistoryPeriodDay HistoryPeriod = "day" + HistoryPeriodWeek HistoryPeriod = "week" + HistoryPeriodMonth HistoryPeriod = "month" + HistoryPeriodYear HistoryPeriod = "year" +) + +func (s *EnergySite) EnergySiteHistory(period HistoryPeriod) (*EnergySiteHistory, error) { + historyResponse := &SiteHistoryResponse{} + if err := s.c.getJSON(s.historyPath(period), historyResponse); err != nil { + return nil, err + } + historyResponse.Response.c = s.c + return historyResponse.Response, nil +} + func (s *EnergySite) basePath() string { return strings.Join([]string{s.c.baseURL, "energy_sites", strconv.FormatInt(s.productId, 10)}, "/") } @@ -77,6 +129,14 @@ func (s *EnergySite) statusPath() string { return strings.Join([]string{s.basePath(), "site_status"}, "/") } +func (s *EnergySite) historyPath(period HistoryPeriod) string { + v := url.Values{} + v.Set("kind", "energy") + v.Set("period", string(period)) + + return strings.Join([]string{s.basePath(), "history"}, "/") + fmt.Sprintf("?%s", v.Encode()) +} + func (s *EnergySite) SetBatteryReserve(percent uint64) error { url := s.basePath() + "/backup" payload := fmt.Sprintf(`{"backup_reserve_percent":%d}`, percent) From 8932b42103ea3dbb64200cd51c38174d4abd0fee Mon Sep 17 00:00:00 2001 From: Mike McKay Date: Sun, 19 Nov 2023 19:28:23 +1100 Subject: [PATCH 2/3] Fix go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 2301cf4..3ec8213 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/bogosj/tesla +module github.com/machship-mm/tesla go 1.18 From 66de594aebb02ea135ba236a341dccf9eb489e53 Mon Sep 17 00:00:00 2001 From: Mike McKay Date: Sun, 19 Nov 2023 19:43:42 +1100 Subject: [PATCH 3/3] fix floats --- energysite.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/energysite.go b/energysite.go index 7d89554..9027a9d 100644 --- a/energysite.go +++ b/energysite.go @@ -46,21 +46,21 @@ type EnergySiteHistory struct { type EnergySiteHistoryTimeSeries struct { Timestamp time.Time `json:"timestamp"` SolarEnergyExported float64 `json:"solar_energy_exported"` - GeneratorEnergyExported int `json:"generator_energy_exported"` + GeneratorEnergyExported float64 `json:"generator_energy_exported"` GridEnergyImported float64 `json:"grid_energy_imported"` - GridServicesEnergyImported int `json:"grid_services_energy_imported"` - GridServicesEnergyExported int `json:"grid_services_energy_exported"` + GridServicesEnergyImported float64 `json:"grid_services_energy_imported"` + GridServicesEnergyExported float64 `json:"grid_services_energy_exported"` GridEnergyExportedFromSolar float64 `json:"grid_energy_exported_from_solar"` - GridEnergyExportedFromGenerator int `json:"grid_energy_exported_from_generator"` + GridEnergyExportedFromGenerator float64 `json:"grid_energy_exported_from_generator"` GridEnergyExportedFromBattery float64 `json:"grid_energy_exported_from_battery"` - BatteryEnergyExported int `json:"battery_energy_exported"` + BatteryEnergyExported float64 `json:"battery_energy_exported"` BatteryEnergyImportedFromGrid float64 `json:"battery_energy_imported_from_grid"` BatteryEnergyImportedFromSolar float64 `json:"battery_energy_imported_from_solar"` - BatteryEnergyImportedFromGenerator int `json:"battery_energy_imported_from_generator"` + BatteryEnergyImportedFromGenerator float64 `json:"battery_energy_imported_from_generator"` ConsumerEnergyImportedFromGrid float64 `json:"consumer_energy_imported_from_grid"` ConsumerEnergyImportedFromSolar float64 `json:"consumer_energy_imported_from_solar"` ConsumerEnergyImportedFromBattery float64 `json:"consumer_energy_imported_from_battery"` - ConsumerEnergyImportedFromGenerator int `json:"consumer_energy_imported_from_generator"` + ConsumerEnergyImportedFromGenerator float64 `json:"consumer_energy_imported_from_generator"` } type SiteInfoResponse struct {