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.
set up eufy-robovac-js and first service/characteristics
- Loading branch information
Showing
6 changed files
with
262 additions
and
214 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
}; |
Oops, something went wrong.