generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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; | ||
} | ||
} | ||
} |