"use strict";
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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.definitions = exports.tzLocal = void 0;
const fz = __importStar(require("../converters/fromZigbee"));
const tz = __importStar(require("../converters/toZigbee"));
const constants = __importStar(require("../lib/constants"));
const exposes = __importStar(require("../lib/exposes"));
const logger_1 = require("../lib/logger");
const reporting = __importStar(require("../lib/reporting"));
const utils = __importStar(require("../lib/utils"));
const e = exposes.presets;
const ea = exposes.access;
const NS = "zhc:onesti";
exports.tzLocal = {
    easycode_auto_relock: {
        key: ["auto_relock"],
        convertSet: async (entity, key, value, meta) => {
            await entity.write("closuresDoorLock", { autoRelockTime: value ? 1 : 0 }, utils.getOptions(meta.mapped, entity));
            return { state: { auto_relock: value } };
        },
    },
};
const fzLocal = {
    nimly_pro_lock_actions: {
        cluster: "closuresDoorLock",
        type: ["attributeReport", "readResponse"],
        convert: (model, msg, publish, options, meta) => {
            const result = {};
            const attributes = {};
            // Handle attribute 257: last_used_pin_code
            // The lock sends PIN codes as the actual digits typed
            // Report exactly what the lock sends
            if (msg.data["257"] !== undefined) {
                const data = msg.data["257"];
                if (Buffer.isBuffer(data)) {
                    // Convert buffer to ASCII string
                    attributes.last_used_pin_code = data.toString("ascii").trim();
                }
                else if (Array.isArray(data)) {
                    // Array of bytes, convert to ASCII string
                    attributes.last_used_pin_code = Buffer.from(data).toString("ascii").trim();
                }
                else if (typeof data === "string") {
                    // Already a string
                    attributes.last_used_pin_code = data.trim();
                }
                else {
                    // Fallback: convert to string
                    attributes.last_used_pin_code = String(data);
                }
            }
            // Handle attribute 256: last action (lock/unlock) source and user
            // Format: 4 bytes as 32-bit integer
            // Byte 0: Source (00=zigbee, 02=keypad, 03=finger, 04=rfid, 0a=manual)
            // Byte 1: Action (01=lock, 02=unlock)
            // Bytes 2-3: User ID (16-bit integer)
            if (msg.data["256"] !== undefined) {
                const hex = msg.data["256"].toString(16).padStart(8, "0");
                const firstOctet = String(hex.substring(0, 2));
                const lookup = {
                    "00": "zigbee",
                    "02": "keypad",
                    "03": "fingerprintsensor",
                    "04": "rfid",
                    "0a": "self",
                };
                result.last_action_source = lookup[firstOctet] || "unknown";
                const secondOctet = hex.substring(2, 4);
                const thirdOctet = hex.substring(4, 8);
                result.last_action_user = Number.parseInt(thirdOctet, 16);
                // Store user ID as string for consistency with Home Assistant expectations
                const userIdStr = result.last_action_user.toString();
                if (secondOctet === "01") {
                    attributes.last_lock_user = userIdStr;
                    attributes.last_lock_source = result.last_action_source;
                }
                else if (secondOctet === "02") {
                    attributes.last_unlock_user = userIdStr;
                    attributes.last_unlock_source = result.last_action_source;
                }
            }
            // Handle voltage attribute (if present)
            if (Object.hasOwn(msg.data, "voltage")) {
                attributes.voltage = msg.data["voltage"];
            }
            // Handle auto_relock_time attribute (if present)
            if (Object.hasOwn(msg.data, "autoRelockTime")) {
                attributes.auto_relock_time = msg.data["autoRelockTime"];
            }
            // Handle lock capabilities (if present)
            // Attribute 18 (0x12): Number of PIN users supported
            if (Object.hasOwn(msg.data, 18)) {
                attributes.max_pin_users = msg.data[18];
            }
            // Attribute 23 (0x17): Min PIN code length
            if (Object.hasOwn(msg.data, 23)) {
                attributes.min_pin_length = msg.data[23];
            }
            // Attribute 24 (0x18): Max PIN code length
            if (Object.hasOwn(msg.data, 24)) {
                attributes.max_pin_length = msg.data[24];
            }
            // Return result if not empty
            if (Object.keys(attributes).length > 0) {
                return attributes;
            }
        },
    },
    easycodetouch_action: {
        cluster: "closuresDoorLock",
        type: "raw",
        convert: (model, msg, publish, options, meta) => {
            const value = constants.easyCodeTouchActions[(msg.data[3] << 8) | msg.data[4]];
            if (value) {
                return { action: value };
            }
            logger_1.logger.warning(`Unknown lock status with source ${msg.data[3]} and event code ${msg.data[4]}`, NS);
        },
    },
};
exports.definitions = [
    {
        zigbeeModel: ["easyCodeTouch_v1", "EasyCodeTouch", "EasyFingerTouch"],
        model: "easyCodeTouch_v1",
        vendor: "Onesti Products AS",
        description: "Zigbee module for EasyAccess code touch series",
        fromZigbee: [
            fzLocal.nimly_pro_lock_actions,
            fz.lock_set_pin_code_response,
            fz.lock,
            fz.lock_operation_event,
            fz.battery,
            fz.lock_programming_event,
            fzLocal.easycodetouch_action,
        ],
        toZigbee: [tz.lock, exports.tzLocal.easycode_auto_relock, tz.lock_sound_volume, tz.pincode_lock],
        meta: { pinCodeCount: 1000, battery: { dontDividePercentage: true } },
        configure: async (device, coordinatorEndpoint) => {
            const endpoint = device.getEndpoint(11);
            await reporting.bind(endpoint, coordinatorEndpoint, ["closuresDoorLock", "genPowerCfg"]);
            await reporting.lockState(endpoint);
            await reporting.batteryPercentageRemaining(endpoint);
            await endpoint.read("closuresDoorLock", ["lockState", "soundVolume"]);
            // Try to read lock capabilities (may not be supported by all models)
            try {
                await endpoint.read("closuresDoorLock", [18, 23, 24]); // maxPinUsers, minPinLength, maxPinLength
            }
            catch (_error) {
                // Capabilities read may fail on some models - this is expected and harmless
                // Attributes will be exposed if the lock reports them during operation
            }
            device.powerSource = "Battery";
            device.save();
        },
        exposes: [
            e.lock(),
            e.battery(),
            e.sound_volume(),
            e.voltage(),
            e
                .enum("last_unlock_source", ea.STATE, ["zigbee", "keypad", "fingerprintsensor", "rfid", "self", "unknown"])
                .withDescription("Last unlock source"),
            e.text("last_unlock_user", ea.STATE).withDescription("Last unlock user (slot number)"),
            e
                .enum("last_lock_source", ea.STATE, ["zigbee", "keypad", "fingerprintsensor", "rfid", "self", "unknown"])
                .withDescription("Last lock source"),
            e.text("last_lock_user", ea.STATE).withDescription("Last lock user (slot number)"),
            e.text("last_used_pin_code", ea.STATE).withDescription("Last used pin code (actual digits)"),
            e.binary("auto_relock", ea.STATE_SET, true, false).withDescription("Auto relock after 7 seconds."),
            e.numeric("auto_relock_time", ea.STATE).withUnit("s").withDescription("Auto relock delay in seconds"),
            e.numeric("max_pin_users", ea.STATE).withDescription("Maximum number of PIN users supported"),
            e.numeric("min_pin_length", ea.STATE).withDescription("Minimum PIN code length"),
            e.numeric("max_pin_length", ea.STATE).withDescription("Maximum PIN code length"),
            e.pincode(),
            e.text("last_successful_pincode_clear", ea.STATE).withDescription("Last deleted Pincode"),
            e.text("last_successful_pincode_save", ea.STATE).withDescription("Last saved Pincode"),
        ],
    },
    {
        zigbeeModel: ["NimlyPRO", "NimlyCode", "NimlyTouch", "NimlyIn", "NimlyPRO24", "NimlyShared", "NimlyCodePRO"],
        model: "Nimly",
        vendor: "Onesti Products AS",
        description: "Zigbee module for Nimly Doorlock series",
        fromZigbee: [
            fzLocal.nimly_pro_lock_actions,
            fz.lock,
            fz.lock_operation_event,
            fz.battery,
            fz.lock_programming_event,
            fzLocal.easycodetouch_action,
        ],
        toZigbee: [tz.lock, exports.tzLocal.easycode_auto_relock, tz.lock_sound_volume, tz.pincode_lock],
        meta: { pinCodeCount: 1000, battery: { dontDividePercentage: true } },
        configure: async (device, coordinatorEndpoint) => {
            const endpoint = device.getEndpoint(11);
            await reporting.bind(endpoint, coordinatorEndpoint, ["closuresDoorLock", "genPowerCfg"]);
            await reporting.lockState(endpoint);
            await reporting.batteryPercentageRemaining(endpoint);
            await endpoint.read("closuresDoorLock", ["lockState", "soundVolume"]);
            device.powerSource = "Battery";
            device.save();
        },
        exposes: [
            e.lock(),
            e.battery(),
            e.sound_volume(),
            e.voltage(),
            e
                .enum("last_unlock_source", ea.STATE, ["zigbee", "keypad", "fingerprintsensor", "rfid", "self", "unknown"])
                .withDescription("Last unlock source"),
            e.text("last_unlock_user", ea.STATE).withDescription("Last unlock user (slot number)"),
            e
                .enum("last_lock_source", ea.STATE, ["zigbee", "keypad", "fingerprintsensor", "rfid", "self", "unknown"])
                .withDescription("Last lock source"),
            e.text("last_lock_user", ea.STATE).withDescription("Last lock user (slot number)"),
            e.text("last_used_pin_code", ea.STATE).withDescription("Last used pin code (actual digits)"),
            e.binary("auto_relock", ea.STATE_SET, true, false).withDescription("Auto relock after 7 seconds."),
            e.numeric("auto_relock_time", ea.STATE).withUnit("s").withDescription("Auto relock delay in seconds"),
            e.numeric("max_pin_users", ea.STATE).withDescription("Maximum number of PIN users supported"),
            e.numeric("min_pin_length", ea.STATE).withDescription("Minimum PIN code length"),
            e.numeric("max_pin_length", ea.STATE).withDescription("Maximum PIN code length"),
            e.pincode(),
        ],
    },
    {
        zigbeeModel: ["S4RX-110"],
        model: "S4RX-110",
        vendor: "Onesti Products AS",
        description: "Relax smart plug",
        fromZigbee: [fz.on_off, fz.electrical_measurement, fz.metering, fz.device_temperature, fz.identify],
        toZigbee: [tz.on_off],
        exposes: [e.switch(), e.power(), e.current(), e.voltage(), e.energy(), e.device_temperature()],
        configure: async (device, coordinatorEndpoint) => {
            const endpoint = device.getEndpoint(2);
            await reporting.bind(endpoint, coordinatorEndpoint, [
                "genIdentify",
                "genOnOff",
                "genDeviceTempCfg",
                "haElectricalMeasurement",
                "seMetering",
            ]);
            await reporting.onOff(endpoint);
            await reporting.readEletricalMeasurementMultiplierDivisors(endpoint);
            await reporting.activePower(endpoint);
            await reporting.rmsCurrent(endpoint);
            await reporting.rmsVoltage(endpoint);
            await reporting.readMeteringMultiplierDivisor(endpoint);
            await reporting.currentSummDelivered(endpoint);
            await reporting.deviceTemperature(endpoint);
        },
        endpoint: (device) => {
            return { default: 2 };
        },
    },
];
//# sourceMappingURL=onesti.js.map