Skip to content

Commit

Permalink
set volume
Browse files Browse the repository at this point in the history
  • Loading branch information
gtalusan committed Sep 6, 2024
1 parent 7ac93d1 commit 1fe7833
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { API, Characteristic, DynamicPlatformPlugin, Logging, PlatformAcces

import { DefaultPlatformAccessory } from './defaultAccessory.js';
import { CleanRoomsPlatformAccessory } from './cleanRoomsAccessory.js';
import { SpeakerPlatformAccessory } from './speakerAccessory.js';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';

import { createRequire } from 'module';
Expand Down Expand Up @@ -86,6 +87,18 @@ export class EufyRobovacHomebridgePlatform implements DynamicPlatformPlugin {
new DefaultPlatformAccessory(this, accessory);
},
},
{
displayName: () => {
return `${this.config.name} Speaker`;
},
uuid: () => {
return this.api.hap.uuid.generate(`${this.config.name}-${this.config.ip}-speaker`);
},
make: (accessory: PlatformAccessory) => {
new SpeakerPlatformAccessory(this, accessory);
},
},

];

const roomSwitches = this.config.roomSwitches;
Expand Down
121 changes: 121 additions & 0 deletions src/speakerAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';

import type { EufyRobovacHomebridgePlatform } from './platform.js';

interface RobovacEvent {
command: string;
value: boolean | number | string | object | null;
};

export class SpeakerPlatformAccessory {
volume: number = 0;

constructor(
private readonly platform: EufyRobovacHomebridgePlatform,
private readonly accessory: PlatformAccessory,
) {
const displayName = this.accessory.context.displayName;

this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Eufy')
.setCharacteristic(this.platform.Characteristic.Model, 'Robovac')
.setCharacteristic(this.platform.Characteristic.SerialNumber, 'Default-Serial');

const main: Service = this.accessory.getService(`${displayName}`) ||
this.accessory.addService(this.platform.Service.Speaker, `${displayName}`, 'volume');
main.getCharacteristic(this.platform.Characteristic.Active)
.onSet(this.setActive.bind(this))
.onGet(this.getActive.bind(this));
main.getCharacteristic(this.platform.Characteristic.Volume)
.onSet(this.setVolume.bind(this))
.onGet(this.getVolume.bind(this));
main.getCharacteristic(this.platform.Characteristic.Mute)
.onSet(this.setMute.bind(this))
.onGet(this.getMute.bind(this));

const updateVolumeLevel = () => {
if (!this.connected()) {
return;
}
try {
this.volume = this.platform.robovac.volume();
main.updateCharacteristic(this.platform.Characteristic.Volume, this.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 === 'volume') {
updateVolumeLevel();
}
});
}

connected(): boolean {
if (!this.platform.connected) {
this.platform.log.warn('not connected');
}
return this.platform.connected;
}

async setActive() {
}

async getActive(): Promise<CharacteristicValue> {
return true;
}

async setVolume(value: CharacteristicValue) {
if (!this.connected()) {
return;
}
if (this.volume === value as number) {
return;
}
try {
await this.platform.robovac.setVolume(value as number);
} catch (error: unknown) {
this.platform.log.error(error as string);
}
}

async getVolume(): Promise<CharacteristicValue> {
if (!this.connected()) {
return 0;
}
try {
return this.volume;
} catch (error: unknown) {
this.platform.log.error(error as string);
return 0;
}
}

async setMute(value: CharacteristicValue) {
if (!this.connected()) {
return;
}
try {
const on: boolean = value as boolean;
await this.platform.robovac.setVolume(on? 0 : this.volume);
} catch (error: unknown) {
this.platform.log.error(error as string);
}
}

async getMute(): Promise<CharacteristicValue> {
if (!this.connected()) {
return false;
}
try {
return this.volume === 0;
} catch (error: unknown) {
this.platform.log.error(error as string);
return false;
}
}

}

0 comments on commit 1fe7833

Please sign in to comment.