Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
marcsowen opened this issue Feb 5, 2021 · 4 comments
Labels
question Further information is requested

Comments

@marcsowen
Copy link

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
@marcsowen marcsowen added the question Further information is requested label Feb 5, 2021
@MiguelRipoll23
Copy link

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
Copy link

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

@kolobock
Copy link

kolobock commented Sep 24, 2021

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

@bwp91
Copy link
Contributor

bwp91 commented Jul 22, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants