@dotwee/homebridge-z2m
Version:
Expose your Zigbee devices to HomeKit with ease, by integrating Zigbee2MQTT with Homebridge.
94 lines • 3.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sanitizeAndFilterExposesEntries = exports.getAllEndpoints = exports.groupByEndpoint = exports.copyExposesRangeToCharacteristic = exports.roundToDecimalPlaces = exports.getOrAddCharacteristic = exports.errorToString = void 0;
const z2mModels_1 = require("./z2mModels");
function errorToString(e) {
if (typeof e === 'string') {
return e;
}
if (e instanceof Error) {
return e.message; // works, `e` narrowed to Error
}
return JSON.stringify(e);
}
exports.errorToString = errorToString;
function getOrAddCharacteristic(service, characteristic) {
return service.getCharacteristic(characteristic) || service.addCharacteristic(characteristic);
}
exports.getOrAddCharacteristic = getOrAddCharacteristic;
function roundToDecimalPlaces(input, decimalPlaces) {
if (decimalPlaces !== Math.round(decimalPlaces) || decimalPlaces < 1 || decimalPlaces > 10) {
throw new Error(`decimalPlaces must be a whole number between 1 and 10, not ${decimalPlaces}`);
}
const maxDecimals = Math.pow(10, decimalPlaces);
return Math.round((input + Number.EPSILON) * maxDecimals) / maxDecimals;
}
exports.roundToDecimalPlaces = roundToDecimalPlaces;
function copyExposesRangeToCharacteristic(exposes, characteristic) {
if ((0, z2mModels_1.exposesHasNumericRangeProperty)(exposes)) {
characteristic.setProps({
minValue: exposes.value_min,
maxValue: exposes.value_max,
minStep: exposes.value_step,
});
return true;
}
return false;
}
exports.copyExposesRangeToCharacteristic = copyExposesRangeToCharacteristic;
function groupByEndpoint(entries) {
const endpointMap = new Map();
entries.forEach((entry) => {
const collection = endpointMap.get(entry.endpoint);
if (!collection) {
endpointMap.set(entry.endpoint, [entry]);
}
else {
collection.push(entry);
}
});
return endpointMap;
}
exports.groupByEndpoint = groupByEndpoint;
function getAllEndpoints(entries, parentEndpoint) {
const endpoints = new Set();
entries.forEach((entry) => {
var _a;
const endpoint = (_a = entry.endpoint) !== null && _a !== void 0 ? _a : parentEndpoint;
if (endpoint !== undefined || entry.property !== undefined) {
endpoints.add(endpoint);
}
if ((0, z2mModels_1.exposesHasFeatures)(entry)) {
getAllEndpoints(entry.features, endpoint).forEach((e) => {
endpoints.add(e);
});
}
});
const result = Array.from(endpoints);
result.sort();
return result;
}
exports.getAllEndpoints = getAllEndpoints;
function sanitizeAndFilterExposesEntries(input, filter, valueFilter, parentEndpoint) {
return input
.filter((e) => filter === undefined || filter(e))
.map((e) => sanitizeAndFilterExposesEntry(e, filter, valueFilter, parentEndpoint));
}
exports.sanitizeAndFilterExposesEntries = sanitizeAndFilterExposesEntries;
function sanitizeAndFilterExposesEntry(input, filter, valueFilter, parentEndpoint) {
const output = {
...input,
};
if (output.endpoint === undefined && parentEndpoint !== undefined) {
// Make sure features inherit the endpoint from their parent, if it is not defined explicitly.
output.endpoint = parentEndpoint;
}
if ((0, z2mModels_1.exposesHasFeatures)(output)) {
output.features = sanitizeAndFilterExposesEntries(output.features, filter, valueFilter, output.endpoint);
}
if (Array.isArray(output.values) && valueFilter !== undefined) {
output.values = valueFilter(output);
}
return output;
}
//# sourceMappingURL=helpers.js.map