Skip to content

Commit 4fce65b

Browse files
committed
First version
1 parent ae59bc8 commit 4fce65b

File tree

8 files changed

+113
-139
lines changed

8 files changed

+113
-139
lines changed

.eslintrc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99
"ecmaVersion": 2018,
1010
"sourceType": "module"
1111
},
12-
"ignorePatterns": [
13-
"dist"
14-
],
12+
"ignorePatterns": ["dist"],
1513
"rules": {
16-
"quotes": ["warn", "single"],
1714
"indent": ["warn", 2, { "SwitchCase": 1 }],
1815
"semi": ["off"],
19-
"comma-dangle": ["warn", "always-multiline"],
2016
"dot-notation": "off",
2117
"eqeqeq": "warn",
2218
"curly": ["warn", "all"],
@@ -28,7 +24,11 @@
2824
"comma-spacing": ["error"],
2925
"no-multi-spaces": ["warn", { "ignoreEOLComments": true }],
3026
"no-trailing-spaces": ["warn"],
31-
"lines-between-class-members": ["warn", "always", {"exceptAfterSingleLine": true}],
27+
"lines-between-class-members": [
28+
"warn",
29+
"always",
30+
{ "exceptAfterSingleLine": true }
31+
],
3232
"@typescript-eslint/explicit-function-return-type": "off",
3333
"@typescript-eslint/no-non-null-assertion": "off",
3434
"@typescript-eslint/explicit-module-boundary-types": "off",

.vscode/settings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"files.eol": "\n",
33
"editor.codeActionsOnSave": {
4-
"source.fixAll.eslint": true
4+
"source.fixAll.eslint": "explicit"
55
},
6-
"editor.rulers": [ 140 ],
6+
"editor.rulers": [140],
77
"eslint.enable": true
8-
}
8+
}

