Last active
January 30, 2024 06:47
-
-
Save DMontgomery40/deddc0ab562f923e7e0da68413d6990a to your computer and use it in GitHub Desktop.
Toggle Scrypted Extensions On/Off
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ExtensionToggler extends ScryptedDeviceBase implements Settings, OnOff { | |
| async getSettings(): Promise<Setting[]> { | |
| return [ | |
| { | |
| key: 'devices', | |
| type: 'device', | |
| title: 'Devices', | |
| description: 'The devices on which the extension will be toggled.', | |
| multiple: true, | |
| value: this.getJSON('devices'), | |
| }, | |
| { | |
| key: 'extension', | |
| type: 'device', | |
| title: 'Extension', | |
| description: 'The extension to toggle.', | |
| deviceFilter: `interfaces.includes('${ScryptedInterface.MixinProvider}')`, | |
| value: this.getJSON('extension'), | |
| }, | |
| ] | |
| } | |
| async putSetting(key: string, value: SettingValue): Promise<void> { | |
| this.storage.setItem(key, JSON.stringify(value)); | |
| this.onDeviceEvent(ScryptedInterface.Settings, undefined); | |
| } | |
| async turnOff(): Promise<void> { | |
| const ids = this.getJSON('devices') as string[]; | |
| const extension = this.getJSON('extension'); | |
| for (const id of ids) { | |
| const device = systemManager.getDeviceById(id); | |
| device.setMixins(device.mixins.filter(m => m !== extension)); | |
| } | |
| this.on = false; | |
| } | |
| async turnOn(): Promise<void> { | |
| const ids = this.getJSON('devices') as string[]; | |
| const extension = this.getJSON('extension') as string; | |
| for (const id of ids) { | |
| const device = systemManager.getDeviceById(id); | |
| device.setMixins([...device.mixins, extension]); | |
| } | |
| this.on = true; | |
| } | |
| getJSON(key: string): SettingValue { | |
| try { | |
| return JSON.parse(this.storage.getItem(key)); | |
| } | |
| catch (e) { | |
| return []; | |
| } | |
| } | |
| } | |
| export default ExtensionToggler; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All credit goes to @koushd, not my code
My use case for this was to create an automation in HomeKit to automatically turn off object detection during severe weather events.
Now my CPU/GPU don't completely throttle when 7 4k cameras are all watching a blizzard at the same time :)