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 2d8f827
Show file tree
Hide file tree
Showing 2 changed files with 90 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
77 changes: 77 additions & 0 deletions src/speakerAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 {
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.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));

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

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

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

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

0 comments on commit 2d8f827

Please sign in to comment.