Fixes to PhilipsHue Node

This commit is contained in:
Jan Oberhauser
2020-08-03 10:06:46 +02:00
parent ea7f1c4b84
commit 06a5ed3690
6 changed files with 50 additions and 46 deletions

View File

@@ -3,8 +3,8 @@ import {
NodePropertyTypes, NodePropertyTypes,
} from 'n8n-workflow'; } from 'n8n-workflow';
export class PhilipHueOAuth2Api implements ICredentialType { export class PhilipsHueOAuth2Api implements ICredentialType {
name = 'philipHueOAuth2Api'; name = 'philipsHueOAuth2Api';
extends = [ extends = [
'oAuth2Api', 'oAuth2Api',
]; ];

View File

@@ -11,7 +11,7 @@ import {
IDataObject, IDataObject,
} from 'n8n-workflow'; } from 'n8n-workflow';
export async function philiphueApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function philipsHueApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -36,7 +36,7 @@ export async function philiphueApiRequest(this: IExecuteFunctions | ILoadOptions
} }
//@ts-ignore //@ts-ignore
return await this.helpers.requestOAuth2.call(this, 'philipHueOAuth2Api', options, { tokenType: 'Bearer' }); return await this.helpers.requestOAuth2.call(this, 'philipsHueOAuth2Api', options, { tokenType: 'Bearer' });
} catch (error) { } catch (error) {
if (error.response && error.response.body && error.response.body.error) { if (error.response && error.response.body && error.response.body.error) {
@@ -52,7 +52,7 @@ export async function philiphueApiRequest(this: IExecuteFunctions | ILoadOptions
} }
export async function getUser(this: IExecuteFunctions | ILoadOptionsFunctions): Promise<any> { // tslint:disable-line:no-any export async function getUser(this: IExecuteFunctions | ILoadOptionsFunctions): Promise<any> { // tslint:disable-line:no-any
const { whitelist } = await philiphueApiRequest.call(this, 'GET', '/bridge/0/config', {}, {}); const { whitelist } = await philipsHueApiRequest.call(this, 'GET', '/bridge/0/config', {}, {});
//check if there is a n8n user //check if there is a n8n user
for (const user of Object.keys(whitelist)) { for (const user of Object.keys(whitelist)) {
if (whitelist[user].name === 'n8n') { if (whitelist[user].name === 'n8n') {
@@ -60,7 +60,7 @@ export async function getUser(this: IExecuteFunctions | ILoadOptionsFunctions):
} }
} }
// n8n user was not fount then create the user // n8n user was not fount then create the user
await philiphueApiRequest.call(this, 'PUT', '/bridge/0/config', { linkbutton: true }); await philipsHueApiRequest.call(this, 'PUT', '/bridge/0/config', { linkbutton: true });
const { success } = await philiphueApiRequest.call(this, 'POST', '/bridge', { devicetype: 'n8n' }); const { success } = await philipsHueApiRequest.call(this, 'POST', '/bridge', { devicetype: 'n8n' });
return success.username; return success.username;
} }

View File

@@ -42,6 +42,7 @@ export const lightOperations = [
] as INodeProperties[]; ] as INodeProperties[];
export const lightFields = [ export const lightFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* light:delete */ /* light:delete */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@@ -62,6 +63,7 @@ export const lightFields = [
}, },
default: '', default: '',
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* light:getAll */ /* light:getAll */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@@ -106,6 +108,7 @@ export const lightFields = [
default: 100, default: 100,
description: 'How many results to return.', description: 'How many results to return.',
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* light:get */ /* light:get */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@@ -126,6 +129,7 @@ export const lightFields = [
}, },
default: '', default: '',
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* light:update */ /* light:update */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@@ -164,8 +168,8 @@ export const lightFields = [
], ],
}, },
}, },
default: '', default: true,
description: 'On/Off state of the light. On=true, Off=false', description: 'On/Off state of the light.',
}, },
{ {
displayName: 'Additional Fields', displayName: 'Additional Fields',
@@ -228,7 +232,7 @@ export const lightFields = [
maxValue: 254, maxValue: 254,
}, },
default: 0, default: 0,
description: 'Increments or decrements the value of the brightness. bri_inc is ignored if the bri attribute is provided.', description: 'Increments or decrements the value of the brightness. This value is ignored if the Brightness attribute is provided.',
}, },
{ {
displayName: 'Color Temperature', displayName: 'Color Temperature',
@@ -248,6 +252,23 @@ export const lightFields = [
default: 0, default: 0,
description: 'Increments or decrements the value of the ct. ct_inc is ignored if the ct attribute is provided', description: 'Increments or decrements the value of the ct. ct_inc is ignored if the ct attribute is provided',
}, },
{
displayName: 'Coordinates',
name: 'xy',
type: 'string',
default: '',
placeholder: '0.64394,0.33069',
description: `The x and y coordinates of a color in CIE color space.</br>
The first entry is the x coordinate and the second entry is the y coordinate. Both x and y are between 0 and 1`,
},
{
displayName: 'Coordinates Increments',
name: 'xy_inc',
type: 'string',
default: '',
placeholder: '0.5,0.5',
description: `Increments or decrements the value of the xy. This value is ignored if the Coordinates attribute is provided. Any ongoing color transition is stopped. Max value [0.5, 0.5]`,
},
{ {
displayName: 'Dynamic Effect', displayName: 'Dynamic Effect',
name: 'effect', name: 'effect',
@@ -285,7 +306,7 @@ export const lightFields = [
maxValue: 65534, maxValue: 65534,
}, },
default: 0, default: 0,
description: 'Increments or decrements the value of the hue. hue_inc is ignored if the hue attribute is provided.', description: 'Increments or decrements the value of the hue. Hue Increments is ignored if the Hue attribute is provided.',
}, },
{ {
displayName: 'Saturation', displayName: 'Saturation',
@@ -307,7 +328,7 @@ export const lightFields = [
maxValue: 254, maxValue: 254,
}, },
default: 0, default: 0,
description: 'Increments or decrements the value of the sat. sat_inc is ignored if the sat attribute is provided.', description: 'Increments or decrements the value of the sat. This value is ignored if the Saturation attribute is provided.',
}, },
{ {
displayName: 'Transition Time', displayName: 'Transition Time',
@@ -319,23 +340,6 @@ export const lightFields = [
default: 4, default: 4,
description: 'The duration in seconds of the transition from the lights current state to the new state', description: 'The duration in seconds of the transition from the lights current state to the new state',
}, },
{
displayName: 'Coordinates',
name: 'xy',
type: 'string',
default: '',
placeholder: '0.64394,0.33069',
description: `The x and y coordinates of a color in CIE color space.</br>
The first entry is the x coordinate and the second entry is the y coordinate. Both x and y are between 0 and 1`,
},
{
displayName: 'Coordinates Increments',
name: 'xy_inc',
type: 'string',
default: '',
placeholder: '0.5,0.5',
description: `Increments or decrements the value of the xy. xy_inc is ignored if the xy attribute is provided. Any ongoing color transition is stopped. Max value [0.5, 0.5]`,
},
], ],
}, },
] as INodeProperties[]; ] as INodeProperties[];

