@tantchen/fritzapi
Version:
Home automation node API for Fritz!Box, Fritz!DECT and FRITZ!Powerline devices
1,117 lines (1,112 loc) • 45.4 kB
JavaScript
"use strict";
/**
* smartFritz - Fritz goes smartHome
*
* AVM SmartHome nodeJS Control - for AVM Fritz!Box and Dect200 Devices
*
* @author Andreas Goetz <cpuidle@gmx.de>
*
* Forked from: https://github.com/nischelwitzer/smartfritz
* nischi - first version: July 2014
*
* based on: Node2Fritz by steffen.timm on 05.12.13 for Fritz!OS > 05.50
* and thk4711 (code https://github.com/pimatic/pimatic/issues/38)
*
* AVM Documentation is at https://avm.de/service/schnittstellen/
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
* Object-oriented API
*/
var axios_1 = __importDefault(require("axios"));
var crypto_1 = __importDefault(require("crypto"));
var fast_xml_parser_1 = require("fast-xml-parser");
var qs = __importStar(require("qs"));
var utils_1 = require("./utils");
var Fritz = /** @class */ (function () {
/**
* Default constructor
* @param username
* @param password
* @param url if not set default is 'http://fritz.box'
*/
function Fritz(username, password, url) {
var _this = this;
this.url = utils_1.defaultUrl;
/**
* get template information (json)
*/
this.getTemplateList = function () { return __awaiter(_this, void 0, void 0, function () {
var templateinfo, options, parser, templates, out;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getTemplateListInfos()];
case 1:
templateinfo = _a.sent();
options = {
ignoreAttributes: false,
attributeNamePrefix: '',
};
parser = new fast_xml_parser_1.XMLParser(options);
templates = parser.parse(templateinfo);
out = [];
out = out.concat((templates.templatelist || {}).template || []);
out = out.map(function (template) {
return template;
});
return [2 /*return*/, out];
}
});
}); };
/**
* get switch state
* @param ain
*/
this.getSwitchState = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'getswitchstate', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, /^1/.test(body)]; // true if on
}
});
}); };
/**
* turn an outlet on. returns the state the outlet was set to
* @param ain
*/
this.setSwitchOn = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'setswitchon', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, /^1/.test(body)]; // true if on
}
});
}); };
/**
* turn an outlet off. returns the state the outlet was set to
* @param ain
*/
this.setSwitchOff = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'setswitchoff', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, /^1/.test(body)]; // false if off
}
});
}); };
/**
* toggle an outlet. returns the state the outlet was set to
* @param ain
*/
this.setSwitchToggle = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'setswitchtoggle', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, /^1/.test(body)]; // false if off
}
});
}); };
/**
* get the total enery consumption. returns the value in Wh
* @param ain
*/
this.getSwitchEnergy = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'getswitchenergy', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, parseFloat(body)]; // Wh
}
});
}); };
/**
* get the current enery consumption of an outlet. returns the value in mW
* @param ain
*/
this.getSwitchPower = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body, power;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'getswitchpower', ain)];
case 1:
body = _a.sent();
power = parseFloat(body);
return [2 /*return*/, Number.isNaN(power) ? null : power / 1000]; // W
}
});
}); };
/**
* get the outet presence status
* @param ain
*/
this.getSwitchPresence = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'getswitchpresent', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, /^1/.test(body)]; // true if present
}
});
}); };
/**
* get switch name
* @param ain
*/
this.getSwitchName = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'getswitchname', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, body.trim()];
}
});
}); };
/*
* Thermostats
*/
/**
* get the thermostat list
*/
this.getThermostatList = function () { return __awaiter(_this, void 0, void 0, function () {
var devices;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getDeviceListFiltered({
functionbitmask: utils_1.FUNCTION_THERMOSTAT,
})];
case 1:
devices = _a.sent();
return [2 /*return*/, devices.map(function (device) {
return device.identifier;
})];
}
});
}); };
/**
* get target temperature (Solltemperatur)
* @param ain
*/
this.getTempTarget = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'gethkrtsoll', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, (0, utils_1.api2temp)(body)];
}
});
}); };
/**
* get night temperature (Absenktemperatur)
* @param ain
*/
this.getTempNight = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'gethkrabsenk', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, (0, utils_1.api2temp)(body)];
}
});
}); };
/**
* get comfort temperature (Komforttemperatur)
* @param ain
*/
this.getTempComfort = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'gethkrkomfort', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, (0, utils_1.api2temp)(body)];
}
});
}); };
/**
* ------------------------------------------------
*/
/*
* AVM Buttons Fritz!DECT 400 and Fritz!DECT 440
* Querying a button isn't really useful because they don't have a state to query,
* there is just a timestamp of the last short and long button press.
* The only useful information is the battery status returned in the 'battery' and
* 'batterylow' property.
* The Fritz!DECT 440 should have an additional 'temperature' property
*/
/**
* get a list of all button devices
*/
this.getButtonList = function () { return __awaiter(_this, void 0, void 0, function () {
var devices;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getDeviceListFiltered({
functionbitmask: utils_1.FUNCTION_BUTTON,
})];
case 1:
devices = _a.sent();
return [2 /*return*/, devices.map(function (device) {
return device.identifier;
})];
}
});
}); };
/*
* Light bulbs (HAN-FUN)
*/
/**
* get a list of all bulbs
*/
this.getBulbList = function () { return __awaiter(_this, void 0, void 0, function () {
var devices;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getDeviceListFiltered({
functionbitmask: utils_1.FUNCTION_LIGHT,
})];
case 1:
devices = _a.sent();
return [2 /*return*/, devices.map(function (device) {
return device.identifier;
})];
}
});
}); };
/**
* get a list of bulbs which support colors
*/
this.getColorBulbList = function () { return __awaiter(_this, void 0, void 0, function () {
var devices;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getDeviceListFiltered({
functionbitmask: utils_1.FUNCTION_LIGHT || utils_1.FUNCTION_COLORCONTROL,
})];
case 1:
devices = _a.sent();
return [2 /*return*/, devices.map(function (device) {
return device.identifier;
})];
}
});
}); };
/**
* get battery charge
* Attention: this function queries the whole device list to get the value for one device.
* If multiple device will be queried for the battery status, a better approach would be to
* get the device list once and then filter out the devices of interest.
* @param ain
*/
this.getBatteryCharge = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var device;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getDevice(ain)];
case 1:
device = _a.sent();
return [2 /*return*/, device === null || device === void 0 ? void 0 : device.battery];
}
});
}); };
/**
* Get the window open flag of a thermostat
* Attention: this function queries the whole device list to get the value for one device.
* If multiple device will be queried for the window open status, a better approach would
* be to get the device list once and then filter out the devices of interest.
* @param ain
*/
this.getWindowOpen = function (ain) { return __awaiter(_this, void 0, void 0, function () {
var device;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.getDevice(ain)];
case 1:
device = _b.sent();
return [2 /*return*/, ((_a = device === null || device === void 0 ? void 0 : device.hkr) === null || _a === void 0 ? void 0 : _a.windowopenactiv) === '1'];
}
});
}); };
this.sid = null;
this.username = username;
this.password = password;
this.deviceList = null;
if (url) {
this.url = url;
}
}
/**
* get SID
*/
Fritz.prototype.getSID = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!(this.sid === null)) return [3 /*break*/, 2];
_a = this;
return [4 /*yield*/, this.getSessionID(this.username, this.password)];
case 1:
_a.sid = _b.sent();
_b.label = 2;
case 2:
if (this.sid === null) {
throw new Error('Bad SID');
}
return [2 /*return*/, this.sid];
}
});
});
};
/**
* Request helper
* @param request
* @param options
* @private
*/
Fritz.httpRequest = function (request, options) {
return __awaiter(this, void 0, void 0, function () {
var res, form, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 7, , 8]);
res = void 0;
if (!((options === null || options === void 0 ? void 0 : options.method) === 'POST')) return [3 /*break*/, 4];
if (!options.form) return [3 /*break*/, 2];
form = qs.stringify(options.form);
return [4 /*yield*/, axios_1.default.post(request, form, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})];
case 1:
res = _a.sent();
return [3 /*break*/, 3];
case 2: throw new Error('no form data');
case 3: return [3 /*break*/, 6];
case 4: return [4 /*yield*/, axios_1.default.get(request)];
case 5:
res = _a.sent();
_a.label = 6;
case 6: return [2 /*return*/, res.data];
case 7:
e_1 = _a.sent();
console.error(e_1);
throw new Error('httpRequest Failed');
case 8: return [2 /*return*/];
}
});
});
};
/**
* Execute Fritz API command for device specified by AIN
*/
Fritz.prototype.executeCommand = function (sid, command, ain, path) {
var param = [];
for (var _i = 4; _i < arguments.length; _i++) {
param[_i - 4] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var ePath, eParam, _a, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
ePath = [];
eParam = [];
ePath.push(this.url);
if (path) {
ePath.push(path);
}
else {
ePath.push('/webservices/homeautoswitch.lua');
eParam.push('0=0');
}
param.forEach(function (el) {
eParam.push(el);
});
if (!sid) return [3 /*break*/, 2];
_b = (_a = eParam).push;
_c = "sid=".concat;
return [4 /*yield*/, this.getSID()];
case 1:
_b.apply(_a, [_c.apply("sid=", [_d.sent()])]);
_d.label = 2;
case 2:
if (command)
eParam.push("switchcmd=".concat(command));
if (ain)
eParam.push("ain=".concat(ain));
if (eParam.length > 0) {
ePath.push('?');
ePath.push(eParam.join('&'));
}
return [2 /*return*/, Fritz.httpRequest(ePath.join(''))];
}
});
});
};
/**
* get session id
* @param username
* @param password
* @private
*/
Fritz.prototype.getSessionID = function (username, password) {
return __awaiter(this, void 0, void 0, function () {
var body, challenge, challengeResponse, url, sessionID;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(false, null, null, '/login_sid.lua')];
case 1:
body = _a.sent();
challenge = body.match('<Challenge>(.*?)</Challenge>')[1];
challengeResponse = "".concat(challenge, "-").concat(crypto_1.default
.createHash('md5')
.update(Buffer.from("".concat(challenge, "-").concat(password), 'utf16le'))
.digest('hex'));
url = "/login_sid.lua?username=".concat(username, "&response=").concat(challengeResponse);
return [4 /*yield*/, this.executeCommand(false, null, null, url)];
case 2:
body = _a.sent();
sessionID = body.match('<SID>(.*?)</SID>')[1];
return [2 /*return*/, sessionID];
}
});
});
};
/**
* check if session id is OK
*/
Fritz.prototype.checkSession = function () {
return __awaiter(this, void 0, void 0, function () {
var body, sessionID;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, null, null, '/login_sid.lua')];
case 1:
body = _a.sent();
sessionID = body.match('<SID>(.*?)</SID>');
return [2 /*return*/, !!sessionID && sessionID[1] !== '0000000000000000'];
}
});
});
};
/**
* get OS version
*/
Fritz.prototype.getOSVersion = function () {
return __awaiter(this, void 0, void 0, function () {
var req, json, osVersion;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = {
method: 'POST'
};
_b = {};
return [4 /*yield*/, this.getSID()];
case 1:
req = (_a.form = (_b.sid = _c.sent(),
_b.xhr = 1,
_b.page = 'overview',
_b),
_a);
return [4 /*yield*/, Fritz.httpRequest("".concat(this.url, "/data.lua"), req)];
case 2:
json = _c.sent();
osVersion = json.data && json.data.fritzos && json.data.fritzos.nspver
? json.data.fritzos.nspver
: null;
return [2 /*return*/, osVersion];
}
});
});
};
/**
* get full FritzBox config
*/
Fritz.prototype.getDataSheet = function () {
return __awaiter(this, void 0, void 0, function () {
var req;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = {
method: 'POST'
};
_b = {};
return [4 /*yield*/, this.getSID()];
case 1:
req = (_a.form = (_b.sid = _c.sent(),
_b.xhr = 1,
_b.page = 'overview',
_b),
_a);
return [2 /*return*/, Fritz.httpRequest("".concat(this.url, "/data.lua"), req)];
}
});
});
};
/**
* get template information (XML)
*/
Fritz.prototype.getTemplateListInfos = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.executeCommand(true, 'gettemplatelistinfos', null)];
});
});
};
/**
* apply template
* @param ain
*/
Fritz.prototype.applyTemplate = function (ain) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.executeCommand(true, 'applytemplate', ain)];
});
});
};
/**
* get basic device info (XML)
* @param ain
*/
Fritz.prototype.getBasicDeviceStats = function (ain) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.executeCommand(true, 'getbasicdevicestats', ain)];
});
});
};
/**
* get detailed device information (XML)
*/
Fritz.prototype.getDeviceListInfos = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.executeCommand(true, 'getdevicelistinfos', null)];
});
});
};
/**
* get device list
*/
Fritz.prototype.getDeviceList = function () {
return __awaiter(this, void 0, void 0, function () {
var devicelistinfo, options, parser, devicesCore, devices;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getDeviceListInfos()];
case 1:
devicelistinfo = _a.sent();
options = {
ignoreAttributes: false,
attributeNamePrefix: '',
};
parser = new fast_xml_parser_1.XMLParser(options);
devicesCore = parser.parse(devicelistinfo);
devices = [];
devices = devices.concat((devicesCore.devicelist || {}).device || []);
devices = devices.map(function (dev) {
var newElement = dev;
newElement.identifier = newElement.identifier.replace(/\s/g, '');
return newElement;
});
return [2 /*return*/, devices];
}
});
});
};
/**
* get device list by filter criteria
* @param filter
*/
Fritz.prototype.getDeviceListFiltered = function (filter) {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!!this.deviceList) return [3 /*break*/, 2];
_a = this;
return [4 /*yield*/, this.getDeviceList()];
case 1:
_a.deviceList = _b.sent();
_b.label = 2;
case 2: return [2 /*return*/, this.deviceList.filter(function (device) {
return Object.keys(filter).every(function (key) {
/* jshint laxbreak:true */
return key === 'functionbitmask'
? device.functionbitmask & filter[key]
: device[key] === filter[key];
});
})];
}
});
});
};
/**
* get single device
* @param ain
*/
Fritz.prototype.getDevice = function (ain) {
return __awaiter(this, void 0, void 0, function () {
var devices;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getDeviceListFiltered({
identifier: ain,
})];
case 1:
devices = _a.sent();
return [2 /*return*/, devices.length ? devices[0] : null];
}
});
});
};
/**
* get temperature- both switches and thermostats are supported, but not powerline modules
* @param ain
*/
Fritz.prototype.getTemperature = function (ain) {
return __awaiter(this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'gettemperature', ain)];
case 1:
body = _a.sent();
return [2 /*return*/, parseFloat(body) / 10]; // °C
}
});
});
};
/**
* get presence from deviceListInfo
* @param ain
*/
Fritz.prototype.getPresence = function (ain) {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.getDevice(ain)];
case 1: return [2 /*return*/, !!((_a = (_b.sent())) === null || _a === void 0 ? void 0 : _a.presence)];
}
});
});
};
/*
* Switches
*/
/**
* get switch list
*/
Fritz.prototype.getSwitchList = function () {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, 'getswitchlist', null)];
case 1:
res = _a.sent();
res = res.replace('\n', '');
return [2 /*return*/, res === '' ? [] : res.split(',')];
}
});
});
};
/**
* set target temperature (Solltemperatur)
* @param ain
* @param temp
*/
Fritz.prototype.setTempTarget = function (ain, temp) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, "sethkrtsoll¶m=".concat((0, utils_1.temp2api)(temp)), ain)];
case 1:
_a.sent();
return [2 /*return*/, temp];
}
});
});
};
/**
* ------------------------------------------------
*/
/**
* Not yet tested - deactivated for now
*/
//
/**
* activate boost with end time or deactivate boost
* @param ain
* @param endtime
*/
Fritz.prototype.setHkrBoost = function (ain, endtime) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, "sethkrboost&endtimestamp=".concat((0, utils_1.time2api)(endtime)), ain)];
case 1:
_a.sent();
return [2 /*return*/, endtime];
}
});
});
};
/**
* activate window open with end time or deactivate boost
* @param ain
* @param endtime
*/
Fritz.prototype.setHkrWindowOpen = function (ain, endtime) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, "sethkrwindowopen&endtimestamp=".concat((0, utils_1.time2api)(endtime)), ain)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* activate window open with end time or deactivate boost
* @param deviceId
* @param offset
*/
Fritz.prototype.setHkrOffset = function (deviceId, offset) {
return __awaiter(this, void 0, void 0, function () {
var path, req;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
path = '/net/home_auto_hkr_edit.lua';
_a = {
method: 'POST'
};
_b = {};
return [4 /*yield*/, this.getSID()];
case 1:
req = (_a.form = (_b.sid = _c.sent(),
_b.xhr = 1,
_b.lang = 'de',
_b.no_sidrenew = '',
_b.ule_device_name = '',
_b.useajax = 1,
_b.WindowOpenTimer = '',
_b.WindowOpenTrigger = '',
_b.tempsensor = '',
_b.Roomtemp = '',
_b.ExtTempsensorID = '',
_b.Offset = offset,
_b.back_to_page = 'sh_dev',
_b.view = '',
_b.graphState = 1,
_b.apply = '',
_b.device = deviceId,
_b.oldpage = '/net/home_auto_hkr_edit.lua',
_b),
_a);
return [4 /*yield*/, Fritz.httpRequest(path, req)];
case 2:
_c.sent();
return [2 /*return*/, offset];
}
});
});
};
/**
* switch the device on, of or toggle its current state
* @param ain
* @param state
*/
Fritz.prototype.setSimpleOnOff = function (ain, state) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
/**
* ain = ain.replace('-1','');
*/
return [4 /*yield*/, this.executeCommand(true, "setsimpleonoff&onoff=".concat((0, utils_1.state2api)(state)), ain)];
case 1:
/**
* ain = ain.replace('-1','');
*/
_a.sent();
return [2 /*return*/, state];
}
});
});
};
/**
* Dimm the device, allowed values are 0 - 255
* @param ain
* @param level
*/
Fritz.prototype.setLevel = function (ain, level) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, "setlevel&level=".concat((0, utils_1.level2api)(level, false)), ain)];
case 1:
_a.sent();
return [2 /*return*/, level];
}
});
});
};
/**
* Dimm the device, allowed values are 0 - 100
* @param ain
* @param levelInPercent
*/
Fritz.prototype.setLevelPercentage = function (ain, levelInPercent) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.executeCommand(true, "setlevelpercentage&level=".concat((0, utils_1.level2api)(levelInPercent, true)), ain).then(function () {
/**
* api does not return a value
*/
return levelInPercent;
})];
});
});
};
/**
* Set the color and saturation of a color bulb
* Valid color values are:
* red, orange, yellow, lime, green, turquoise, cyan,
* lightblue, blue, purple, magenta and pink
* Valid satindex values are: 0, 1 or 2
* @param ain
* @param color
* @param satindex
* @param duration
*/
Fritz.prototype.setColor = function (ain, color, satindex, duration) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, "setcolor&hue=".concat((0, utils_1.color2apihue)(color), "&saturation=").concat((0, utils_1.satindex2apisat)(color, satindex), "&duration=").concat(duration), ain)];
case 1:
_a.sent();
return [2 /*return*/, color];
}
});
});
};
/**
* Set the color temperature of a bulb.
* @param ain
* @param temperature Valid values are 2700, 3000, 3400,3800, 4200, 4700, 5300, 5900 and 6500.
* @param duration
*/
Fritz.prototype.setColorTemperature = function (ain, temperature, duration) {
return __awaiter(this, void 0, void 0, function () {
var temp;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
temp = (0, utils_1.colortemp2api)(temperature);
return [4 /*yield*/, this.executeCommand(true, "setcolortemperature&temperature=".concat(temp, "&duration=").concat(duration), ain)];
case 1:
_a.sent();
return [2 /*return*/, temp];
}
});
});
};
// get the color defaults
// This is mostly useless because they are no defaults which can be changed but
// fixed values. Only combinations returned by this api call are accepted by
// setcolor and setcolortemperature.
// module.exports.getColorDefaults = function(sid, ain, options)
// {
// return executeCommand(true, 'getcolordefaults', ain, options).then(function(body) {
// return body;
// });
// };
// ------------------------------------------------
// Not yet tested - deactivated for now
// I don't know about any blind control unit with HANFUN support, but this API call makes
// it plausible that AVM or a partner has somthing like that in the pipeline.
//
Fritz.prototype.setBlind = function (ain, blindState) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// „open“, „close“ or „stop“
return [4 /*yield*/, this.executeCommand(true, "setblind&target=".concat(blindState), ain)];
case 1:
// „open“, „close“ or „stop“
_a.sent();
return [2 /*return*/, blindState];
}
});
});
};
/*
* WLAN
*/
// Parse guest WLAN form settings
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Fritz.prototype.parseGuestWlanHTML = function (html) {
throw new Error('Not Implemented');
/*
$ = cheerio.load(html);
const form = $('form');
const settings = {};
$('input', form).each(function (i, elem) {
let val;
const name = $(elem).attr('name');
if (!name) return;
switch ($(elem).attr('type')) {
case 'checkbox':
val = $(elem).attr('checked') == 'checked';
break;
default:
val = $(elem).val();
}
settings[name] = val;
});
$('select option[selected=selected]', form).each(function (i, elem) {
const val = $(elem).val();
const name = $(elem).parent().attr('name');
settings[name] = val;
});
return settings; */
};
// get guest WLAN settings - not part of Fritz API
Fritz.prototype.getGuestWlan = function () {
return __awaiter(this, void 0, void 0, function () {
var body;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeCommand(true, null, null, '/wlan/guest_access.lua?0=0')];
case 1:
body = _a.sent();
return [2 /*return*/, this.parseGuestWlanHTML(body)];
}
});
});
};
// set guest WLAN settings - not part of Fritz API
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Fritz.prototype.setGuestWlan = function (enable) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
throw new Error('Not Implemented');
});
});
};
/**
* get phone devices
*/
Fritz.prototype.getPhoneList = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.executeCommand(true, null, null, '/fon_num/foncalls_list.lua?csv=')];
});
});
};
return Fritz;
}());
exports.default = Fritz;