Skip to content

Allow plugins to properly type platform config during registration #3609

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

Merged

Conversation

duddu
Copy link
Contributor

@duddu duddu commented May 4, 2024

♻️ Current situation

Currently if a plugin developer wants to strongly type their own plugin configuration interface (e.g. extending PlatformConfig and thus having config fields type checking throughout all plugin code), they would be somehow blocked once they invoke api.registerPlatform to register the platform, since PlatformPluginConstructor.new has the argument config typed as PlatformConfig. A brief snippet to exemplify the scenario:

import { API, DynamicPlatformPlugin, Logger, PlatformConfig } from 'homebridge';

interface MyConfig extends PlatformConfig {
  myProperty: string;
}

class MyPlatform implements DynamicPlatformPlugin {
  constructor(
    public readonly log: Logger,
    public readonly config: MyConfig,
    public readonly api: API,
  ) {}
  // ...
}

export = (api: API) => {
  api.registerPlatform('MyPlugin', 'MyPlatform', MyPlatform); // <- Here tsc will bomb
};

// ERROR:
// Argument of type 'typeof MyPlatform' is not assignable to parameter of type 'PlatformPluginConstructor'.
//   Types of parameters 'config' and 'config' are incompatible.
//     Property 'myProperty' is missing in type 'PlatformConfig' but required in type 'MyConfig'. ts(2345)

💡 Proposed solution

An easy solution is to update the PlatformPluginConstructor interface by adding a generic that allows to auto-detect the type of config argument of the new method:

// src/api.ts

export interface PlatformPluginConstructor<Config extends PlatformConfig = PlatformConfig> {
  new(logger: Logging, config: Config, api: API): DynamicPlatformPlugin | StaticPlatformPlugin | IndependentPlatformPlugin;
}

// And updating `API` interface `registerPlatform` method typing to reflect this.

With this change the same snippet above will work fine - example from vscode:
Screenshot 2024-05-04 at 08 02 35

➕ Additional Information

I think this is a useful change because it encourages plugin developers to strongly type their config (extending PlatformConfig), and allows to use everywhere a single config interface (i.e. when cascading down config from platform instance).

@github-actions github-actions bot added the latest label May 4, 2024
@coveralls
Copy link

coveralls commented May 4, 2024

Pull Request Test Coverage Report for Build 9670740845

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 28.009%

Totals Coverage Status
Change from base Build 9655066281: 0.0%
Covered Lines: 426
Relevant Lines: 1374

💛 - Coveralls

@bwp91
Copy link
Contributor

bwp91 commented May 20, 2024

Hi @duddu

Would you be able to resolve the conflicts on the docs/ files, perhaps remove the changes to these files from your branch, are they necessary to be regenerated as part of your changes?

@duddu duddu force-pushed the api-register-platform-generic-config branch from 99907df to ae59f9f Compare May 22, 2024 11:44
@duddu
Copy link
Contributor Author

duddu commented May 22, 2024

thanks @bwp91, rebased from homebridge/latest and re-generated docs.
i'm afraid diffs in typedoc dir are needed as my change updates an interface (see https://github.com/homebridge/homebridge/pull/3609/files#diff-769911c416ccf8514d8fd941ae0abe8fb5c606ade0c218e22151a5f5f9f3d700R84)

@duddu duddu force-pushed the api-register-platform-generic-config branch from ae59f9f to b6b24bd Compare June 25, 2024 22:40
@bwp91
Copy link
Contributor

bwp91 commented Jun 25, 2024

@donavanbecker can we merge this into a beta?

@donavanbecker donavanbecker changed the base branch from latest to beta-1.8.4 June 25, 2024 23:01
@github-actions github-actions bot added the beta label Jun 25, 2024
@donavanbecker donavanbecker merged commit eff5074 into homebridge:beta-1.8.4 Jun 25, 2024
10 checks passed
@donavanbecker donavanbecker mentioned this pull request Jul 11, 2024
@bwp91 bwp91 mentioned this pull request Jul 14, 2024
@bwp91 bwp91 mentioned this pull request Jul 14, 2024
donavanbecker added a commit that referenced this pull request Jul 19, 2024
## v1.8.4 (2024-07-19)

### Fixed

- Fix default FirmwareRevision (#3644) (@hjdhjd)

### Changed

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2.0](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2.0)
donavanbecker added a commit that referenced this pull request Jul 19, 2024
- Fix default FirmwareRevision (#3644) (@hjdhjd)

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2.0](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2.0)
donavanbecker added a commit that referenced this pull request Jul 19, 2024
- Fix default FirmwareRevision (#3644) (@hjdhjd)

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2.0](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2.0)
donavanbecker added a commit that referenced this pull request Jul 19, 2024
- Fix default FirmwareRevision (#3644) (@hjdhjd)

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2.0](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2.0)

Revert "v1.8.4"

This reverts commit bfd209f.

v1.8.4

- Fix default FirmwareRevision (#3644) (@hjdhjd)

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2.0](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2.0)
donavanbecker added a commit that referenced this pull request Jul 19, 2024
- Fix default FirmwareRevision (#3644) (@hjdhjd)

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2.0](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2.0)
donavanbecker added a commit that referenced this pull request Jul 19, 2024
- Fix default FirmwareRevision (#3644) (@hjdhjd)

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2.0](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2.0)
donavanbecker added a commit that referenced this pull request Jul 19, 2024
- Fix default FirmwareRevision (#3644) (@hjdhjd)

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2.0](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2.0)
donavanbecker added a commit that referenced this pull request Jul 19, 2024
- Fix default FirmwareRevision (#3644) (@hjdhjd)

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2)
donavanbecker added a commit that referenced this pull request Jul 22, 2024
- Fix default FirmwareRevision (#3644) (@hjdhjd)

- Detect config interface on registerPlatform (#3609) (@duddu)
- Updated dependencies, fix `typedoc` generation
- Added log messaging about upcoming Homebridge v2.0.0 update
  - NOTICE TO USERS AND PLUGIN DEVELOPERS
    - Homebridge 2.0 is on the way and brings some breaking changes to existing plugins.
    - Please visit the following link to learn more about the changes and how to prepare:
      - [Updating-To-Homebridge-v2.0](https://github.com/homebridge/homebridge/wiki/Updating-To-Homebridge-v2.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy