Skip to content

Commit

Permalink
feat: Add interface methods to Zone object
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzolino committed Mar 18, 2022
1 parent 87787b5 commit 32982d4
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 126 deletions.
47 changes: 10 additions & 37 deletions examples/away/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -35,68 +34,42 @@ func main() {
homeName, zoneName := os.Args[1], os.Args[2]

ctx := context.Background()
tado := gotado.New(clientID, clientSecret)

// Create authenticated tado° client
httpClient := &http.Client{Timeout: 5 * time.Second}
client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient)
client, err := client.WithCredentials(ctx, username, password)
if err != nil {
fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err)
os.Exit(1)
}

user, err := gotado.GetMe(client)
user, err := tado.Me(ctx, username, password)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get user info: %v\n", err)
os.Exit(1)
}

// Find the home to control
var home *gotado.UserHome
for _, h := range user.Homes {
if h.Name == homeName {
home = &h
break
}
}
if home == nil {
fmt.Fprintf(os.Stderr, "Home '%s' not found\n", homeName)
home, err := user.GetHome(ctx, homeName)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to find home '%s': %v\n", homeName, err)
os.Exit(1)
}

// Find zone to control
zones, err := gotado.GetZones(client, home)
zone, err := home.GetZone(ctx, zoneName)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get zones: %v\n", err)
os.Exit(1)
}
var zone *gotado.Zone
for _, z := range zones {
if z.Name == zoneName {
zone = z
break
}
}
if zone == nil {
fmt.Fprintf(os.Stderr, "Zone '%s' not found\n", zoneName)
fmt.Fprintf(os.Stderr, "Failed to find zone '%s': %v\n", zoneName, err)
os.Exit(1)
}

// Show away configuration
awayConfig, err := gotado.GetAwayConfiguration(client, home, zone)
awayConfig, err := zone.GetAwayConfiguration(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get away configuration: %v\n", err)
os.Exit(1)
}
fmt.Println("Away Configuration:")
if awayConfig.AutoAdjust {
fmt.Printf("Comfort Level: %d\n", awayConfig.ComfortLevel)
} else {
fmt.Printf("Temperature: %.2f C°, %.2f F°\n", awayConfig.Setting.Temperature.Celsius, awayConfig.Setting.Temperature.Fahrenheit)
}

// Update comfort level
err = gotado.SetAwayComfortLevel(client, home, zone, 0)
err = zone.SetAwayPreheatComfortLevel(ctx, gotado.ComfortLevelEco)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get set comfort level: %v\n", err)
os.Exit(1)
Expand All @@ -106,7 +79,7 @@ func main() {
time.Sleep(10 * time.Second)

// Restore original away configuration
if err := gotado.SetAwayConfiguration(client, home, zone, awayConfig); err != nil {
if err := zone.SetAwayConfiguration(ctx, awayConfig); err != nil {
fmt.Fprintf(os.Stderr, "Failed to set away configuration: %v\n", err)
os.Exit(1)
}
Expand Down
50 changes: 12 additions & 38 deletions examples/earlystart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -35,55 +34,30 @@ func main() {
homeName, zoneName := os.Args[1], os.Args[2]

ctx := context.Background()
tado := gotado.New(clientID, clientSecret)

// Create authenticated tado° client
httpClient := &http.Client{Timeout: 5 * time.Second}
client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient)
client, err := client.WithCredentials(ctx, username, password)
if err != nil {
fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err)
os.Exit(1)
}

user, err := gotado.GetMe(client)
user, err := tado.Me(ctx, username, password)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get user info: %v\n", err)
os.Exit(1)
}

// Find the home to control
var home *gotado.UserHome
for _, h := range user.Homes {
if h.Name == homeName {
home = &h
break
}
}
if home == nil {
fmt.Fprintf(os.Stderr, "Home '%s' not found\n", homeName)
home, err := user.GetHome(ctx, homeName)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to find home '%s': %v\n", homeName, err)
os.Exit(1)
}

// Find zone to control
zones, err := gotado.GetZones(client, home)
zone, err := home.GetZone(ctx, zoneName)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get zones: %v\n", err)
os.Exit(1)
}
var zone *gotado.Zone
for _, z := range zones {
if z.Name == zoneName {
zone = z
break
}
}
if zone == nil {
fmt.Fprintf(os.Stderr, "Zone '%s' not found\n", zoneName)
fmt.Fprintf(os.Stderr, "Failed to find zone '%s': %v\n", zoneName, err)
os.Exit(1)
}

