@dotwee/homebridge-z2m
Version:
Expose your Zigbee devices to HomeKit with ease, by integrating Zigbee2MQTT with Homebridge.
181 lines • 8.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPropertyValueConfiguration = exports.isDeviceConfiguration = exports.isBaseDeviceConfiguration = exports.hasOptionalStringArrays = exports.isAdaptiveLightingConfiguration = exports.isMqttConfiguration = exports.isLogConfiguration = exports.isPluginConfiguration = void 0;
const z2mModels_1 = require("./z2mModels");
function hasValidConverterConfigurations(config, converterConfigValidator, logger) {
return config.converters === undefined || converterConfigValidator.allConverterConfigurationsAreValid(config.converters, logger);
}
function hasValidDeviceConfigurations(devices, converterConfigValidator, logger) {
if (devices !== undefined) {
if (!Array.isArray(devices)) {
logger === null || logger === void 0 ? void 0 : logger.error('Incorrect configuration: devices must be an array');
return false;
}
for (const element of devices) {
if (!(0, exports.isDeviceConfiguration)(element) || !hasValidConverterConfigurations(element, converterConfigValidator, logger)) {
logger === null || logger === void 0 ? void 0 : logger.error('Incorrect configuration: Entry for device is not correct: ' + JSON.stringify(element));
return false;
}
}
}
return true;
}
const isPluginConfiguration = (x, converterConfigValidator, logger = undefined) => {
if (x.mqtt === undefined || !(0, exports.isMqttConfiguration)(x.mqtt)) {
logger === null || logger === void 0 ? void 0 : logger.error('Incorrect configuration: mqtt does not contain required fields');
return false;
}
if (x.log !== undefined && !(0, exports.isLogConfiguration)(x.log)) {
logger === null || logger === void 0 ? void 0 : logger.error('Incorrect configuration: log configuration is invalid: ' + JSON.stringify(x.log));
}
if (x.defaults !== undefined) {
if (!(0, exports.isBaseDeviceConfiguration)(x.defaults)) {
logger === null || logger === void 0 ? void 0 : logger.error('Incorrect configuration: Device defaults are incorrect: ' + JSON.stringify(x.defaults));
return false;
}
if (!hasValidConverterConfigurations(x.defaults, converterConfigValidator, logger)) {
logger === null || logger === void 0 ? void 0 : logger.error('Incorrect configuration: Invalid converter configuration in device defaults.');
return false;
}
}
if (x.experimental !== undefined && !isStringArray(x.experimental)) {
logger === null || logger === void 0 ? void 0 : logger.error('Incorrect configuration: Experimental flags are incorrect ' + JSON.stringify(x.experimental));
return false;
}
if (x.exclude_grouped_devices !== undefined && typeof x.exclude_grouped_devices !== 'boolean') {
logger === null || logger === void 0 ? void 0 : logger.error('Incorrect configuration: exclude_grouped_devices must be a boolean, if defined.');
return false;
}
return hasValidDeviceConfigurations(x.devices, converterConfigValidator, logger);
};
exports.isPluginConfiguration = isPluginConfiguration;
const allowedLogLevels = ["debug" /* LogLevel.DEBUG */, "info" /* LogLevel.INFO */, "warn" /* LogLevel.WARN */, "error" /* LogLevel.ERROR */];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isLogConfiguration = (x) => !(x.mqtt_publish !== undefined && typeof x.mqtt_publish !== 'string' && !allowedLogLevels.includes(x.mqtt_publish));
exports.isLogConfiguration = isLogConfiguration;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isMqttConfiguration = (x) => x.base_topic !== undefined &&
typeof x.base_topic === 'string' &&
x.base_topic.length > 0 &&
x.server !== undefined &&
typeof x.server === 'string' &&
x.server.length > 0;
exports.isMqttConfiguration = isMqttConfiguration;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isAdaptiveLightingConfiguration = (x) => {
if (x.enabled !== undefined && typeof x.enabled !== 'boolean') {
return false;
}
if (x.min_ct_change !== undefined) {
if (typeof x.min_ct_change !== 'number') {
return false;
}
if (x.min_ct_change < 0) {
return false;
}
}
if (x.transition !== undefined) {
if (typeof x.transition !== 'number') {
return false;
}
if (x.transition < 0 || x.transition > 300) {
return false;
}
}
return true;
};
exports.isAdaptiveLightingConfiguration = isAdaptiveLightingConfiguration;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const hasOptionalStringArrays = (object, ...properties) => {
// Check if properties exist and are string arrays
for (const property of properties) {
if (property in object && object[property] !== undefined && !isStringArray(object[property])) {
return false;
}
}
return true;
};
exports.hasOptionalStringArrays = hasOptionalStringArrays;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isBaseDeviceConfiguration = (x) => {
// Optional boolean exclude property
if (x.exclude !== undefined && typeof x.exclude !== 'boolean') {
return false;
}
// Optional string arrays
if (!(0, exports.hasOptionalStringArrays)(x, 'excluded_keys', 'excluded_endpoints', 'experimental')) {
return false;
}
// Optional adaptive lighting options
if (x.adaptive_lighting !== undefined && !(0, exports.isAdaptiveLightingConfiguration)(x.adaptive_lighting)) {
return false;
}
// Optional 'converters' must be an object if present
if (x.converters !== undefined && typeof x.converters !== 'object') {
return false;
}
// Optional values property which must be an array of PropertyValueConfigurations if present
if (x.values !== undefined) {
if (!Array.isArray(x.values)) {
return false;
}
for (const element of x.values) {
if (!(0, exports.isPropertyValueConfiguration)(element)) {
return false;
}
}
}
return true;
};
exports.isBaseDeviceConfiguration = isBaseDeviceConfiguration;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isDeviceConfiguration = (x) => {
// Required id property
if (x.id === undefined || typeof x.id !== 'string' || x.id.length < 1) {
return false;
}
// Optional included_keys which must be an array of strings if present
if (x.included_keys !== undefined && !isStringArray(x.included_keys)) {
return false;
}
// Check if exposes is an array of ExposesEntry, if configured.
if (x.exposes !== undefined) {
if (!Array.isArray(x.exposes)) {
return false;
}
for (const element of x.exposes) {
if (!(0, z2mModels_1.isExposesEntry)(element)) {
return false;
}
}
}
return (0, exports.isBaseDeviceConfiguration)(x);
};
exports.isDeviceConfiguration = isDeviceConfiguration;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isPropertyValueConfiguration = (x) => {
// Required 'property' property
if (x.property === undefined || typeof x.property !== 'string' || x.property.length < 1) {
return false;
}
// Optional include property
if (x.include !== undefined && !isStringArray(x.include)) {
return false;
}
// Optional exclude property
return x.exclude === undefined || isStringArray(x.exclude);
};
exports.isPropertyValueConfiguration = isPropertyValueConfiguration;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isStringArray = (x) => {
if (!Array.isArray(x)) {
return false;
}
for (const element of x) {
if (typeof element !== 'string') {
return false;
}
}
return true;
};
//# sourceMappingURL=configModels.js.map