1+ import { MatterAccessory } from 'homebridge' ;
2+
13import { MQTTAccessory } from './mqtt.js' ;
24
35import { Battery } from '../addons/battery.js' ;
@@ -10,26 +12,38 @@ import { strings } from '../../i18n/i18n.js';
1012
1113import { CharacteristicKey } from '../../model/enums.js' ;
1214import { HomeKitCharacteristicKey } from '../../model/homekit.js' ;
15+ import { MATTER_SERIAL_MAX_LEN , MatterType } from '../../model/matter.js' ;
1316import { BaseAccessoryConfig , MQTTAccessoryDependency } from '../../model/types.js' ;
1417
1518import getVersion from '../../tools/version.js' ;
1619
1720export abstract class BaseAccessory < C extends BaseAccessoryConfig = BaseAccessoryConfig > extends MQTTAccessory < C > {
1821
22+ private _UUID ?: string ;
23+
24+ private readonly manufacturer : string ;
25+ private readonly model : string ;
26+ private readonly serialNumber : string ;
27+ private readonly softwareVersion : string ;
28+
1929 constructor ( dependency : MQTTAccessoryDependency < C > ) {
2030 super ( dependency ) ;
2131
32+ this . manufacturer = dependency . config . info . manufacturer ?? PLATFORM_NAME ;
33+ this . model = dependency . config . info . model ?? dependency . config . info . type ;
34+ this . serialNumber = this . identifier . length <= MATTER_SERIAL_MAX_LEN ? this . identifier : this . identifier . substring ( 0 , MATTER_SERIAL_MAX_LEN - 1 ) + '…' ;
35+ this . softwareVersion = dependency . config . info . version ?? getVersion ( ) ;
36+
2237 if ( ! dependency . isGrouped ) {
23- this . homekitAccessory . getService ( this . Service . AccessoryInformation ) !
24- . setCharacteristic ( this . Characteristic . Name , dependency . config . info . name )
25- . setCharacteristic ( this . Characteristic . ConfiguredName , dependency . config . info . name )
26- . setCharacteristic ( this . Characteristic . Manufacturer , dependency . config . info . manufacturer ?? PLATFORM_NAME )
27- . setCharacteristic ( this . Characteristic . Model , dependency . config . info . model ?? dependency . config . info . type )
28- . setCharacteristic ( this . Characteristic . SerialNumber , dependency . config . info . serialNumber ?? this . identifier )
29- . setCharacteristic ( this . Characteristic . FirmwareRevision , dependency . config . info . version ?? getVersion ( ) ) ;
38+ this . homekitAccessory . getService ( this . Service . AccessoryInformation ) !
39+ . setCharacteristic ( this . Characteristic . Name , dependency . config . info . name )
40+ . setCharacteristic ( this . Characteristic . ConfiguredName , dependency . config . info . name )
41+ . setCharacteristic ( this . Characteristic . Manufacturer , this . manufacturer )
42+ . setCharacteristic ( this . Characteristic . Model , this . model )
43+ . setCharacteristic ( this . Characteristic . SerialNumber , dependency . config . info . serialNumber ?? this . identifier )
44+ . setCharacteristic ( this . Characteristic . FirmwareRevision , this . softwareVersion ) ;
3045 }
3146
32-
3347 this . setup ( HomeKitCharacteristicKey . StatusActive , true ,
3448 'topicGetStatusActive' ,
3549 this . bindOnUpdateBooleanSingle ( HomeKitCharacteristicKey . StatusActive , 'valueStatusActive' ,
@@ -42,4 +56,71 @@ export abstract class BaseAccessory<C extends BaseAccessoryConfig = BaseAccessor
4256 override isOptionalCharacteristic ( key : CharacteristicKey ) : boolean {
4357 return super . isOptionalCharacteristic ( key ) || isOptionalHKCharacteristic ( key , this . config . info . type ) ;
4458 }
59+
60+ protected getMatterType ( ) : MatterType | undefined {
61+ return undefined ;
62+ } ;
63+
64+ private get matter ( ) {
65+ const matter = this . dependency . getMatter ( ) ;
66+ if ( matter === undefined ) {
67+ throw new Error ( `${ this . displayName } unable to get MatterAPI instance` ) ;
68+ }
69+ return matter ;
70+ }
71+
72+ private get deviceType ( ) {
73+
74+ const type = this . getMatterType ( ) ;
75+ if ( type !== undefined ) {
76+ return this . matter . deviceTypes [ type ] ;
77+ }
78+
79+ throw new Error ( `${ this . getMatterType . name } not implemented for ${ this . getHomeKitType ( ) } ` ) ;
80+ }
81+
82+ public get UUID ( ) : string {
83+ if ( ! this . _UUID ) {
84+ this . _UUID = this . matter . uuid . generate ( this . identifier ) ;
85+ }
86+ return this . _UUID ;
87+ }
88+
89+ protected get clusters ( ) : MatterAccessory [ 'clusters' ] | undefined {
90+ return undefined ;
91+ }
92+
93+ protected get handlers ( ) : MatterAccessory [ 'handlers' ] | undefined {
94+ return undefined ;
95+ }
96+
97+ protected get parts ( ) : MatterAccessory [ 'parts' ] | undefined {
98+ return undefined ; // Use this for sensors and groups
99+ }
100+
101+ public toMatterAccessory ( ) : MatterAccessory {
102+ return {
103+ UUID : this . UUID ,
104+ displayName : this . displayName ,
105+ deviceType : this . deviceType ,
106+ serialNumber : this . serialNumber ,
107+ manufacturer : this . manufacturer ,
108+ model : this . model ,
109+ context : {
110+ UUID : this . UUID ,
111+ deviceType : this . deviceType ,
112+ displayName : this . displayName ,
113+ serialNumber : this . serialNumber ,
114+ manufacturer : this . manufacturer ,
115+ model : this . model ,
116+ softwareVersion : this . softwareVersion ,
117+ clusters : this . clusters ,
118+ handlers : this . handlers ,
119+ parts : this . parts ,
120+ } ,
121+ clusters : this . clusters ,
122+ handlers : this . handlers ,
123+ parts : this . parts ,
124+ } ;
125+ }
45126}
0 commit comments