Skip to content

Commit

Permalink
dummy-switch: select which interfaces to implement
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Jan 8, 2025
1 parent 0405e13 commit cea5c95
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 86 deletions.
2 changes: 1 addition & 1 deletion plugins/dummy-switch/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

{
"scrypted.debugHost": "127.0.0.1",
"scrypted.debugHost": "scrypted-nvr",
}
130 changes: 69 additions & 61 deletions plugins/dummy-switch/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/dummy-switch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
"@scrypted/common": "file:../../common",
"@scrypted/sdk": "file:../../sdk"
},
"version": "0.0.24"
"version": "0.0.25"
}
92 changes: 70 additions & 22 deletions plugins/dummy-switch/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,60 @@ import { BinarySensor, DeviceCreator, DeviceCreatorSettings, DeviceProvider, Loc
import sdk from '@scrypted/sdk';
import { ReplaceMotionSensor, ReplaceMotionSensorNativeId } from './replace-motion-sensor';
import { ReplaceBinarySensor, ReplaceBinarySensorNativeId } from './replace-binary-sensor';
import { StorageSettings } from '@scrypted/sdk/storage-settings';

const { log, deviceManager } = sdk;

class DummyDevice extends ScryptedDeviceBase implements OnOff, Lock, StartStop, OccupancySensor, MotionSensor, BinarySensor, Settings {
timeout: NodeJS.Timeout;
storageSettings = new StorageSettings(this, {
reset: {
title: 'Reset Sensor',
description: 'Reset the motion sensor and binary sensor after the given seconds. Enter 0 to never reset.',
defaultValue: 10,
type: 'number',
placeholder: '10',
onPut: () => {
clearTimeout(this.timeout);
}
},
actionTypes: {
title: 'Action Types',
description: 'Select the action types to expose.',
defaultValue: [
ScryptedInterface.OnOff,
ScryptedInterface.StartStop,
ScryptedInterface.Lock,
],
multiple: true,
choices: [
ScryptedInterface.OnOff,
ScryptedInterface.StartStop,
ScryptedInterface.Lock,
],
onPut: () => {
this.reportInterfaces();
},
},
sensorTypes: {
title: 'Sensor Types',
description: 'Select the sensor types to expose.',
defaultValue: [
ScryptedInterface.MotionSensor,
ScryptedInterface.BinarySensor,
ScryptedInterface.OccupancySensor,
],
multiple: true,
choices: [
ScryptedInterface.MotionSensor,
ScryptedInterface.BinarySensor,
ScryptedInterface.OccupancySensor,
],
onPut: () => {
this.reportInterfaces();
},
}
});

constructor(nativeId: string) {
super(nativeId);
Expand All @@ -19,6 +68,22 @@ class DummyDevice extends ScryptedDeviceBase implements OnOff, Lock, StartStop,
this.occupied = false;
}

async reportInterfaces() {
const interfaces: ScryptedInterface[] = this.storageSettings.values.sensorTypes || [];
if (!interfaces.length)
interfaces.push(ScryptedInterface.MotionSensor, ScryptedInterface.BinarySensor, ScryptedInterface.OccupancySensor);
const actionTyoes = this.storageSettings.values.actionTypes || [];
if (!actionTyoes.length)
actionTyoes.push(ScryptedInterface.OnOff, ScryptedInterface.StartStop, ScryptedInterface.Lock);

await sdk.deviceManager.onDeviceDiscovered({
nativeId: this.nativeId,
interfaces: [...interfaces, ...actionTyoes, ScryptedInterface.Settings],
type: ScryptedDeviceType.Switch,
name: this.providedName,
});
}

lock(): Promise<void> {
return this.turnOff();
}
Expand All @@ -31,20 +96,12 @@ class DummyDevice extends ScryptedDeviceBase implements OnOff, Lock, StartStop,
stop(): Promise<void> {
return this.turnOff();
}

async getSettings(): Promise<Setting[]> {
return [
{
key: 'reset',
title: 'Reset Sensor',
description: 'Reset the motion sensor and binary sensor after the given seconds. Enter 0 to never reset.',
value: this.storage.getItem('reset') || '10',
placeholder: '10',
}
]
return this.storageSettings.getSettings();
}
async putSetting(key: string, value: SettingValue): Promise<void> {
this.storage.setItem(key, value.toString());
clearTimeout(this.timeout);
return this.storageSettings.putSetting(key, value);
}

// note that turnOff locks the lock
Expand Down Expand Up @@ -131,12 +188,6 @@ class DummyDeviceProvider extends ScryptedDeviceBase implements DeviceProvider,
const nativeId = 'shell:' + Math.random().toString();
const name = settings.name?.toString();

await this.onDiscovered(nativeId, name);

return nativeId;
}

async onDiscovered(nativeId: string, name: string) {
await deviceManager.onDeviceDiscovered({
nativeId,
name,
Expand All @@ -151,6 +202,8 @@ class DummyDeviceProvider extends ScryptedDeviceBase implements DeviceProvider,
],
type: ScryptedDeviceType.Switch,
});

return nativeId;
}

async getDevice(nativeId: string) {
Expand All @@ -163,11 +216,6 @@ class DummyDeviceProvider extends ScryptedDeviceBase implements DeviceProvider,
if (!ret) {
ret = new DummyDevice(nativeId);

// remove legacy scriptable interface
if (ret.interfaces.includes(ScryptedInterface.Scriptable)) {
setTimeout(() => this.onDiscovered(ret.nativeId, ret.providedName), 2000);
}

if (ret)
this.devices.set(nativeId, ret);
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/dummy-switch/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "Node16",
"target": "ES2021",
"resolveJsonModule": true,
"moduleResolution": "Node16",
Expand Down

0 comments on commit cea5c95

Please sign in to comment.