Description
Describe Your Problem:
Hi. I want to use custom characteristics in my plugin ("homebridge-homematicip"). Since I'm quite new to TypeScript I would like to know what is the best way to implement a custom Characteristic on plugin-level. Here is my code:
import {Characteristic, Formats, Perms} from 'homebridge';
export class ElectricPower extends Characteristic {
static readonly UUID: string = 'E863F10D-079E-48FF-8F27-9C2605A29F52';
constructor() {
super('Electric Power', ElectricPower.UUID);
this.setProps({
format: Formats.UINT16,
perms: [Perms.NOTIFY, Perms.PAIRED_READ],
maxValue: 100000,
minValue: 0,
minStep: 1,
});
this.value = this.getDefaultValue();
}
}
Unfortunately, when I try to transpile this, I get the following error:
error TS1362: 'Characteristic' cannot be used as a value because it was exported using 'export type'.
This is true, since homebridge exports those classes as a type only. I could include hap-nodejs as a direct dependency, but this might lead to conflicts as you stated in several places. If I just remove the dependency after transpiling, I get the logs shown below.
So my question: What is the best way of doing this? I have seen several plugins just creating the characteristics at runtime, but can't you just do it like this above somehow? You mentioned something like this above yourself in another issue:
homebridge/homebridge#1453 (comment)
You can also look at the full source code for my plugin over here:
https://github.com/marcsowen/homebridge-homematicip
Thanks a lot for your help!
Logs:
[2/5/2021, 8:21:50 AM] ====================
[2/5/2021, 8:21:50 AM] ERROR LOADING PLUGIN homebridge-homematicip:
[2/5/2021, 8:21:50 AM] Error: Cannot find module 'hap-nodejs'
Require stack:
- /usr/lib/node_modules/homebridge-homematicip/dist/EveCharacteristics.js
- /usr/lib/node_modules/homebridge-homematicip/dist/devices/HmIPSwitchMeasuring.js
- /usr/lib/node_modules/homebridge-homematicip/dist/HmIPPlatform.js
- /usr/lib/node_modules/homebridge-homematicip/dist/index.js
- /usr/lib/node_modules/homebridge/lib/plugin.js
- /usr/lib/node_modules/homebridge/lib/pluginManager.js
- /usr/lib/node_modules/homebridge/lib/api.js
- /usr/lib/node_modules/homebridge/lib/server.js
- /usr/lib/node_modules/homebridge/lib/cli.js
- /usr/lib/node_modules/homebridge/bin/homebridge
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/usr/lib/node_modules/homebridge-homematicip/src/EveCharacteristics.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
[2/5/2021, 8:21:50 AM] ====================
Environment:
- Plugin Version: [email protected]
- Homebridge Version: 1.1.7
- Node.js Version: 15.7.0
- NPM Version: 7.5.2
- Operating System: macOS 11.2