From 44431d423d2a366b8dc9f77023eeac9f22df166d Mon Sep 17 00:00:00 2001 From: George Talusan Date: Fri, 6 Sep 2024 13:06:08 -0400 Subject: [PATCH] set volume --- src/defaultAccessory.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/defaultAccessory.ts b/src/defaultAccessory.ts index 89966c3..eafb4ed 100644 --- a/src/defaultAccessory.ts +++ b/src/defaultAccessory.ts @@ -58,9 +58,38 @@ export class DefaultPlatformAccessory { } }); + const speakerService: Service = this.accessory.getService(`${displayName} Speaker`) || + this.accessory.addService(this.platform.Service.Speaker, `${displayName} Speaker`); + speakerService.getCharacteristic(this.platform.Characteristic.Volume) + .onSet(async (value: CharacteristicValue) => { + try { + if (!this.connected()) { + return; + } + await this.platform.robovac.setVolume(value as Number); + } catch (error: unknown) { + this.platform.log.error(error as string); + } + }); + + const updateVolumeLevel = () => { + if (!this.connected()) { + return; + } + try { + speakerService.updateCharacteristic(this.platform.Characteristic.Volume, this.platform.robovac.volume()); + } catch (error: unknown) { + this.platform.log.error(error as string); + } + }; + + this.platform.robovac.on('tuya.data', updateVolumeLevel); + this.platform.robovac.on('event', (event: RobovacEvent) => { if (event.command === 'battery') { updateBatteryLevel(); + } else if (event.command === 'volume') { + updateVolumeLevel(); } else if (event.command === 'locate') { findMyRobot.updateCharacteristic(this.platform.Characteristic.On, event.value as boolean); }