Skip to content

Commit

Permalink
google-device-access: implement new api
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Feb 24, 2023
1 parent a9ab2d0 commit 031e290
Showing 1 changed file with 55 additions and 17 deletions.
72 changes: 55 additions & 17 deletions plugins/google-device-access/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import sdk, { DeviceManifest, DeviceProvider, HttpRequest, HttpRequestHandler, HttpResponse, HumiditySensor, MediaObject, MotionSensor, OauthClient, Refresh, ScryptedDeviceType, ScryptedInterface, Setting, Settings, TemperatureSetting, TemperatureUnit, Thermometer, ThermostatMode, VideoCamera, MediaStreamOptions, BinarySensor, DeviceInformation, RTCAVSignalingSetup, Camera, PictureOptions, ObjectsDetected, ObjectDetector, ObjectDetectionTypes, FFmpegInput, RequestMediaStreamOptions, Readme, RTCSignalingChannel, RTCSessionControl, RTCSignalingSession, ResponseMediaStreamOptions, RTCSignalingOptions, RTCSignalingSendIceCandidate, ScryptedMimeTypes, MediaStreamUrl, TemperatureCommand } from '@scrypted/sdk';
import { ScryptedDeviceBase } from '@scrypted/sdk';
import qs from 'query-string';
import ClientOAuth2 from 'client-oauth2';
import { URL } from 'url';
import axios from 'axios';
import throttle from 'lodash/throttle';
import { connectRTCSignalingClients } from '@scrypted/common/src/rtc-signaling';
import { sleep } from '@scrypted/common/src/sleep';
import fs from 'fs';
import sdk, { BinarySensor, Camera, DeviceInformation, DeviceManifest, DeviceProvider, FFmpegInput, HttpRequest, HttpRequestHandler, HttpResponse, HumiditySensor, MediaObject, MediaStreamUrl, MotionSensor, OauthClient, ObjectDetectionTypes, ObjectDetector, ObjectsDetected, PictureOptions, Readme, Refresh, RequestMediaStreamOptions, ResponseMediaStreamOptions, RTCAVSignalingSetup, RTCSessionControl, RTCSignalingChannel, RTCSignalingSendIceCandidate, RTCSignalingSession, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, ScryptedMimeTypes, Setting, Settings, TemperatureCommand, TemperatureSetting, TemperatureUnit, Thermometer, ThermostatMode, VideoCamera } from '@scrypted/sdk';
import axios from 'axios';
import ClientOAuth2 from 'client-oauth2';
import { randomBytes } from 'crypto';
import fs from 'fs';
import throttle from 'lodash/throttle';
import qs from 'query-string';
import { URL } from 'url';

const { deviceManager, mediaManager, endpointManager, systemManager } = sdk;

Expand Down Expand Up @@ -102,7 +101,7 @@ class NestRTCSessionControl implements RTCSessionControl {
}

async setPlayback(options: { audio: boolean; video: boolean; }): Promise<void> {

}

async getRefreshAt(): Promise<number> {
Expand Down Expand Up @@ -366,10 +365,7 @@ class NestThermostat extends ScryptedDeviceBase implements HumiditySensor, Therm
this.console.log('executeCommandSetCelsius', command);
return this.provider.authPost(`/devices/${this.nativeId}:executeCommand`, command);
}
}, 12000, {
leading: false,
trailing: true,
})
}, 12000)

constructor(provider: GoogleSmartDeviceAccess, device: any) {
super(device.name.split('/').pop());
Expand All @@ -379,8 +375,50 @@ class NestThermostat extends ScryptedDeviceBase implements HumiditySensor, Therm
this.reload();
}

setTemperature(command: TemperatureCommand): Promise<void> {
throw new Error('Method not implemented.');
async setTemperature(command: TemperatureCommand): Promise<void> {
// set this in case round trip is slow.
let { mode, setpoint } = command;
if (mode) {
const nestMode = toNestMode(mode);
this.device.traits['sdm.devices.traits.ThermostatMode'].mode = nestMode;

this.executeCommandSetMode = {
command: 'sdm.devices.commands.ThermostatMode.SetMode',
params: {
mode: nestMode,
},
}
}

if (command.setpoint) {
mode ||= fromNestMode(this.device.traits['sdm.devices.traits.ThermostatMode'].mode);

this.executeCommandSetCelsius = {
command: setpointReverseMap.get(mode),
params: {
},
};

if (typeof command.setpoint === 'number') {
if (mode === ThermostatMode.Heat) {
this.executeCommandSetCelsius.params.heatCelsius = command.setpoint;

}
else if (mode === ThermostatMode.Cool) {
this.executeCommandSetCelsius.params.coolCelsius = command.setpoint;
}
else {
this.executeCommandSetCelsius.params.coolCelsius = command.setpoint;
this.executeCommandSetCelsius.params.heatCelsius = command.setpoint;
}
}
else {
this.executeCommandSetCelsius.params.heatCelsius = command[0];
this.executeCommandSetCelsius.params.coolCelsius = command[1];
}
}
await this.executeThrottle();
await this.refresh(null, true);
}

async setTemperatureUnit(temperatureUnit: TemperatureUnit): Promise<void> {
Expand Down Expand Up @@ -409,7 +447,7 @@ class NestThermostat extends ScryptedDeviceBase implements HumiditySensor, Therm
const heat = device.traits?.['sdm.devices.traits.ThermostatTemperatureSetpoint']?.heatCelsius;
const cool = device.traits?.['sdm.devices.traits.ThermostatTemperatureSetpoint']?.coolCelsius;

let setpoint: number|[number,number];
let setpoint: number | [number, number];
if (this.thermostatMode === ThermostatMode.Heat) {
this.thermostatSetpoint = heat;
this.thermostatSetpointHigh = undefined;
Expand All @@ -426,7 +464,7 @@ class NestThermostat extends ScryptedDeviceBase implements HumiditySensor, Therm
this.thermostatSetpoint = undefined;
this.thermostatSetpointHigh = heat;
this.thermostatSetpointLow = cool;
setpoint = [cool, heat];
setpoint = [heat, cool];
}
else {
this.thermostatSetpoint = undefined;
Expand Down

0 comments on commit 031e290

Please sign in to comment.