config.schema.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
{
2-
"pluginAlias": "ExampleHomebridgePlugin",
2+
"pluginAlias": "HomebridgeVirtualButton",
33
"pluginType": "platform",
44
"singular": true,
55
"schema": {
66
"type": "object",
7+
"additionalProperties": true,
78
"properties": {
8-
"name": {
9-
"title": "Name",
10-
"type": "string",
11-
"required": true,
12-
"default": "Example Dynamic Platform"
9+
"switches": {
10+
"type": "array",
11+
"additionalItems": true,
12+
"items": {
13+
"type": "object",
14+
"additionalProperties": true,
15+
"properties": {
16+
"name": {
17+
"title": "Disply name for homekit",
18+
"type": "string",
19+
"required": true
20+
}
21+
}
22+
}
1323
}
1424
}
1525
}
16-
}
26+
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
{
2-
"private": true,
3-
"displayName": "Plugin Name",
4-
"name": "homebridge-plugin-name",
2+
"displayName": "Homebridge Virtual Button",
3+
"name": "homebridge-virtual-button",
54
"version": "1.0.0",
65
"description": "A short description about what your plugin does.",
76
"license": "Apache-2.0",
87
"repository": {
98
"type": "git",
10-
"url": "https://github.com/USERNAME/GITHUB_PROJECT_NAME.git"
9+
"url": "https://github.com/rocket-monkey/homebridge-virtual-button.git"
1110
},
1211
"bugs": {
13-
"url": "https://github.com/USERNAME/GITHUB_PROJECT_NAME/issues"
12+
"url": "https://github.com/rocket-monkey/homebridge-virtual-button/issues"
1413
},
1514
"engines": {
1615
"node": "^18.17.0 || ^20.9.0",

src/platform.ts

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic } from 'homebridge';
2-
3-
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
4-
import { ExamplePlatformAccessory } from './platformAccessory';
1+
import {
2+
API,
3+
DynamicPlatformPlugin,
4+
Logger,
5+
PlatformAccessory,
6+
PlatformConfig,
7+
Service,
8+
Characteristic,
9+
} from "homebridge";
10+
11+
import { PLATFORM_NAME, PLUGIN_NAME } from "./settings";
12+
import { PlatformSwitchAccessory } from "./platformAccessory";
513

614
/**
715
* HomebridgePlatform
@@ -10,24 +18,25 @@ import { ExamplePlatformAccessory } from './platformAccessory';
1018
*/
1119
export class ExampleHomebridgePlatform implements DynamicPlatformPlugin {
1220
public readonly Service: typeof Service = this.api.hap.Service;
13-
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
21+
public readonly Characteristic: typeof Characteristic =
22+
this.api.hap.Characteristic;
1423

1524
// this is used to track restored cached accessories
1625
public readonly accessories: PlatformAccessory[] = [];
1726

1827
constructor(
1928
public readonly log: Logger,
2029
public readonly config: PlatformConfig,
21-
public readonly api: API,
30+
public readonly api: API
2231
) {
23-
this.log.debug('Finished initializing platform:', this.config.name);
32+
this.log.debug("Finished initializing platform:", this.config.name);
2433

2534
// When this event is fired it means Homebridge has restored all cached accessories from disk.
2635
// Dynamic Platform plugins should only register new accessories after this event was fired,
2736
// in order to ensure they weren't added to homebridge already. This event can also be used
2837
// to start discovery of new accessories.
29-
this.api.on('didFinishLaunching', () => {
30-
log.debug('Executed didFinishLaunching callback');
38+
this.api.on("didFinishLaunching", () => {
39+
log.debug("Executed didFinishLaunching callback");
3140
// run the method to discover / register your devices as accessories
3241
this.discoverDevices();
3342
});
@@ -38,7 +47,7 @@ export class ExampleHomebridgePlatform implements DynamicPlatformPlugin {
3847
* It should be used to setup event handlers for characteristics and update respective values.
3948
*/
4049
configureAccessory(accessory: PlatformAccessory) {
41-
this.log.info('Loading accessory from cache:', accessory.displayName);
50+
this.log.info("Loading accessory from cache:", accessory.displayName);
4251

4352
// add the restored accessory to the accessories cache so we can track if it has already been registered
4453
this.accessories.push(accessory);
@@ -50,66 +59,67 @@ export class ExampleHomebridgePlatform implements DynamicPlatformPlugin {
5059
* must not be registered again to prevent "duplicate UUID" errors.
5160
*/
5261
discoverDevices() {
62+
if (!this.config.switches) {
63+
return;
64+
}
65+
const switches = this.config.switches;
5366

54-
// EXAMPLE ONLY
55-
// A real plugin you would discover accessories from the local network, cloud services
56-
// or a user-defined array in the platform config.
57-
const exampleDevices = [
58-
{
59-
exampleUniqueId: 'ABCD',
60-
exampleDisplayName: 'Bedroom',
61-
},
62-
{
63-
exampleUniqueId: 'EFGH',
64-
exampleDisplayName: 'Kitchen',
65-
},
66-
];
67-
68-
// loop over the discovered devices and register each one if it has not already been registered
69-
for (const device of exampleDevices) {
70-
67+
// loop over the discovered switches and register each one if it has not already been registered
68+
for (const button of switches) {
7169
// generate a unique id for the accessory this should be generated from
7270
// something globally unique, but constant, for example, the device serial
7371
// number or MAC address
74-
const uuid = this.api.hap.uuid.generate(device.exampleUniqueId);
72+
const uuid = this.api.hap.uuid.generate(button.name);
7573

7674
// see if an accessory with the same uuid has already been registered and restored from
7775
// the cached devices we stored in the `configureAccessory` method above
78-
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
76+
const existingAccessory = this.accessories.find(
77+
(accessory) => accessory.UUID === uuid
78+
);
7979

8080
if (existingAccessory) {
8181
// the accessory already exists
82-
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
82+
this.log.info(
83+
"Restoring existing accessory from cache:",
84+
existingAccessory.displayName
85+
);
8386

8487
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
8588
// existingAccessory.context.device = device;
8689
// this.api.updatePlatformAccessories([existingAccessory]);
8790

8891
// create the accessory handler for the restored accessory
8992
// this is imported from `platformAccessory.ts`
90-
new ExamplePlatformAccessory(this, existingAccessory);
93+
new PlatformSwitchAccessory(this, existingAccessory);
9194

9295
// it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.:
9396
// remove platform accessories when no longer present
94-
// this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]);
95-
// this.log.info('Removing existing accessory from cache:', existingAccessory.displayName);
97+
// this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
98+
// existingAccessory,
99+
// ]);
100+
// this.log.info(
101+
// "Removing existing accessory from cache:",
102+
// existingAccessory.displayName
103+
// );
96104
} else {
97105
// the accessory does not yet exist, so we need to create it
98-
this.log.info('Adding new accessory:', device.exampleDisplayName);
106+
this.log.info("Adding new accessory:", button.name);
99107

100108
// create a new accessory
101-
const accessory = new this.api.platformAccessory(device.exampleDisplayName, uuid);
109+
const accessory = new this.api.platformAccessory(button.name, uuid);
102110

103111
// store a copy of the device object in the `accessory.context`
104112
// the `context` property can be used to store any data about the accessory you may need
105-
accessory.context.device = device;
113+
accessory.context.switch = button;
106114

107115
// create the accessory handler for the newly create accessory
108116
// this is imported from `platformAccessory.ts`
109-
new ExamplePlatformAccessory(this, accessory);
117+
new PlatformSwitchAccessory(this, accessory);
110118

111119
// link the accessory to your platform
112-
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
120+
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
121+
accessory,
122+
]);
113123
}
114124
}
115125
}

0 commit comments

Comments
 (0)