n8n/packages/nodes-base/nodes/PhilipsHue/PhilipsHue.node.ts

198 lines
4.8 KiB
TypeScript
Raw Normal View History

2020-07-31 13:26:23 -07:00
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
2020-07-31 13:26:23 -07:00
INodePropertyOptions,
INodeType,
INodeTypeDescription,
2020-07-31 13:26:23 -07:00
} from 'n8n-workflow';
import {
getUser,
philipsHueApiRequest,
2020-07-31 13:26:23 -07:00
} from './GenericFunctions';
import {
lightFields,
lightOperations,
2020-07-31 13:26:23 -07:00
} from './LightDescription';
2020-08-03 01:06:46 -07:00
export class PhilipsHue implements INodeType {
2020-07-31 13:26:23 -07:00
description: INodeTypeDescription = {
2020-08-03 01:06:46 -07:00
displayName: 'Philips Hue',
name: 'philipsHue',
icon: 'file:philipshue.png',
2020-07-31 13:26:23 -07:00
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Philips Hue API',
2020-07-31 13:26:23 -07:00
defaults: {
2020-08-03 01:06:46 -07:00
name: 'Philips Hue',
2020-07-31 13:26:23 -07:00
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
2020-08-03 01:06:46 -07:00
name: 'philipsHueOAuth2Api',
2020-07-31 13:26:23 -07:00
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Light',
value: 'light',
},
],
default: 'light',
description: 'The resource to operate on.',
},
...lightOperations,
...lightFields,
],
};
methods = {
loadOptions: {
// Get all the lights to display them to user so that he can
// select them easily
async getLights(
2020-10-22 09:00:28 -07:00
this: ILoadOptionsFunctions,
2020-07-31 13:26:23 -07:00
): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const user = await getUser.call(this);
2020-08-03 01:06:46 -07:00
const lights = await philipsHueApiRequest.call(
2020-07-31 13:26:23 -07:00
this,
'GET',
`/api/${user}/lights`,
2020-07-31 13:26:23 -07:00
);
const groups = await philipsHueApiRequest.call(
this,
'GET',
`/api/${user}/groups`,
);
2020-07-31 13:26:23 -07:00
for (const light of Object.keys(lights)) {
let lightName = lights[light].name;
2020-07-31 13:26:23 -07:00
const lightId = light;
for (const groupId of Object.keys(groups)) {
if(groups[groupId].type === 'Room' && groups[groupId].lights.includes(lightId)) {
lightName = `${groups[groupId].name}: ${lightName}`;
}
}
2020-07-31 13:26:23 -07:00
returnData.push({
name: lightName,
2020-10-22 06:46:03 -07:00
value: lightId,
2020-07-31 13:26:23 -07:00
});
}
return returnData;
},
2020-10-22 06:46:03 -07:00
},
2020-07-31 13:26:23 -07:00
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = (items.length as unknown) as number;
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
if (resource === 'light') {
if (operation === 'update') {
const lightId = this.getNodeParameter('lightId', i) as string;
const on = this.getNodeParameter('on', i) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body = {
on,
};
if (additionalFields.transitiontime) {
additionalFields.transitiontime = (additionalFields.transitiontime as number * 100);
}
if (additionalFields.xy) {
additionalFields.xy = (additionalFields.xy as string).split(',').map((e: string) => parseFloat(e));
}
if (additionalFields.xy_inc) {
additionalFields.xy_inc = (additionalFields.xy_inc as string).split(',').map((e: string) => parseFloat(e));
}
Object.assign(body, additionalFields);
const user = await getUser.call(this);
2020-08-03 01:06:46 -07:00
const data = await philipsHueApiRequest.call(
2020-07-31 13:26:23 -07:00
this,
'PUT',
`/api/${user}/lights/${lightId}/state`,
2020-10-22 09:00:28 -07:00
body,
2020-07-31 13:26:23 -07:00
);
responseData = {};
for (const response of data) {
Object.assign(responseData, response.success);
}
}
if (operation === 'delete') {
const lightId = this.getNodeParameter('lightId', i) as string;
const user = await getUser.call(this);
responseData = await philipsHueApiRequest.call(this, 'DELETE', `/api/${user}/lights/${lightId}`);
2020-07-31 13:26:23 -07:00
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const user = await getUser.call(this);
const lights = await philipsHueApiRequest.call(this, 'GET', `/api/${user}/lights`);
2020-07-31 13:26:23 -07:00
responseData = Object.values(lights);
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
responseData = responseData.splice(0, limit);
}
}
if (operation === 'get') {
const lightId = this.getNodeParameter('lightId', i) as string;
const user = await getUser.call(this);
responseData = await philipsHueApiRequest.call(this, 'GET', `/api/${user}/lights/${lightId}`);
2020-07-31 13:26:23 -07:00
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
}
return [this.helpers.returnJsonArray(returnData)];
}
}