homebridge-plugin-template icon indicating copy to clipboard operation
homebridge-plugin-template copied to clipboard

What's the best way to implement custom characteristics ("class ElectricPower extends Characteristic")

Open marcsowen opened this issue 4 years ago • 3 comments

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:

https://github.com/homebridge/homebridge/issues/1453#issuecomment-562991647

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

marcsowen avatar Feb 05 '21 07:02 marcsowen

I had the same issue when I was rewriting my plugin to TypeScript and I couldn’t solve the problem.

I saw some time ago that there’s an upcoming effort to provide an API for custom characteristics although there’s a lot of work right now for Homebridge developers and it has been proponed.

Custom characteristics solve the “switches hell” pretty well.

MiguelRipoll23 avatar Feb 13 '21 20:02 MiguelRipoll23

I'd suggest to create this exact issue into the HAP-NodeJS repository.

MiguelRipoll23 avatar Feb 14 '21 00:02 MiguelRipoll23

It can be helpful, here is a workaround https://github.com/kolobock/homebridge-overda-uranus/blob/main/src/overda/customCharacteristic.ts#L8-L10 used in another plugin.

This uses something different approach of exporting with require https://github.com/kolobock/homebridge-overda-uranus/blob/main/src/platform.ts#L7 and then extending existing Characteristic with a custom one https://github.com/kolobock/homebridge-overda-uranus/blob/main/src/platform.ts#L37

kolobock avatar Sep 24 '21 20:09 kolobock

Have added a link to this issue in the github readme. https://github.com/homebridge/homebridge-plugin-template#useful-links

I think at some point it would be nice to have proper docs for this. For the moment I will close this issue.

bwp91 avatar Jul 22 '23 00:07 bwp91