Skip to content

Commit

Permalink
set up eufy-robovac-js and first service/characteristics
Browse files Browse the repository at this point in the history
  • Loading branch information
gtalusan committed Aug 31, 2024
1 parent 507c0c3 commit 608328e
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 214 deletions.
133 changes: 126 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"ts-node": "^10.9.2",
"typescript": "^5.5.4",
"typescript-eslint": "^8.0.1"
},
"dependencies": {
"@george.talusan/eufy-robovac-js": "^0.0.4"
}
}
72 changes: 72 additions & 0 deletions src/defaultAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';

import type { EufyRobovacHomebridgePlatform } from './platform.js';

interface RobovacEvent {
command: string;
value: number | string | object | null;
};

export class DefaultPlatformAccessory {
private service: Service;

constructor(
private readonly platform: EufyRobovacHomebridgePlatform,
private readonly accessory: PlatformAccessory,
) {
const displayName = this.accessory.context.displayName;

// set accessory information
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');

this.service = this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch);
this.service.setCharacteristic(this.platform.Characteristic.Name, `${displayName}`);
this.service.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 = () => {
try {
batteryLevelService.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.platform.robovac.batteryLevel());
} catch (error: unknown) {
this.platform.log.error(error as string);
}
};

this.platform.robovac.on('tuya.data', updateBatteryLevel);
this.platform.robovac.on('event', (event: RobovacEvent) => {
this.platform.log('battery', event.command, event.value);
if (event.command === 'battery') {
updateBatteryLevel();
}
});
}

async setOn(value: CharacteristicValue) {
try {
const on: boolean = value as boolean;
if (on) {
await this.platform.robovac.clean();
} else {
await this.platform.robovac.goHome();
}
} catch (error: unknown) {
this.platform.log.error(error as string);
}
}

async getOn(): Promise<CharacteristicValue> {
try {
return !this.platform.robovac.docked();
} catch (error: unknown) {
this.platform.log.error(error as string);
return false;
}
}
}
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { API } from 'homebridge';

import { ExampleHomebridgePlatform } from './platform.js';
import { EufyRobovacHomebridgePlatform } from './platform.js';
import { PLATFORM_NAME } from './settings.js';

/**
* This method registers the platform with Homebridge
*/
export default (api: API) => {
api.registerPlatform(PLATFORM_NAME, ExampleHomebridgePlatform);
api.registerPlatform(PLATFORM_NAME, EufyRobovacHomebridgePlatform);
};
Loading

0 comments on commit 608328e

Please sign in to comment.