@@ -2,26 +2,55 @@ import { writeFileSync } from 'node:fs'
22import { dirname , resolve } from 'node:path'
33import { fileURLToPath } from 'node:url'
44
5- import { Categories , Characteristic , Service } from '@homebridge/hap-nodejs'
5+ import { Access , Categories , Characteristic , Formats , Perms , Service , Units } from '@homebridge/hap-nodejs'
66
77const __dirname = dirname ( fileURLToPath ( import . meta. url ) )
88
9+ // @ts -expect-error - TS7016
10+ const { EveHomeKitTypes } = await import ( 'homebridge-lib/EveHomeKitTypes' )
11+ // @ts -expect-error - TS7016
12+ const { MyHomeKitTypes } = await import ( 'homebridge-lib/MyHomeKitTypes' )
13+
14+ const homebridge = {
15+ hap : {
16+ Access,
17+ Categories,
18+ Characteristic,
19+ Formats,
20+ Perms,
21+ Service,
22+ Units,
23+ } ,
24+ }
25+
26+ const customTypes = [
27+ new EveHomeKitTypes ( homebridge ) ,
28+ new MyHomeKitTypes ( homebridge ) ,
29+ ]
30+
931/** Generate Service Types */
1032
1133let Services = [
1234 'export const Services = {' ,
1335] as any
1436
15- const uuidMap = new Map ( )
37+ const serviceUUIDs = new Set ( )
38+ const serviceNames = new Set ( )
39+ const serviceEntries = [
40+ ...Object . entries ( Service ) ,
41+ ...customTypes . flatMap ( customType => Object . entries ( customType . Services ) ) ,
42+ ]
1643
17- for ( const [ name , value ] of Object . entries ( Service ) ) {
44+ for ( const [ name , value ] of serviceEntries ) {
1845 if ( value . UUID ) {
19- if ( ! uuidMap . has ( value . UUID ) ) {
20- // If the UUID does not exist, add a new entry
46+ if ( ! serviceUUIDs . has ( value . UUID ) ) {
2147 Services . push ( ` '${ value . UUID } ': '${ name } ',` )
22- uuidMap . set ( value . UUID , Services . length - 1 )
48+ serviceUUIDs . add ( value . UUID )
49+ }
50+ if ( ! serviceNames . has ( name ) ) {
51+ Services . push ( ` '${ name } ': '${ value . UUID } ',` )
52+ serviceNames . add ( name )
2353 }
24- Services . push ( ` '${ name } ': '${ value . UUID } ',` )
2554 }
2655}
2756
@@ -34,10 +63,24 @@ let Characteristics = [
3463 'export const Characteristics = {' ,
3564] as any
3665
37- for ( const [ name , value ] of Object . entries ( Characteristic ) ) {
66+ const characteristicEntries = [
67+ ...Object . entries ( Characteristic ) ,
68+ ...customTypes . flatMap ( customType => Object . entries ( customType . Characteristics ) ) ,
69+ ]
70+
71+ const characteristicUUIDs = new Set ( )
72+ const characteristicNames = new Set ( )
73+
74+ for ( const [ name , value ] of characteristicEntries ) {
3875 if ( value . UUID ) {
39- Characteristics . push ( ` '${ value . UUID } ': '${ name } ',` )
40- Characteristics . push ( ` '${ name } ': '${ value . UUID } ',` )
76+ if ( ! characteristicUUIDs . has ( value . UUID ) ) {
77+ Characteristics . push ( ` '${ value . UUID } ': '${ name } ',` )
78+ characteristicUUIDs . add ( value . UUID )
79+ }
80+ if ( ! characteristicNames . has ( name ) ) {
81+ Characteristics . push ( ` '${ name } ': '${ value . UUID } ',` )
82+ characteristicNames . add ( name )
83+ }
4184 }
4285}
4386
0 commit comments