@@ -5,143 +5,143 @@ import {
55 Logger ,
66 PlatformAccessory ,
77 PlatformConfig ,
8- Service
8+ Service ,
99} from 'homebridge' ;
1010
1111import { PLATFORM_NAME , PLUGIN_NAME } from './settings' ;
1212import { HomewizardPrincessHeaterAccessory } from './platformAccessory' ;
13- import { PrincessHeaterAccessoryContext , ResponseWsIncomingMessage , WsIncomingMessage } from " ./ws/types" ;
14- import { DeviceType , MessageType } from " ./ws/const" ;
15- import { getDevices , login } from " ./http" ;
16- import { open } from " ./ws" ;
17- import { WsClient } from " ./ws/client" ;
13+ import { PrincessHeaterAccessoryContext , ResponseWsIncomingMessage , WsIncomingMessage } from ' ./ws/types' ;
14+ import { DeviceType , MessageType } from ' ./ws/const' ;
15+ import { getDevices , login } from ' ./http' ;
16+ import { open } from ' ./ws' ;
17+ import { WsClient } from ' ./ws/client' ;
1818
1919/**
2020 * HomebridgePlatform
2121 * This class is the main constructor for your plugin, this is where you should
2222 * parse the user config and discover/register accessories with Homebridge.
2323 */
2424export class HomebridgePrincessHeaterPlatform implements DynamicPlatformPlugin {
25- public readonly Service : typeof Service = this . api . hap . Service ;
26- public readonly Characteristic : typeof Characteristic = this . api . hap . Characteristic ;
27-
28- // this is used to track restored cached accessories
29- public readonly accessories : PlatformAccessory < PrincessHeaterAccessoryContext > [ ] = [ ] ;
30-
31- constructor (
32- public readonly log : Logger ,
33- public readonly config : PlatformConfig ,
34- public readonly api : API ,
35- ) {
36- this . log . debug ( 'Finished initializing platform:' , this . config . name ) ;
37-
38- // When this event is fired it means Homebridge has restored all cached accessories from disk.
39- // Dynamic Platform plugins should only register new accessories after this event was fired,
40- // in order to ensure they weren't added to homebridge already. This event can also be used
41- // to start discovery of new accessories.
42- this . api . on ( 'didFinishLaunching' , ( ) => {
43- log . debug ( 'Executed didFinishLaunching callback' ) ;
44- // run the method to discover / register your devices as accessories
45- this . discoverDevices ( ) ;
46- } ) ;
47- }
48-
49- /**
50- * This function is invoked when homebridge restores cached accessories from disk at startup.
51- * It should be used to setup event handlers for characteristics and update respective values.
52- */
53- configureAccessory ( accessory : PlatformAccessory < PrincessHeaterAccessoryContext > ) {
54- this . log . info ( 'Loading accessory from cache:' , accessory . displayName ) ;
55-
56- // add the restored accessory to the accessories cache so we can track if it has already been registered
57- this . accessories . push ( accessory ) ;
58- }
59-
60- /**
61- * This is an example method showing how to register discovered accessories.
62- * Accessories must only be registered once, previously created accessories
63- * must not be registered again to prevent "duplicate UUID" errors.
64- */
65- discoverDevices ( ) {
66-
67- const authResponsePromise = login ( this . config . authorization as string ) ;
68- const wsPromise = open ( ) ;
69-
70- Promise . all ( [ authResponsePromise , wsPromise ] ) . then ( ( [ auth , ws ] ) => {
71-
72- const client = new WsClient ( ws )
73-
74- ws . on ( 'message' , ( message : string ) => {
75- const incomingMessage : WsIncomingMessage = JSON . parse ( message )
76- this . log . debug ( 'Incoming message:' , incomingMessage )
77- if (
25+ public readonly Service : typeof Service = this . api . hap . Service ;
26+ public readonly Characteristic : typeof Characteristic = this . api . hap . Characteristic ;
27+
28+ // this is used to track restored cached accessories
29+ public readonly accessories : PlatformAccessory < PrincessHeaterAccessoryContext > [ ] = [ ] ;
30+
31+ constructor (
32+ public readonly log : Logger ,
33+ public readonly config : PlatformConfig ,
34+ public readonly api : API ,
35+ ) {
36+ this . log . debug ( 'Finished initializing platform:' , this . config . name ) ;
37+
38+ // When this event is fired it means Homebridge has restored all cached accessories from disk.
39+ // Dynamic Platform plugins should only register new accessories after this event was fired,
40+ // in order to ensure they weren't added to homebridge already. This event can also be used
41+ // to start discovery of new accessories.
42+ this . api . on ( 'didFinishLaunching' , ( ) => {
43+ log . debug ( 'Executed didFinishLaunching callback' ) ;
44+ // run the method to discover / register your devices as accessories
45+ this . discoverDevices ( ) ;
46+ } ) ;
47+ }
48+
49+ /**
50+ * This function is invoked when homebridge restores cached accessories from disk at startup.
51+ * It should be used to setup event handlers for characteristics and update respective values.
52+ */
53+ configureAccessory ( accessory : PlatformAccessory < PrincessHeaterAccessoryContext > ) {
54+ this . log . info ( 'Loading accessory from cache:' , accessory . displayName ) ;
55+
56+ // add the restored accessory to the accessories cache so we can track if it has already been registered
57+ this . accessories . push ( accessory ) ;
58+ }
59+
60+ /**
61+ * This is an example method showing how to register discovered accessories.
62+ * Accessories must only be registered once, previously created accessories
63+ * must not be registered again to prevent "duplicate UUID" errors.
64+ */
65+ discoverDevices ( ) {
66+
67+ const authResponsePromise = login ( this . config . authorization as string ) ;
68+ const wsPromise = open ( ) ;
69+
70+ Promise . all ( [ authResponsePromise , wsPromise ] ) . then ( ( [ auth , ws ] ) => {
71+
72+ const client = new WsClient ( ws ) ;
73+
74+ ws . on ( 'message' , ( message : string ) => {
75+ const incomingMessage : WsIncomingMessage = JSON . parse ( message ) ;
76+ this . log . debug ( 'Incoming message:' , incomingMessage ) ;
77+ if (
7878 'message_id' in incomingMessage &&
79- incomingMessage . message_id in client . outgoingMessages &&
80- client . outgoingMessages [ incomingMessage . message_id ] . type === MessageType . Hello
81- ) {
82- return this . onHelloMessageResponse ( incomingMessage , client ) ;
83- }
79+ incomingMessage . message_id in client . outgoingMessages &&
80+ client . outgoingMessages [ incomingMessage . message_id ] . type === MessageType . Hello
81+ ) {
82+ return this . onHelloMessageResponse ( incomingMessage , client ) ;
83+ }
84+ } ) ;
85+
86+ client . send ( {
87+ type : MessageType . Hello ,
88+ message_id : client . generateMessageId ( ) ,
89+ version : '2.4.0' ,
90+ os : 'ios' ,
91+ source : 'climate' ,
92+ compatibility : 3 ,
93+ token : auth . token ,
94+ } ) ;
8495 } ) ;
96+ }
97+
98+ async onHelloMessageResponse ( response : ResponseWsIncomingMessage , wsClient : WsClient ) {
99+ this . log . debug ( 'Received a response to Hello message. Going to get list of devices...' , response ) ;
100+
101+ const authorization : string = this . config . authorization as string ;
102+ const devices = await getDevices ( authorization ) ;
103+
104+ this . log . debug ( 'Received a list of devices:' , devices . map ( d => d . name ) ) ;
85105
86- client . send ( {
87- type : MessageType . Hello ,
88- message_id : client . generateMessageId ( ) ,
89- version : "2.4.0" ,
90- os : "ios" ,
91- source : "climate" ,
92- compatibility : 3 ,
93- token : auth . token
94- } )
95- } )
96- }
97-
98- async onHelloMessageResponse ( response : ResponseWsIncomingMessage , wsClient : WsClient ) {
99- this . log . debug ( 'Received a response to Hello message. Going to get list of devices...' , response ) ;
100-
101- const authorization : string = this . config . authorization as string ;
102- const devices = await getDevices ( authorization ) ;
103-
104- this . log . debug ( 'Received a list of devices:' , devices . map ( d => d . name ) ) ;
105-
106- const devicesUUIDs = devices . map ( d => this . api . hap . uuid . generate ( d . identifier ) ) ;
107-
108- this . accessories
106+ const devicesUUIDs = devices . map ( d => this . api . hap . uuid . generate ( d . identifier ) ) ;
107+
108+ this . accessories
109109 . filter ( a => ! devicesUUIDs . includes ( a . UUID ) )
110110 . forEach ( a => {
111111 this . api . unregisterPlatformAccessories ( PLUGIN_NAME , PLATFORM_NAME , [ a ] ) ;
112112 this . log . info ( 'Removing existing accessory from cache:' , a . displayName ) ;
113- } )
113+ } ) ;
114114
115- devices . forEach ( ( device , i ) => {
115+ devices . forEach ( ( device , i ) => {
116116
117- if ( device . type === DeviceType . Heater ) {
117+ if ( device . type === DeviceType . Heater ) {
118118
119- const uuid = devicesUUIDs [ i ] ;
119+ const uuid = devicesUUIDs [ i ] ;
120120
121- const existingAccessory = this . accessories . find ( accessory => accessory . UUID === uuid ) ;
121+ const existingAccessory = this . accessories . find ( accessory => accessory . UUID === uuid ) ;
122122
123- if ( existingAccessory ) {
124- this . log . info ( 'Restoring existing accessory from cache:' , existingAccessory . displayName ) ;
125- new HomewizardPrincessHeaterAccessory ( this , existingAccessory , wsClient ) ;
126- this . api . updatePlatformAccessories ( [ existingAccessory ] ) ;
127- } else {
128- this . log . info ( 'Adding new accessory:' , device . name ) ;
123+ if ( existingAccessory ) {
124+ this . log . info ( 'Restoring existing accessory from cache:' , existingAccessory . displayName ) ;
125+ new HomewizardPrincessHeaterAccessory ( this , existingAccessory , wsClient ) ;
126+ this . api . updatePlatformAccessories ( [ existingAccessory ] ) ;
127+ } else {
128+ this . log . info ( 'Adding new accessory:' , device . name ) ;
129129
130- const accessory = new this . api . platformAccessory ( device . name , uuid ) ;
130+ const accessory = new this . api . platformAccessory ( device . name , uuid ) ;
131131
132- accessory . context . device = device ;
132+ accessory . context . device = device ;
133133
134- new HomewizardPrincessHeaterAccessory (
134+ new HomewizardPrincessHeaterAccessory (
135135 this ,
136- accessory as PlatformAccessory < PrincessHeaterAccessoryContext > ,
137- wsClient
138- ) ;
136+ accessory as PlatformAccessory < PrincessHeaterAccessoryContext > ,
137+ wsClient ,
138+ ) ;
139139
140- this . api . registerPlatformAccessories ( PLUGIN_NAME , PLATFORM_NAME , [ accessory ] ) ;
140+ this . api . registerPlatformAccessories ( PLUGIN_NAME , PLATFORM_NAME , [ accessory ] ) ;
141+ }
142+ } else {
143+ this . log . info ( 'Unsupported device type:' , device . type ) ;
141144 }
142- } else {
143- this . log . info ( 'Unsupported device type:' , device . type ) ;
144- }
145- } )
146- }
145+ } ) ;
146+ }
147147}
0 commit comments