// Check if early start is currently enabled for zone
earlyStartEnabled, err := gotado.IsEarlyStartEnabled(client, home, zone)
earlyStartEnabled, err := zone.GetEarlyStart(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to check if early start is enabled: %v\n", err)
os.Exit(1)
Expand All @@ -96,9 +70,9 @@ func main() {

// Toggle early start setting
if earlyStartEnabled {
err = gotado.DisableEarlyStart(client, home, zone)
err = zone.SetEarlyStart(ctx, false)
} else {
err = gotado.EnableEarlyStart(client, home, zone)
err = zone.SetEarlyStart(ctx, true)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to toggle early start: %v\n", err)
Expand All @@ -110,9 +84,9 @@ func main() {

// Toggle early start back to original value
if earlyStartEnabled {
err = gotado.EnableEarlyStart(client, home, zone)
err = zone.SetEarlyStart(ctx, true)
} else {
err = gotado.DisableEarlyStart(client, home, zone)
err = zone.SetEarlyStart(ctx, false)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to return to initial early start settings: %v\n", err)
Expand Down
49 changes: 11 additions & 38 deletions examples/overlay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -35,55 +34,30 @@ func main() {
homeName, zoneName := os.Args[1], os.Args[2]

ctx := context.Background()
tado := gotado.New(clientID, clientSecret)

// Create authenticated tado° client
httpClient := &http.Client{Timeout: 5 * time.Second}
client := gotado.NewClient(clientID, clientSecret).WithHTTPClient(httpClient)
client, err := client.WithCredentials(ctx, username, password)
if err != nil {
fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err)
os.Exit(1)
}

user, err := gotado.GetMe(client)
user, err := tado.Me(ctx, username, password)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get user info: %v\n", err)
os.Exit(1)
}

// Find the home to control
var home *gotado.UserHome
for _, h := range user.Homes {
if h.Name == homeName {
home = &h
break
}
}
if home == nil {
fmt.Fprintf(os.Stderr, "Home '%s' not found\n", homeName)
home, err := user.GetHome(ctx, homeName)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to find home '%s': %v\n", homeName, err)
os.Exit(1)
}

// Find zone to control
zones, err := gotado.GetZones(client, home)
zone, err := home.GetZone(ctx, zoneName)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get zones: %v\n", err)
os.Exit(1)
}
var zone *gotado.Zone
for _, z := range zones {
if z.Name == zoneName {
zone = z
break
}
}
if zone == nil {
fmt.Fprintf(os.Stderr, "Zone '%s' not found\n", zoneName)
fmt.Fprintf(os.Stderr, "Failed to find zone '%s': %v\n", zoneName, err)
os.Exit(1)
}

// Set heating off in zone
if err := gotado.SetZoneOverlayHeatingOff(client, home, zone); err != nil {
if err := zone.SetHeatingOff(ctx); err != nil {
fmt.Fprintf(os.Stderr, "Failed to turn off heating: %v\n", err)
os.Exit(1)
}
Expand All @@ -92,16 +66,15 @@ func main() {
time.Sleep(30 * time.Second)

// Set heating on in zone (unit for temperature matches default unit of home)
overlay, err := gotado.SetZoneOverlayHeatingOn(client, home, zone, 25)
if err != nil {
if err := zone.SetHeatingOn(ctx, 25.0); err != nil {
fmt.Fprintf(os.Stderr, "Failed to turn heating to 25 degrees: %v\n", err)
os.Exit(1)
}
fmt.Printf("Turned heating in home '%s', zone '%s' to %f°C\n", home.Name, zone.Name, overlay.Setting.Temperature.Celsius)
fmt.Printf("Turned on heating in home '%s', zone '%s'\n", home.Name, zone.Name)
time.Sleep(30 * time.Second)

// Turn off manual heating control in zone. Return to smart schedule
if err := gotado.DeleteZoneOverlay(client, home, zone); err != nil {
if err := zone.ResumeSchedule(ctx); err != nil {
fmt.Fprintf(os.Stderr, "Failed to turn off manual heating: %v\n", err)
os.Exit(1)
}
Expand Down
1 change: 1 addition & 0 deletions home.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (h *Home) GetZones(ctx context.Context) ([]*Zone, error) {
}
for _, zone := range zones {
zone.client = h.client
zone.home = h
}
return zones, nil
}
Expand Down
45 changes: 32 additions & 13 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const (
// Zone represents a tado° zone
type Zone struct {
client *client
home *Home
ID int32 `json:"id"`
Name string `json:"name"`
Type ZoneType `json:"type"`
Expand Down Expand Up @@ -323,18 +324,36 @@ type WeatherMeasurement struct {
Value string `json:"value"`
}

type TimetableType string

const (
TimetableTypeOneDay TimetableType = "ONE_DAY"
TimetableTypeThreeDay TimetableType = "THREE_DAY"
TimetableTypeSevenDay TimetableType = "SEVEN_DAY"
)

// ScheduleTimetable is the type of a tado° schedule timetable
type ScheduleTimetable struct {
ID int32 `json:"id"`
Type TimetableType `json:"type,omitempty"`
client *client
ID int32 `json:"id"`
Type string `json:"type,omitempty"`
}

// TimetableMonToSun has the same schedule for all days between monday and sunday
func TimetableMonToSun() *ScheduleTimetable {
return &ScheduleTimetable{
ID: 0,
Type: "ONE_DAY",
}
}

// TimetableTMonToFriSatSun has the same schedule for all days between monday
// and friday and different schedules for saturday and sunday
func TimetableMonToFriSatSun() *ScheduleTimetable {
return &ScheduleTimetable{
ID: 1,
Type: "THREE_DAY",
}
}

// TimetableAllDays has a different schedule for each day of the week
func TimetableAllDays() *ScheduleTimetable {
return &ScheduleTimetable{
ID: 2,
Type: "SEVEN_DAY",
}
}

// DayType specifies the type of day for a heating schedule block
Expand Down Expand Up @@ -366,11 +385,11 @@ type ComfortLevel int32

const (
// ComfortLevelEco will not preheat the zone too early before arrival and only reach the target temperature after arrival
ComfortLevelEco = 0
ComfortLevelEco ComfortLevel = 0
// ComfortLevelBalance will find the best trade-off between comfort and savings
ComfortLevelBalance = 50
ComfortLevelBalance ComfortLevel = 50
// ComfortLevelComfort ensures that the desired home temperature is reached shortly before arrival
ComfortLevelComfort = 100
ComfortLevelComfort ComfortLevel = 100
)

// AwayConfiguration holds the settings to use when everybody leaves the house
Expand Down
Loading

0 comments on commit 32982d4

Please sign in to comment.