UNPKG

@george.talusan/homebridge-eufy-robovac

Version:
114 lines 4.07 kB
; const sleep = (ms) => new Promise(r => setTimeout(r, ms)); export class DefaultPlatformAccessory { platform; accessory; 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}`, 'clean'); 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); const findMyRobot = this.accessory.getService(`Find ${displayName}`) || this.accessory.addService(this.platform.Service.Switch, `Find ${displayName}`, 'find_my_robot'); findMyRobot.getCharacteristic(this.platform.Characteristic.On) .onSet(async (value) => { try { if (!this.connected()) { return; } const on = value; await this.platform.robovac.locate(on); } catch (error) { this.platform.log.error(error); } }); this.platform.robovac.on('event', (event) => { if (event.command === 'battery') { updateBatteryLevel(); } else if (event.command === 'locate') { findMyRobot.updateCharacteristic(this.platform.Characteristic.On, event.value); } }); } connected() { if (!this.platform.connected) { this.platform.log.warn('not connected'); } return this.platform.connected; } supportsHome() { if (!this.connected()) { this.platform.log.warn('not connected'); return false; } let homeSupported = true; try { this.platform.robovac.goingHome(); } catch (error) { homeSupported = false; } return homeSupported; } async setOn(value) { if (!this.connected()) { return; } try { const on = value; if (on) { await this.platform.robovac.clean(); } else { await this.platform.robovac.pause(); if (this.supportsHome()) { await sleep(3000); await this.platform.robovac.goHome(true); } } } catch (error) { this.platform.log.error(error); } } async getOn() { if (!this.connected()) { return false; } try { if (this.supportsHome() && this.platform.robovac.goingHome()) { return false; } return !this.platform.robovac.docked(); } catch (error) { this.platform.log.error(error); return false; } } } //# sourceMappingURL=defaultAccessory.js.map