Skip to content

Commit

Permalink
Merge pull request #84 from abn/pushnotification
Browse files Browse the repository at this point in the history
api: add support for creating device push notification registration
  • Loading branch information
mattdavis90 authored Nov 1, 2024
2 parents 166c3f4 + 4e4b195 commit f348da2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
MobileDevice,
MobileDeviceSettings,
Power,
PushNotificationRegistration,
PushNotificationRegistrationData,
RunningTimeAggregation,
RunningTimes,
RunningTimesSummaryOnly,
Expand Down Expand Up @@ -363,6 +365,33 @@ export class Tado {
);
}

/**
* Creates a push notification registration for a given home and mobile device.
*
* Note: Do not use this unless you know what you are doing, you might want to consider
* registering a dummy device.
*
* @param home_id - The identifier for the home.
* @param mobile_device_id - The identifier for the mobile device.
* @param token - The push notification token for the device.
* @returns A promise that resolves to the push notification registration (AWS SNS Endpoint ARN).
*/
createPushNotificationRegistration(
home_id: number,
mobile_device_id: number,
token: string,
): Promise<PushNotificationRegistration> {
return this.apiCall<PushNotificationRegistration>(
`/api/v2/homes/${home_id}/mobileDevices/${mobile_device_id}/pushNotificationRegistration`,
"put",
{
token: token,
firebaseProject: "tado-app",
provider: "FCM",
} as PushNotificationRegistrationData,
);
}

/**
* Fetches the zones for a given home.
*
Expand Down
10 changes: 10 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ export type MobileDeviceSettingsPushNotification = {
incidentDetection: boolean;
};

export type PushNotificationRegistrationData = {
token: string;
firebaseProject: "tado-app" | string;
provider: "FCM" | string;
};

export type PushNotificationRegistration = {
endpointArnValue: string;
};

export type MobileDeviceSettings = {
geoTrackingEnabled: boolean;
onDemandLogRetrievalEnabled: boolean;
Expand Down
30 changes: 30 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import me_response from "./response.me.json";
import mobileDevice_response from "./response.mobileDevice.json";
import mobileDevice_settings_response from "./response.mobileDevice.settings.json";
import mobileDevices_response from "./response.mobileDevices.json";
import mobileDevice_push_notification_registration_response from "./response.pushNotificationRegistration.json";
import state_response from "./response.state.json";
import timetable_response from "./response.timetable.json";
import timetables_response from "./response.timetables.json";
Expand Down Expand Up @@ -318,6 +319,35 @@ describe("High-level API tests", () => {
.catch(done);
});

it("Should register push notification endpoints", (done) => {
const token = "5ad64f2d-b9a2-47ff-be65-3f3f9327a775";

nock("https://my.tado.com")
.put(
"/api/v2/homes/1907/mobileDevices/644583/pushNotificationRegistration",
(body): boolean => {
expect(body).to.deep.equal({
token: token,
firebaseProject: "tado-app",
provider: "FCM",
});
return true;
},
)
.reply(200, mobileDevice_push_notification_registration_response);

tado
.createPushNotificationRegistration(1907, 644583, token)
.then((response) => {
expect(typeof response).to.equal("object");
expect(response.endpointArnValue).to.equal(
"arn:aws:sns:eu-west-0:000000000000:endpoint/GCM/Android-Production/e00000d0-0f00-0000-a0e0-00ee00e0b000",
);
done();
})
.catch(done);
});

it("Should get zones", (done) => {
nock("https://my.tado.com").get("/api/v2/homes/1907/zones").reply(200, zones_response);

Expand Down
3 changes: 3 additions & 0 deletions test/response.pushNotificationRegistration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"endpointArnValue": "arn:aws:sns:eu-west-0:000000000000:endpoint/GCM/Android-Production/e00000d0-0f00-0000-a0e0-00ee00e0b000"
}

0 comments on commit f348da2

Please sign in to comment.