Skip to content

Commit

Permalink
use connected event
Browse files Browse the repository at this point in the history
  • Loading branch information
gtalusan committed Sep 3, 2024
1 parent 4561fec commit 1b0955e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/defaultAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ interface RobovacEvent {
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));

export class DefaultPlatformAccessory {
private service: Service;

constructor(
private readonly platform: EufyRobovacHomebridgePlatform,
private readonly accessory: PlatformAccessory,
Expand All @@ -23,13 +21,13 @@ export class DefaultPlatformAccessory {
.setCharacteristic(this.platform.Characteristic.Model, 'Robovac')
.setCharacteristic(this.platform.Characteristic.SerialNumber, 'Default-Serial');

this.service = this.accessory.getService(`${displayName}`) ||
const main: Service = this.accessory.getService(`${displayName}`) ||
this.accessory.addService(this.platform.Service.Switch, `${displayName}`, 'clean');
this.service.getCharacteristic(this.platform.Characteristic.On)
main.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));

const batteryLevelService = this.accessory.getService(`${displayName} Battery Level`) ||
const batteryLevelService: Service = this.accessory.getService(`${displayName} Battery Level`) ||
this.accessory.addService(this.platform.Service.Battery, `${displayName} Battery Level`);

const updateBatteryLevel = () => {
Expand Down
10 changes: 6 additions & 4 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,23 @@ export class EufyRobovacHomebridgePlatform implements DynamicPlatformPlugin {

try {
this.robovac = new RoboVac({ ip: config.ip, deviceId: config.deviceId, localKey: config.deviceKey });
this.robovac.on('tuya.disconnected', async () => {
this.robovac.on('tuya.connected', () => {
this.connected = true;
this.log.info('Connected');
});
this.robovac.on('tuya.disconnected', () => {
this.log.info('Disconnected. Attempting reconnect...');
this.connected = false;
const id = setInterval(async () => {
try {
this.log.debug('reconnecting...');
await this.robovac.connect();
clearInterval(id);
this.connected = true;
} catch (error: unknown) {
this.log.error(error as string);
}
}, 2000);
});
await this.robovac.initialize();
this.connected = true;
} catch (error: unknown) {
this.log.error(error as string);
}
Expand Down

0 comments on commit 1b0955e

Please sign in to comment.