2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-07-10 04:44:23 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2021-07-10 04:44:23 -07:00
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, INodePropertyOptions, NodeApiError } from 'n8n-workflow';
|
2021-07-10 04:44:23 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function homeAssistantApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
) {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('homeAssistantApi');
|
2021-07-10 04:44:23 -07:00
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${credentials.accessToken}`,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2022-08-17 08:50:24 -07:00
|
|
|
uri:
|
|
|
|
uri ??
|
|
|
|
`${credentials.ssl === true ? 'https' : 'http'}://${credentials.host}:${
|
|
|
|
credentials.port
|
|
|
|
}/api${resource}`,
|
2021-07-10 04:44:23 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
if (Object.keys(options.body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
try {
|
2021-10-17 09:48:24 -07:00
|
|
|
if (this.helpers.request) {
|
|
|
|
return await this.helpers.request(options);
|
|
|
|
}
|
2021-07-10 04:44:23 -07:00
|
|
|
} catch (error) {
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
|
|
|
}
|
|
|
|
}
|
2021-10-17 09:48:24 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getHomeAssistantEntities(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
domain = '',
|
|
|
|
) {
|
2021-10-17 09:48:24 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const entities = await homeAssistantApiRequest.call(this, 'GET', '/states');
|
|
|
|
for (const entity of entities) {
|
|
|
|
const entityId = entity.entity_id as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
if (domain === '' || (domain && entityId.startsWith(domain))) {
|
|
|
|
const entityName = (entity.attributes.friendly_name as string) || entityId;
|
2021-10-17 09:48:24 -07:00
|
|
|
returnData.push({
|
|
|
|
name: entityName,
|
|
|
|
value: entityId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getHomeAssistantServices(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
domain = '',
|
|
|
|
) {
|
2021-10-17 09:48:24 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const services = await homeAssistantApiRequest.call(this, 'GET', '/services');
|
|
|
|
if (domain === '') {
|
|
|
|
// If no domain specified return domains
|
2022-12-02 12:54:28 -08:00
|
|
|
const domains = services.map(({ domain: service }: IDataObject) => service as string).sort();
|
2021-10-17 09:48:24 -07:00
|
|
|
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;
|
2023-01-19 04:37:19 -08:00
|
|
|
const serviceName = serviceProperties.description || serviceID;
|
2021-10-17 09:48:24 -07:00
|
|
|
returnData.push({
|
|
|
|
name: serviceName as string,
|
|
|
|
value: serviceID,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
}
|