View File

@@ -12,7 +12,7 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
philiphueApiRequest, philipsHueApiRequest,
getUser, getUser,
} from './GenericFunctions'; } from './GenericFunctions';
@@ -21,24 +21,24 @@ import {
lightFields, lightFields,
} from './LightDescription'; } from './LightDescription';
export class PhilipHue implements INodeType { export class PhilipsHue implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Philip Hue', displayName: 'Philips Hue',
name: 'philipHue', name: 'philipsHue',
icon: 'file:philiphue.png', icon: 'file:philipshue.png',
group: ['input'], group: ['input'],
version: 1, version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Philip Hue API.', description: 'Consume Philips Hue API.',
defaults: { defaults: {
name: 'Philip Hue', name: 'Philips Hue',
color: '#063c9a', color: '#063c9a',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
credentials: [ credentials: [
{ {
name: 'philipHueOAuth2Api', name: 'philipsHueOAuth2Api',
required: true, required: true,
}, },
], ],
@@ -72,7 +72,7 @@ export class PhilipHue implements INodeType {
const user = await getUser.call(this); const user = await getUser.call(this);
const lights = await philiphueApiRequest.call( const lights = await philipsHueApiRequest.call(
this, this,
'GET', 'GET',
`/bridge/${user}/lights`, `/bridge/${user}/lights`,
@@ -128,7 +128,7 @@ export class PhilipHue implements INodeType {
const user = await getUser.call(this); const user = await getUser.call(this);
const data = await philiphueApiRequest.call( const data = await philipsHueApiRequest.call(
this, this,
'PUT', 'PUT',
`/bridge/${user}/lights/${lightId}/state`, `/bridge/${user}/lights/${lightId}/state`,
@@ -148,7 +148,7 @@ export class PhilipHue implements INodeType {
const user = await getUser.call(this); const user = await getUser.call(this);
responseData = await philiphueApiRequest.call(this, 'DELETE', `/bridge/${user}/lights/${lightId}`); responseData = await philipsHueApiRequest.call(this, 'DELETE', `/bridge/${user}/lights/${lightId}`);
} }
if (operation === 'getAll') { if (operation === 'getAll') {
@@ -156,7 +156,7 @@ export class PhilipHue implements INodeType {
const user = await getUser.call(this); const user = await getUser.call(this);
const lights = await philiphueApiRequest.call(this, 'GET', `/bridge/${user}/lights`); const lights = await philipsHueApiRequest.call(this, 'GET', `/bridge/${user}/lights`);
responseData = Object.values(lights); responseData = Object.values(lights);
@@ -170,7 +170,7 @@ export class PhilipHue implements INodeType {
const user = await getUser.call(this); const user = await getUser.call(this);
responseData = await philiphueApiRequest.call(this, 'GET', `/bridge/${user}/lights/${lightId}`); responseData = await philipsHueApiRequest.call(this, 'GET', `/bridge/${user}/lights/${lightId}`);
} }
} }
} }

View File

Before

Width:  |  Height:  |  Size: 7.0 KiB

After

Width:  |  Height:  |  Size: 7.0 KiB

View File

@@ -117,7 +117,7 @@
"dist/credentials/PayPalApi.credentials.js", "dist/credentials/PayPalApi.credentials.js",
"dist/credentials/PipedriveApi.credentials.js", "dist/credentials/PipedriveApi.credentials.js",
"dist/credentials/PipedriveOAuth2Api.credentials.js", "dist/credentials/PipedriveOAuth2Api.credentials.js",
"dist/credentials/PhilipHueOAuth2Api.credentials.js", "dist/credentials/PhilipsHueOAuth2Api.credentials.js",
"dist/credentials/Postgres.credentials.js", "dist/credentials/Postgres.credentials.js",
"dist/credentials/PostmarkApi.credentials.js", "dist/credentials/PostmarkApi.credentials.js",
"dist/credentials/QuestDb.credentials.js", "dist/credentials/QuestDb.credentials.js",
@@ -275,7 +275,7 @@
"dist/nodes/Pipedrive/PipedriveTrigger.node.js", "dist/nodes/Pipedrive/PipedriveTrigger.node.js",
"dist/nodes/Postgres/Postgres.node.js", "dist/nodes/Postgres/Postgres.node.js",
"dist/nodes/Postmark/PostmarkTrigger.node.js", "dist/nodes/Postmark/PostmarkTrigger.node.js",
"dist/nodes/PhilipHue/PhilipHue.node.js", "dist/nodes/PhilipsHue/PhilipsHue.node.js",
"dist/nodes/QuestDb/QuestDb.node.js", "dist/nodes/QuestDb/QuestDb.node.js",
"dist/nodes/ReadBinaryFile.node.js", "dist/nodes/ReadBinaryFile.node.js",
"dist/nodes/ReadBinaryFiles.node.js", "dist/nodes/ReadBinaryFiles.node.js",