-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplatform.ts
177 lines (156 loc) · 5.49 KB
/
platform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import rp from 'request-promise';
import jwt from 'jsonwebtoken';
import WebSocket from 'ws';
import {
API,
DynamicPlatformPlugin,
Logger,
PlatformAccessory,
PlatformConfig,
Service,
Characteristic,
} from 'homebridge';
import {
PLATFORM_NAME,
PLUGIN_NAME,
MYSMARTBLINDS_DOMAIN,
TILTSMARTHOME_OPTIONS,
TILTSMARTHOME_URL,
TILTSMARTHOME_WSS,
} from './settings';
import {
MySmartRollerShadesConfig,
MySmartRollerShadesAuth,
MySmartRollerShade,
} from './config';
import { MySmartRollerShadesAccessory } from './platformAccessory';
export class MySmartRollerShadesBridgePlatform implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
// this is used to track restored cached accessories
public readonly accessories: PlatformAccessory[] = [];
auth!: MySmartRollerShadesAuth;
authToken!: string;
authTokenInterval?: NodeJS.Timeout;
tiltWebSocket!: WebSocket;
requestOptions!: {
method: string;
uri: string;
json: boolean;
headers: {
Authorization: string;
};
};
constructor(
public readonly log: Logger,
public readonly config: PlatformConfig & MySmartRollerShadesConfig,
public readonly api: API,
) {
/* plugin not configured check */
if (!config) {
this.log.info('No configuration found for platform ', PLATFORM_NAME);
return;
}
/* setup config */
this.config = config;
this.log = log;
try {
if (!this.config.username) {
throw new Error('MySmartRollerShades Bridge - You must provide a username');
}
if (!this.config.password) {
throw new Error('MySmartRollershades Bridge - You must provide a password');
}
this.auth = {
username: this.config.username,
password: this.config.password,
};
} catch(err) {
this.log.error(err);
}
this.log.debug('Finished initializing platform:', this.config.name);
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
// run the method to discover / register your devices as accessories
this.discoverDevices();
});
}
configureAccessory(accessory: PlatformAccessory) {
this.log.info('Loading roller shade from cache:', accessory.displayName);
// add the restored accessory to the accessories cache so we can track if it has already been registered
this.accessories.push(accessory);
}
refreshAuthToken() {
return rp({
method: 'POST',
uri: `https://${MYSMARTBLINDS_DOMAIN}/oauth/token`,
json: true,
body: Object.assign(
{},
TILTSMARTHOME_OPTIONS,
this.auth,
),
}).then((response) => {
this.authToken = response.access_token;
this.requestOptions = {
method: 'GET',
uri: TILTSMARTHOME_URL,
json: true,
headers: { Authorization: `Bearer ${response.access_token}` },
};
if (this.config.allowDebug) {
const authTokenExpireDate = new Date((jwt.decode(response.id_token || '{ exp: 0 }') as { exp: number }).exp * 1000).toISOString();
this.log.info(`authToken refresh, now expires ${authTokenExpireDate}`);
}
});
}
discoverDevices() {
this.log.info('This plugin is still a work in progress');
this.refreshAuthToken().then(() => {
this.authTokenInterval = setInterval(this.refreshAuthToken.bind(this), 1000 * 60 * 60 * 8);
rp(this.requestOptions).then((response) => {
// attempt WebSocket setup
try {
this.tiltWebSocket = new WebSocket(TILTSMARTHOME_WSS, [], { headers: this.requestOptions.headers });
this.tiltWebSocket.on('open', () => {
this.log.warn('WebSocket has connected!');
});
this.tiltWebSocket.on('message', (data: string) => {
this.log.warn(`WebSocket message recieved: ${JSON.parse(data)}`);
});
} catch(err) {
this.log.warn('WebSocket connection failed!', err);
}
// create blind accessories
response.rooms.forEach((room: { rollerShades: MySmartRollerShade[] }) => {
room.rollerShades.forEach((rollerShade) => {
const {
id: shadeID,
name: shadeName,
} = rollerShade;
const uuid = this.api.hap.uuid.generate(shadeID);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
if (existingAccessory) {
this.log.debug(`Restore cached roller shade: ${shadeName}`);
new MySmartRollerShadesAccessory(this, existingAccessory);
this.api.updatePlatformAccessories([existingAccessory]);
} else {
this.log.info(`Adding new roller shade: ${shadeName}`);
// create a new accessory
const accessory = new this.api.platformAccessory(shadeName, uuid);
accessory.context.shade = {
name: shadeName,
id: shadeID,
shadePosition: 100, // change to real value via get shade
batteryLevel: 100, // change to real value via get shade
};
new MySmartRollerShadesAccessory(this, accessory);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
});
// need to figure out how to handle deleted roller shades
});
});
});
}
}