Skip to content

Commit

Permalink
🐛 Fix Philips Hue API Connection (#2631)
Browse files Browse the repository at this point in the history
* 🐛 Fix Philips Hue API Connection

* Philips Hue Node: Fixed typo in  update operation description
  • Loading branch information
that-one-tom authored Jan 13, 2022
1 parent c9e1892 commit e0ef645
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export class PhilipsHueOAuth2Api implements ICredentialType {
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden',
default: 'https://api.meethue.com/oauth2/auth',
default: 'https://api.meethue.com/v2/oauth2/authorize',
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden',
default: 'https://api.meethue.com/oauth2/token',
default: 'https://api.meethue.com/v2/oauth2/token',
},
{
displayName: 'Auth URI Query Parameters',
Expand Down
11 changes: 6 additions & 5 deletions packages/nodes-base/nodes/PhilipsHue/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function philipsHueApiRequest(this: IExecuteFunctions | ILoadOption
method,
body,
qs,
uri: uri || `https://api.meethue.com${resource}`,
uri: uri || `https://api.meethue.com/route${resource}`,
json: true,
};
try {
Expand All @@ -36,22 +36,23 @@ export async function philipsHueApiRequest(this: IExecuteFunctions | ILoadOption
}

//@ts-ignore
return await this.helpers.requestOAuth2.call(this, 'philipsHueOAuth2Api', options, { tokenType: 'Bearer' });
const response = await this.helpers.requestOAuth2.call(this, 'philipsHueOAuth2Api', options, { tokenType: 'Bearer' });
return response;
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}

export async function getUser(this: IExecuteFunctions | ILoadOptionsFunctions): Promise<any> { // tslint:disable-line:no-any
const { whitelist } = await philipsHueApiRequest.call(this, 'GET', '/bridge/0/config', {}, {});
const { whitelist } = await philipsHueApiRequest.call(this, 'GET', '/api/0/config', {}, {});
//check if there is a n8n user
for (const user of Object.keys(whitelist)) {
if (whitelist[user].name === 'n8n') {
return user;
}
}
// n8n user was not fount then create the user
await philipsHueApiRequest.call(this, 'PUT', '/bridge/0/config', { linkbutton: true });
const { success } = await philipsHueApiRequest.call(this, 'POST', '/bridge', { devicetype: 'n8n' });
await philipsHueApiRequest.call(this, 'PUT', '/api/0/config', { linkbutton: true });
const { success } = await philipsHueApiRequest.call(this, 'POST', '/api', { devicetype: 'n8n' });
return success.username;
}
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/PhilipsHue/LightDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const lightOperations: INodeProperties[] = [
{
name: 'Update',
value: 'update',
description: 'Update an light',
description: 'Update a light',
},
],
default: 'update',
Expand Down
12 changes: 6 additions & 6 deletions packages/nodes-base/nodes/PhilipsHue/PhilipsHue.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export class PhilipsHue implements INodeType {
const lights = await philipsHueApiRequest.call(
this,
'GET',
`/bridge/${user}/lights`,
`/api/${user}/lights`,
);

const groups = await philipsHueApiRequest.call(
this,
'GET',
`/bridge/${user}/groups`,
`/api/${user}/groups`,
);

for (const light of Object.keys(lights)) {
Expand Down Expand Up @@ -144,7 +144,7 @@ export class PhilipsHue implements INodeType {
const data = await philipsHueApiRequest.call(
this,
'PUT',
`/bridge/${user}/lights/${lightId}/state`,
`/api/${user}/lights/${lightId}/state`,
body,
);

Expand All @@ -161,15 +161,15 @@ export class PhilipsHue implements INodeType {

const user = await getUser.call(this);

responseData = await philipsHueApiRequest.call(this, 'DELETE', `/bridge/${user}/lights/${lightId}`);
responseData = await philipsHueApiRequest.call(this, 'DELETE', `/api/${user}/lights/${lightId}`);

}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;

const user = await getUser.call(this);

const lights = await philipsHueApiRequest.call(this, 'GET', `/bridge/${user}/lights`);
const lights = await philipsHueApiRequest.call(this, 'GET', `/api/${user}/lights`);

responseData = Object.values(lights);

Expand All @@ -183,7 +183,7 @@ export class PhilipsHue implements INodeType {

const user = await getUser.call(this);

responseData = await philipsHueApiRequest.call(this, 'GET', `/bridge/${user}/lights/${lightId}`);
responseData = await philipsHueApiRequest.call(this, 'GET', `/api/${user}/lights/${lightId}`);
}
}
}
Expand Down

0 comments on commit e0ef645

Please sign in to comment.