Skip to content

Commit

Permalink
Update platform.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
donavanbecker committed Dec 5, 2024
1 parent 04078cb commit 1b73fb1
Showing 1 changed file with 95 additions and 78 deletions.
173 changes: 95 additions & 78 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,88 +163,105 @@ export class SmartHQPlatform implements DynamicPlatformPlugin {
if (!username || !password) {
throw new Error('Username or password is undefined')
}
this.tokenSet = await getAccessToken(username, password)
await this.startRefreshTokenLogic()
try {
this.tokenSet = await getAccessToken(username, password)
} catch (e: any) {
await this.errorLog(`discoverDevices, Failed to get Access Token, Error Message: ${e.message ?? e}, Submit Bugs Here: https://bit.ly/smarthq-bug-report`)
}
try {
await this.startRefreshTokenLogic()
} catch (e: any) {
await this.errorLog(`discoverDevices, Failed to start Refresh Token Logic, Error Message: ${e.message ?? e}, Submit Bugs Here: https://bit.ly/smarthq-bug-report`)
}

const wssData = await axios.get('/websocket')
const connection = new ws(wssData.data.endpoint)
try {
const wssData = await axios.get('/websocket')

connection.on('message', (data) => {
const obj = JSON.parse(data.toString())
this.debugLog(`data: ${JSON.stringify(obj)}`)
const connection = new ws(wssData.data.endpoint)

if (obj.kind === 'publish#erd') {
const accessory = find(this.accessories, a => a.context.device.applianceId === obj.item.applianceId)
connection.on('message', (data) => {
const obj = JSON.parse(data.toString())
this.debugLog(`data: ${JSON.stringify(obj)}`)

if (!accessory) {
this.infoLog('Device not found in my list. Maybe we should rerun this plugin?')
return
}
if (obj.kind === 'publish#erd') {
const accessory = find(this.accessories, a => a.context.device.applianceId === obj.item.applianceId)

if (ERD_CODES[obj.item.erd]) {
this.debugLog(`ERD_CODES: ${ERD_CODES[obj.item.erd]}`)
this.debugLog(`obj>item>value: ${obj.item.value}`)
if (!accessory) {
this.infoLog('Device not found in my list. Maybe we should rerun this plugin?')
return
}

if (obj.item.erd === ERD_TYPES.UPPER_OVEN_LIGHT) {
const service = accessory.getService('Upper Oven Light')
if (service) {
service.updateCharacteristic(this.Characteristic.On, obj.item.value === '01')
if (ERD_CODES[obj.item.erd]) {
this.debugLog(`ERD_CODES: ${ERD_CODES[obj.item.erd]}`)
this.debugLog(`obj>item>value: ${obj.item.value}`)

if (obj.item.erd === ERD_TYPES.UPPER_OVEN_LIGHT) {
const service = accessory.getService('Upper Oven Light')
if (service) {
service.updateCharacteristic(this.Characteristic.On, obj.item.value === '01')
}
}
}
}
})

connection.on('close', (_, reason) => {
this.debugLog('Connection closed')
this.debugLog(`reason: ${reason.toString()}`)
})

connection.on('open', () => {
connection.send(
JSON.stringify({
kind: 'websocket#subscribe',
action: 'subscribe',
resources: ['/appliance/*/erd/*'],
}),
)

setInterval(
() =>
connection.send(
JSON.stringify({
kind: 'websocket#ping',
id: 'keepalive-ping',
action: 'ping',
}),
),
KEEPALIVE_TIMEOUT,
)
})
} catch (e: any) {
await this.errorLog(`discoverDevices, Failed to get Websocket Data, Error Message: ${e.message ?? e}, Submit Bugs Here: https://bit.ly/smarthq-bug-report`)
}

try {
const devices = await axios.get('/appliance')

const userId = devices.data.userId
for (const device of devices.data.items) {
const [{ data: details }, { data: features }] = await Promise.all([
axios.get(`/appliance/${device.applianceId}`),
axios.get(`/appliance/${device.applianceId}/feature`),
])
this.debugLog(`Device: ${JSON.stringify(device)}`)
switch (device.type) {
case 'Dishwasher':
await this.createSmartHQDishWasher(userId, device, details, features)
break
case 'Oven':
await this.createSmartHQOven(userId, device, details, features)
break
case 'Refrigerator':
await this.createSmartHQRefrigerator(userId, device, details, features)
break
default:
await this.warnLog(`Device Type Not Supported: ${device.type}`)
break
}
}
})

connection.on('close', (_, reason) => {
this.debugLog('Connection closed')
this.debugLog(`reason: ${reason.toString()}`)
})

connection.on('open', () => {
connection.send(
JSON.stringify({
kind: 'websocket#subscribe',
action: 'subscribe',
resources: ['/appliance/*/erd/*'],
}),
)

setInterval(
() =>
connection.send(
JSON.stringify({
kind: 'websocket#ping',
id: 'keepalive-ping',
action: 'ping',
}),
),
KEEPALIVE_TIMEOUT,
)
})

const devices = await axios.get('/appliance')

const userId = devices.data.userId
for (const device of devices.data.items) {
const [{ data: details }, { data: features }] = await Promise.all([
axios.get(`/appliance/${device.applianceId}`),
axios.get(`/appliance/${device.applianceId}/feature`),
])
this.debugLog(`Device: ${JSON.stringify(device)}`)
switch (device.type) {
case 'Dishwasher':
await this.createSmartHQDishWasher(userId, device, details, features)
break
case 'Oven':
await this.createSmartHQOven(userId, device, details, features)
break
case 'Refrigerator':
await this.createSmartHQRefrigerator(userId, device, details, features)
break
default:
await this.warnLog(`Device Type Not Supported: ${device.type}`)
break
}
} catch (e: any) {
await this.errorLog(`discoverDevices, Failed to get Devices Data, Error Message: ${e.message ?? e}, Submit Bugs Here: https://bit.ly/smarthq-bug-report`)
}
} catch (e: any) {
await this.errorLog(`discoverDevices, No Device Config, Error Message: ${e.message ?? e}, Submit Bugs Here: https://bit.ly/smarthq-bug-report`)
Expand All @@ -263,7 +280,7 @@ export class SmartHQPlatform implements DynamicPlatformPlugin {
if (!device.hide_device) {
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
existingAccessory.context.device = device
existingAccessory.context = { device: { ...details, ...features }, userId }
existingAccessory.context = { device: { brand: 'GE', ...details, ...features }, userId }
existingAccessory.displayName = await this.validateAndCleanDisplayName(device.nickname, 'nickname', device.nickname)
existingAccessory.context.device.firmware = device.firmware ?? await this.getVersion()
this.api.updatePlatformAccessories([existingAccessory])
Expand All @@ -283,7 +300,7 @@ export class SmartHQPlatform implements DynamicPlatformPlugin {
// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
accessory.context.device = device
accessory.context = { device: { ...details, ...features }, userId }
accessory.context = { device: { brand: 'GE', ...details, ...features }, userId }
accessory.displayName = await this.validateAndCleanDisplayName(device.nickname, 'nickname', device.nickname)
accessory.context.device.firmware = device.firmware ?? await this.getVersion()
// the accessory does not yet exist, so we need to create it
Expand Down Expand Up @@ -312,7 +329,7 @@ export class SmartHQPlatform implements DynamicPlatformPlugin {
if (!device.hide_device) {
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
existingAccessory.context.device = device
existingAccessory.context = { device: { ...details, ...features }, userId }
existingAccessory.context = { device: { brand: 'GE', ...details, ...features }, userId }
existingAccessory.displayName = await this.validateAndCleanDisplayName(device.nickname, 'nickname', device.nickname)
existingAccessory.context.device.firmware = device.firmware ?? await this.getVersion()
this.api.updatePlatformAccessories([existingAccessory])
Expand All @@ -332,7 +349,7 @@ export class SmartHQPlatform implements DynamicPlatformPlugin {
// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
accessory.context.device = device
accessory.context = { device: { ...details, ...features }, userId }
accessory.context = { device: { brand: 'GE', ...details, ...features }, userId }
accessory.displayName = await this.validateAndCleanDisplayName(device.nickname, 'nickname', device.nickname)
accessory.context.device.firmware = device.firmware ?? await this.getVersion()
// the accessory does not yet exist, so we need to create it
Expand Down Expand Up @@ -361,7 +378,7 @@ export class SmartHQPlatform implements DynamicPlatformPlugin {
if (!device.hide_device) {
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
existingAccessory.context.device = device
existingAccessory.context = { device: { ...details, ...features }, userId }
existingAccessory.context = { device: { brand: 'GE', ...details, ...features }, userId }
existingAccessory.displayName = await this.validateAndCleanDisplayName(device.nickname, 'nickname', device.nickname)
existingAccessory.context.device.firmware = device.firmware ?? await this.getVersion()
this.api.updatePlatformAccessories([existingAccessory])
Expand All @@ -381,7 +398,7 @@ export class SmartHQPlatform implements DynamicPlatformPlugin {
// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
accessory.context.device = device
accessory.context = { device: { ...details, ...features }, userId }
accessory.context = { device: { brand: 'GE', ...details, ...features }, userId }
accessory.displayName = await this.validateAndCleanDisplayName(device.nickname, 'nickname', device.nickname)
accessory.context.device.firmware = device.firmware ?? await this.getVersion()
// the accessory does not yet exist, so we need to create it
Expand Down

0 comments on commit 1b73fb1

Please sign in to comment.