Skip to content

Commit

Permalink
Merge pull request #42 from gonzolino/gonzolino/issue36
Browse files Browse the repository at this point in the history
Add functions to control mobile device settings
  • Loading branch information
mergify[bot] authored Jul 14, 2021
2 parents 265f80a + 7336d47 commit fc58b2f
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions tado.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ type MobileDevice struct {

// MobileDeviceSettings holds the settings of a mobile device
type MobileDeviceSettings struct {
GeoTrackingEnabled bool `json:"geoTrackingEnabled"`
PushNotifications MobileDeviceSettingsPushNotifications `json:"pushNotifications"`
GeoTrackingEnabled bool `json:"geoTrackingEnabled"`
PushNotifications *MobileDeviceSettingsPushNotifications `json:"pushNotifications,omitempty"`
}

// MobileDeviceSettingsPushNotifications holds the push notification settings
Expand Down Expand Up @@ -1068,6 +1068,42 @@ func GetMobileDevices(client *Client, userHome *UserHome) ([]*MobileDevice, erro
return mobileDevices, nil
}

// DeleteMobileDevice deletes the given mobile device
func DeleteMobileDevice(client *Client, userHome *UserHome, mobileDevice *MobileDevice) error {
resp, err := client.Request(http.MethodDelete, apiURL("homes/%d/mobileDevices/%d", userHome.ID, mobileDevice.ID), nil)
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("unexpected tado° API response status: %s", resp.Status)
}
return nil
}

// SetMobileDeviceSettings updates the given mobile device with the given settings
func SetMobileDeviceSettings(client *Client, userHome *UserHome, mobileDevice *MobileDevice, settings MobileDeviceSettings) error {
data, err := json.Marshal(settings)
if err != nil {
return fmt.Errorf("unable to marshal mobile device settings: %w", err)
}
req, err := http.NewRequest(http.MethodPut, apiURL("homes/%d/mobileDevices/%d/settings", userHome.ID, mobileDevice.ID), bytes.NewReader(data))
if err != nil {
return fmt.Errorf("unable to create http request: %w", err)
}
req.Header.Set("Content-Type", "application/json;charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return err
}

if err := isError(resp); err != nil {
return fmt.Errorf("tado° API error: %w", err)
}

return nil
}

// GetUsers lists all users and their mobile devices linked to the given home
func GetUsers(client *Client, userHome *UserHome) ([]*User, error) {
resp, err := client.Request(http.MethodGet, apiURL("homes/%d/users", userHome.ID), nil)
Expand Down

0 comments on commit fc58b2f

Please sign in to comment.