@george.talusan/homebridge-eufy-robovac
Version:
Homebridge Plugin for Eufy Robovac
85 lines • 3.02 kB
JavaScript
;
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
export class CleanRoomsPlatformAccessory {
platform;
accessory;
on;
constructor(platform, accessory) {
this.platform = platform;
this.accessory = accessory;
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 = this.accessory.getService(`${displayName}`) ||
this.accessory.addService(this.platform.Service.Switch, `${displayName}`);
main.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
const batteryLevelService = this.accessory.getService(`${displayName} Battery Level`) ||
this.accessory.addService(this.platform.Service.Battery, `${displayName} Battery Level`);
const updateBatteryLevel = () => {
if (!this.connected()) {
return;
}
try {
batteryLevelService.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.platform.robovac.batteryLevel());
}
catch (error) {
this.platform.log.error(error);
}
};
this.platform.robovac.on('tuya.data', updateBatteryLevel);
this.platform.robovac.on('event', (event) => {
if (event.command === 'battery') {
updateBatteryLevel();
}
});
this.on = false;
}
connected() {
if (!this.platform.connected) {
this.platform.log.warn('not connected');
}
return this.platform.connected;
}
async setOn(value) {
if (!this.connected()) {
return;
}
try {
const on = value;
if (on) {
const rooms = this.accessory.context.rooms;
await this.platform.robovac.cleanRooms(rooms);
}
else {
await this.platform.robovac.pause();
await sleep(3000);
await this.platform.robovac.goHome(true);
}
this.on = on;
}
catch (error) {
this.platform.log.error(error);
}
}
async getOn() {
if (!this.connected()) {
return false;
}
try {
if (this.platform.robovac.goingHome() || this.platform.robovac.docked()) {
this.on = false;
return false;
}
return this.on;
}
catch (error) {
this.platform.log.error(error);
return false;
}
}
}
//# sourceMappingURL=cleanRoomsAccessory.js.map