homebridge-homewizard-energy-socket
Version:
This Homebridge plugin exposes your HomeWizard Energy Sockets to Apple HomeKit. So you can use the Home App to switch your Energy Sockets on or off and integrate the Energy Sockets into your Home Automations.
158 lines • 7.1 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HomeWizardApi = exports.HomeWizardApiResponseError = exports.HomeWizardApiError = void 0;
const types_1 = require("./types");
const undici_1 = require("undici");
const request = (...args) => (0, undici_1.request)(args[0], Object.assign(Object.assign({}, args[1]), { bodyTimeout: 1000, headersTimeout: 1000 }));
class HomeWizardApiError extends Error {
constructor(message) {
super(message);
this.name = 'HomeWizardApiError';
}
}
exports.HomeWizardApiError = HomeWizardApiError;
class HomeWizardApiResponseError extends HomeWizardApiError {
constructor(message, url, statusCode, response) {
super(message);
this.name = 'HomeWizardApiResponseError';
this.url = url;
this.statusCode = statusCode;
this.response = response;
}
}
exports.HomeWizardApiResponseError = HomeWizardApiResponseError;
class HomeWizardApi {
constructor(url, options) {
this.log = options.logger;
this.url = url;
this.apiVersion = options.apiVersion || 'v1';
}
get endpoints() {
const { url } = this;
return {
basic: `${url}/api`,
state: `${url}/api/${this.apiVersion}/state`,
identify: `${url}/api/${this.apiVersion}/identify`,
data: `${url}/api/${this.apiVersion}/data`,
};
}
get loggerPrefix() {
return `[Api] -> ${this.url} -> `;
}
isResponseOk(response) {
return response.statusCode >= 200 && response.statusCode < 300;
}
throwApiResponseError(url, method, response) {
return __awaiter(this, void 0, void 0, function* () {
const { statusCode, body } = response;
const text = yield body.text();
throw new HomeWizardApiResponseError(`Api ${method} call at ${url} failed with status ${statusCode} and response data: ${text}`, url, statusCode, text);
});
}
getBasicInformation() {
return __awaiter(this, void 0, void 0, function* () {
const url = this.endpoints.basic;
this.log.debug(this.loggerPrefix, `Fetching the basic information at ${url}`);
const method = 'GET';
const response = yield request(url, {
method,
});
if (!this.isResponseOk(response)) {
return this.throwApiResponseError(url, method, response);
}
const data = (yield response.body.json());
this.log.debug(this.loggerPrefix, `Fetched basic information: ${JSON.stringify(data)}`);
return data;
});
}
getState() {
return __awaiter(this, void 0, void 0, function* () {
const url = this.endpoints.state;
this.log.debug(this.loggerPrefix, `Fetching the state at ${url}`);
const method = 'GET';
const response = yield request(url, {
method,
});
if (!this.isResponseOk(response)) {
return this.throwApiResponseError(url, method, response);
}
const data = (yield response.body.json());
this.log.debug(this.loggerPrefix, `Received state ${JSON.stringify(data)} from ${this.endpoints.state}`);
return data;
});
}
putState(params) {
return __awaiter(this, void 0, void 0, function* () {
const url = this.endpoints.state;
this.log.debug(this.loggerPrefix, `Setting the state to ${JSON.stringify(params)} at ${this.endpoints.state}`);
const method = 'PUT';
const response = yield request(this.endpoints.state, {
method,
body: JSON.stringify(params),
});
if (!this.isResponseOk(response)) {
return this.throwApiResponseError(url, method, response);
}
const data = (yield response.body.json());
this.log.debug(this.loggerPrefix, `Received updated state ${JSON.stringify(data)} from ${this.endpoints.state}`);
return data;
});
}
putIdentify(firmwareVersion) {
return __awaiter(this, void 0, void 0, function* () {
if (!firmwareVersion) {
throw new HomeWizardApiError('Cannot identify this Energy Socket. The firmware version is not set.');
}
if (firmwareVersion < 3) {
throw new HomeWizardApiError(`Cannot identify this Energy Socket. Firmware version is ${firmwareVersion}. But the identify feature is only available on Energy Sockets with firmware version 3.00 or later.`);
}
const url = this.endpoints.identify;
this.log.debug(this.loggerPrefix, `Fetching identify at ${url}`);
const method = 'PUT';
const response = yield request(url, {
method,
});
if (!this.isResponseOk(response)) {
return this.throwApiResponseError(url, method, response);
}
const data = (yield response.body.json());
this.log.debug(this.loggerPrefix, `Energy Socket identified: ${JSON.stringify(data)}`);
return data;
});
}
getData(productType, disableLogs) {
return __awaiter(this, void 0, void 0, function* () {
if (productType !== types_1.HomeWizardDeviceTypes.WIFI_PI_METER &&
productType !== types_1.HomeWizardDeviceTypes.WIFI_ENERGY_SOCKET) {
throw new HomeWizardApiError(`Product type "${productType}" is not supported for this API call.`);
}
const url = this.endpoints.data;
if (!disableLogs) {
this.log.debug(this.loggerPrefix, `Fetching the data at ${url}`);
}
const method = 'GET';
const response = yield request(url, {
method,
});
if (!this.isResponseOk(response)) {
return this.throwApiResponseError(url, method, response);
}
const data = (yield response.body.json());
if (!disableLogs) {
this.log.debug(this.loggerPrefix, `Fetched data: ${JSON.stringify(data)}`);
}
return data;
});
}
}
exports.HomeWizardApi = HomeWizardApi;
//# sourceMappingURL=api.js.map