From 4e4b195136bd7a24324e10c75ad3ccdcd76d6361 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Fri, 1 Nov 2024 00:38:02 +0100 Subject: [PATCH] api: add support for creating device push notification registration --- src/index.ts | 29 ++++++++++++++++++ src/types/index.ts | 10 +++++++ test/index.ts | 30 +++++++++++++++++++ ...response.pushNotificationRegistration.json | 3 ++ 4 files changed, 72 insertions(+) create mode 100644 test/response.pushNotificationRegistration.json diff --git a/src/index.ts b/src/index.ts index 4891d36..5bf5bea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,6 +28,8 @@ import { MobileDevice, MobileDeviceSettings, Power, + PushNotificationRegistration, + PushNotificationRegistrationData, RunningTimeAggregation, RunningTimes, RunningTimesSummaryOnly, @@ -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 { + return this.apiCall( + `/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. * diff --git a/src/types/index.ts b/src/types/index.ts index a94862c..2dceffb 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -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; diff --git a/test/index.ts b/test/index.ts index 39fb62f..abf82bc 100644 --- a/test/index.ts +++ b/test/index.ts @@ -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"; @@ -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); diff --git a/test/response.pushNotificationRegistration.json b/test/response.pushNotificationRegistration.json new file mode 100644 index 0000000..dc59fcc --- /dev/null +++ b/test/response.pushNotificationRegistration.json @@ -0,0 +1,3 @@ +{ + "endpointArnValue": "arn:aws:sns:eu-west-0:000000000000:endpoint/GCM/Android-Production/e00000d0-0f00-0000-a0e0-00ee00e0b000" +}