Skip to content

Commit

Permalink
refactor(philips-hue): Proper error handling for Promises (no-floatin…
Browse files Browse the repository at this point in the history
…g-promises)

refactor
  • Loading branch information
banksio committed Nov 4, 2020
1 parent 5cdb5e9 commit e49c479
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/notification/philips-hue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,30 @@ const adjustLightsWithAPI = (hueBridge: import('node-hue-api/lib/api/Api')) => {
const arrayOfIDs = lightIds.split(',');
arrayOfIDs.forEach(light => {
logger.debug('adjusting all hue lights');
hueBridge.lights.setLightState(light, lightState);
hueBridge.lights.setLightState(light, lightState).then(result => {
logger.debug(`Light state change was successful? ${result}`);
},
error => {
logger.error('Could not update state of light');
logger.error(error);
});
});
} else { // Adjust all light IDs
hueBridge.lights.getAll().then((allLights: any[]) => {
allLights.forEach((light: any) => {
logger.debug('adjusting specified lights');
hueBridge.lights.setLightState(light, lightState);
hueBridge.lights.setLightState(light, lightState).then(result => {
logger.debug(`Light state change was successful? ${result}`);
},
error => {
logger.error('Could not update state of light');
logger.error(error);
});;
});
}).catch(error => {
logger.error('Failed to get all lights.');
logger.error(error);
throw error;
});
}
};
Expand All @@ -78,14 +94,14 @@ export function adjustPhilipsHueLights() {
const remoteBootstrap = hueAPI.api.createRemote(clientId, clientSecret);
if (hue.accessToken && hue.refreshToken) {
remoteBootstrap.connectWithTokens(accessToken, refreshToken, remoteApiUsername)
.catch(error => {
logger.error('Failed to get a remote Hue connection using supplied tokens.');
logger.error(error);
throw error;
})
.then(hueBridge => {
adjustLightsWithAPI(hueBridge);
logger.info('✔ adjusted Hue lights over cloud');
},
error => {
logger.error('Failed to get a remote Hue connection using supplied tokens.');
logger.error(error);
throw error;
});
}
})();
Expand Down

0 comments on commit e49c479

Please sign in to comment.