🔀 Merge branch 'ha-load-options' of https://github.com/pemontto/n8n into pemontto-ha-load-options

This commit is contained in:
Jan Oberhauser 2021-12-11 17:54:20 +01:00
commit 6bc5a0eb42
5 changed files with 95 additions and 8 deletions

View file

@ -33,7 +33,10 @@ export const cameraProxyFields: INodeProperties[] = [
{ {
displayName: 'Camera Entity ID', displayName: 'Camera Entity ID',
name: 'cameraEntityId', name: 'cameraEntityId',
type: 'string', type: 'options',
typeOptions: {
loadOptionsMethod: 'getCameraEntities',
},
default: '', default: '',
required: true, required: true,
displayOptions: { displayOptions: {

View file

@ -4,15 +4,17 @@ import {
import { import {
IExecuteFunctions, IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject, IDataObject,
INodePropertyOptions,
NodeApiError, NodeApiError,
NodeOperationError, NodeOperationError,
} from 'n8n-workflow'; } from 'n8n-workflow';
export async function homeAssistantApiRequest(this: IExecuteFunctions, method: string, resource: string, body: IDataObject = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}) { export async function homeAssistantApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, resource: string, body: IDataObject = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}) {
const credentials = await this.getCredentials('homeAssistantApi'); const credentials = await this.getCredentials('homeAssistantApi');
if (credentials === undefined) { if (credentials === undefined) {
@ -35,8 +37,51 @@ export async function homeAssistantApiRequest(this: IExecuteFunctions, method: s
delete options.body; delete options.body;
} }
try { try {
if (this.helpers.request) {
return await this.helpers.request(options); return await this.helpers.request(options);
}
} catch (error) { } catch (error) {
throw new NodeApiError(this.getNode(), error); throw new NodeApiError(this.getNode(), error);
} }
} }
export async function getHomeAssistantEntities(this: IExecuteFunctions | ILoadOptionsFunctions, domain = '') {
const returnData: INodePropertyOptions[] = [];
const entities = await homeAssistantApiRequest.call(this, 'GET', '/states');
for (const entity of entities) {
const entityId = entity.entity_id as string;
if (domain === '' || domain && entityId.startsWith(domain)) {
const entityName = entity.attributes.friendly_name as string || entityId;
returnData.push({
name: entityName,
value: entityId,
});
}
}
return returnData;
}
export async function getHomeAssistantServices(this: IExecuteFunctions | ILoadOptionsFunctions, domain = '') {
const returnData: INodePropertyOptions[] = [];
const services = await homeAssistantApiRequest.call(this, 'GET', '/services');
if (domain === '') {
// If no domain specified return domains
const domains = services.map(({ domain }: IDataObject) => domain as string).sort();
returnData.push(...domains.map((service: string) => ({ name: service, value: service })));
return returnData;
} else {
// If we have a domain, return all relevant services
const domainServices = services.filter((service: IDataObject) => service.domain === domain);
for (const domainService of domainServices) {
for (const [serviceID, value] of Object.entries(domainService.services)) {
const serviceProperties = value as IDataObject;
const serviceName = serviceProperties.description || serviceID;
returnData.push({
name: serviceName as string,
value: serviceID,
});
}
}
}
return returnData;
}

View file

@ -6,7 +6,9 @@ import {
ICredentialsDecrypted, ICredentialsDecrypted,
ICredentialTestFunctions, ICredentialTestFunctions,
IDataObject, IDataObject,
ILoadOptionsFunctions,
INodeExecutionData, INodeExecutionData,
INodePropertyOptions,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
NodeCredentialTestResult, NodeCredentialTestResult,
@ -52,6 +54,8 @@ import {
} from './CameraProxyDescription'; } from './CameraProxyDescription';
import { import {
getHomeAssistantEntities,
getHomeAssistantServices,
homeAssistantApiRequest, homeAssistantApiRequest,
} from './GenericFunctions'; } from './GenericFunctions';
@ -169,9 +173,29 @@ export class HomeAssistant implements INodeType {
status: 'OK', status: 'OK',
message: 'Authentication successful!', message: 'Authentication successful!',
}; };
},
},
loadOptions: {
async getAllEntities(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
return await getHomeAssistantEntities.call(this);
},
async getCameraEntities(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
return await getHomeAssistantEntities.call(this, 'camera');
},
async getDomains(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
return await getHomeAssistantServices.call(this);
},
async getDomainServices(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const currentDomain = this.getCurrentNodeParameter('domain') as string;
if (currentDomain) {
return await getHomeAssistantServices.call(this, currentDomain);
} else {
return [];
}
}, },
}, },
}; };
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {

View file

@ -83,7 +83,10 @@ export const serviceFields: INodeProperties[] = [
{ {
displayName: 'Domain', displayName: 'Domain',
name: 'domain', name: 'domain',
type: 'string', type: 'options',
typeOptions: {
loadOptionsMethod: 'getDomains',
},
default: '', default: '',
required: true, required: true,
displayOptions: { displayOptions: {
@ -100,7 +103,13 @@ export const serviceFields: INodeProperties[] = [
{ {
displayName: 'Service', displayName: 'Service',
name: 'service', name: 'service',
type: 'string', type: 'options',
typeOptions: {
loadOptionsDependsOn: [
'domain',
],
loadOptionsMethod: 'getDomainServices',
},
default: '', default: '',
required: true, required: true,
displayOptions: { displayOptions: {

View file

@ -43,7 +43,10 @@ export const stateFields: INodeProperties[] = [
{ {
displayName: 'Entity ID', displayName: 'Entity ID',
name: 'entityId', name: 'entityId',
type: 'string', type: 'options',
typeOptions: {
loadOptionsMethod: 'getAllEntities',
},
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
@ -110,7 +113,10 @@ export const stateFields: INodeProperties[] = [
{ {
displayName: 'Entity ID', displayName: 'Entity ID',
name: 'entityId', name: 'entityId',
type: 'string', type: 'options',
typeOptions: {
loadOptionsMethod: 'getAllEntities',
},
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [