2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-02-27 19:39:43 -08:00
|
|
|
IDataObject,
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2020-07-31 13:26:23 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeExecutionData,
|
2020-07-31 13:26:23 -07:00
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2020-07-31 13:26:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { getUser, philipsHueApiRequest } from './GenericFunctions';
|
2020-07-31 13:26:23 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { lightFields, lightOperations } from './LightDescription';
|
2020-07-31 13:26:23 -07:00
|
|
|
|
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',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-08-03 01:06:46 -07:00
|
|
|
icon: 'file:philipshue.png',
|
2020-07-31 13:26:23 -07:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
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',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-07-31 13:26:23 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Light',
|
|
|
|
value: 'light',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'light',
|
|
|
|
},
|
|
|
|
...lightOperations,
|
|
|
|
...lightFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the lights to display them to user so that they can
|
2020-07-31 13:26:23 -07:00
|
|
|
// select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getLights(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-07-31 13:26:23 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
|
|
|
|
const user = await getUser.call(this);
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const lights = await philipsHueApiRequest.call(this, 'GET', `/api/${user}/lights`);
|
2021-12-23 04:30:35 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const groups = await philipsHueApiRequest.call(this, 'GET', `/api/${user}/groups`);
|
2021-01-06 22:58:08 -08:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
for (const light of Object.keys(lights as IDataObject)) {
|
2021-01-06 22:58:08 -08:00
|
|
|
let lightName = lights[light].name;
|
2020-07-31 13:26:23 -07:00
|
|
|
const lightId = light;
|
2021-12-23 04:30:35 -08:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
for (const groupId of Object.keys(groups as IDataObject)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (groups[groupId].type === 'Room' && groups[groupId].lights.includes(lightId)) {
|
2021-01-06 22:58:08 -08:00
|
|
|
lightName = `${groups[groupId].name}: ${lightName}`;
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 04:30:35 -08:00
|
|
|
|
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();
|
2023-01-31 11:39:20 -08:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-07-31 13:26:23 -07:00
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-07-31 13:26:23 -07:00
|
|
|
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;
|
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-07-31 13:26:23 -07:00
|
|
|
|
|
|
|
const body = {
|
|
|
|
on,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (additionalFields.transitiontime) {
|
2022-08-17 08:50:24 -07:00
|
|
|
additionalFields.transitiontime = (additionalFields.transitiontime as number) * 100;
|
2020-07-31 13:26:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.xy) {
|
2022-08-17 08:50:24 -07:00
|
|
|
additionalFields.xy = (additionalFields.xy as string)
|
|
|
|
.split(',')
|
|
|
|
.map((e: string) => parseFloat(e));
|
2020-07-31 13:26:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.xy_inc) {
|
2022-08-17 08:50:24 -07:00
|
|
|
additionalFields.xy_inc = (additionalFields.xy_inc as string)
|
|
|
|
.split(',')
|
|
|
|
.map((e: string) => parseFloat(e));
|
2020-07-31 13:26:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2022-01-12 22:51:58 -08:00
|
|
|
`/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);
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await philipsHueApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/api/${user}/lights/${lightId}`,
|
|
|
|
);
|
2020-07-31 13:26:23 -07:00
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-07-31 13:26:23 -07:00
|
|
|
|
|
|
|
const user = await getUser.call(this);
|
|
|
|
|
2022-01-12 22:51:58 -08:00
|
|
|
const lights = await philipsHueApiRequest.call(this, 'GET', `/api/${user}/lights`);
|
2020-07-31 13:26:23 -07:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = Object.values(lights as IDataObject);
|
2020-07-31 13:26:23 -07:00
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2020-07-31 13:26:23 -07:00
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
const lightId = this.getNodeParameter('lightId', i) as string;
|
|
|
|
|
|
|
|
const user = await getUser.call(this);
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await philipsHueApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/api/${user}/lights/${lightId}`,
|
|
|
|
);
|
2020-07-31 13:26:23 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2023-01-31 11:39:20 -08:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2020-07-31 13:26:23 -07:00
|
|
|
}
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2020-07-31 13:26:23 -07:00
|
|
|
}
|
|
|
|
}
|