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 */
1119export 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