homebridge-electromagnetic-lock

中文文档
2.0.2发布于 2026/11/04MITCommonJS零依赖

Homebridge plugin to control an electromagnetic lock through Raspberry Pi GPIO lines

原始使用文档
文档目录8 节
  1. Safety behaviour
  2. Requirements
  3. Installation
  4. Configuration
  5. Upgrading
  6. From 2.0.1
  7. From 1.x
  8. Troubleshooting

Homebridge Electromagnetic Lock

Control an electromagnetic lock through a Raspberry Pi GPIO line and expose it to Apple Home as a lock mechanism.

Version 2.0.2 uses a Homebridge dynamic platform and is designed for Homebridge 2 with Node.js 22.12 or 24.

Safety behaviour

  • The GPIO line is requested directly in the secured state, including for an active-low relay.
  • The lock automatically returns to the secured state after the configured unlock duration.
  • Homebridge and HomeKit states are updated together.
  • GPIO failures are shown as a jammed lock instead of crashing Homebridge.
  • During shutdown, the plugin writes the secured value before releasing the GPIO line.

Test the relay logic without connecting the lock power supply. Hardware design must provide a safe state when the Raspberry Pi is powered off or the GPIO line is released.

Requirements

  • Homebridge 2.x
  • Node.js 22.12.x or 24.x
  • Raspberry Pi OS / Debian with a libgpiod 1.x development package
  • A relay interface suitable for the Raspberry Pi's 3.3 V GPIO

On Debian 12 / Raspberry Pi OS Bookworm:

sudo apt update
sudo apt install -y build-essential gpiod libgpiod-dev

The Homebridge service user must have permission to access /dev/gpiochip*. On standard Raspberry Pi OS installations this normally means membership in the gpio group:

sudo usermod -aG gpio homebridge
sudo hb-service restart

Installation

sudo npm install -g homebridge-electromagnetic-lock

Homebridge UI can also install the package by searching for homebridge-electromagnetic-lock.

Configuration

New installations should use the dynamic platform:

{
  "platform": "ElectromagneticLock",
  "name": "Lock",
  "lockPin": 18,
  "gpioChip": 0,
  "activeLow": true,
  "unlockingDuration": 2
}
  • name: accessory name shown in Apple Home.
  • lockPin: BCM GPIO / libgpiod line number, not the physical header pin.
  • gpioChip: libgpiod chip number; normally 0.
  • activeLow: true when GPIO value 0 activates the relay.
  • unlockingDuration: seconds before automatic re-lock, from 1 to 3600.

For example, Raspberry Pi physical header pin 12 is BCM GPIO 18, so configure "lockPin": 18.

Upgrading

From 2.0.1

The old accessory configuration remains supported in 2.0.2, so existing installations continue to start:

{
  "accessory": "ElectromagneticLock",
  "name": "Lock",
  "lockPin": 18,
  "gpioChip": 0,
  "activeLow": true,
  "unlockingDuration": 2
}

To use the dynamic platform, remove that block from accessories and add the new block to platforms. Do not keep both configurations active because both would attempt to reserve the same GPIO line.

From 1.x

Version 2 changed the backend from rpi-gpio/sysfs to libgpiod. The lockPin option is now a BCM GPIO line number rather than a physical header pin. For example, replace physical pin 12 with "lockPin": 18.

Troubleshooting

  • Confirm the chip and line with gpioinfo.
  • Confirm the Homebridge service user can access /dev/gpiochip*.
  • Install libgpiod-dev before installing the npm package because the native GPIO binding is compiled locally.
  • Run Homebridge in debug mode and look for the plugin's GPIO initialization message.

版本与发布记录

homebridge-electromagnetic-lock 共发布 5 个版本,首次发布于 2023/09/01;过去 12 个月发布了 2 个版本。

版本发布日期距今
2.0.2latest2026/11/041 天前
2.0.12026/06/104 个月前
1.0.22023/09/023 年前
1.0.12023/09/023 年前
1.0.02023/09/013 年前

关于 homebridge-electromagnetic-lock 的常见问题

homebridge-electromagnetic-lock 是什么?

Homebridge plugin to control an electromagnetic lock through Raspberry Pi GPIO lines

如何安装 homebridge-electromagnetic-lock?

在项目目录运行 npm install homebridge-electromagnetic-lock,或使用 pnpm add / yarn add / bun add。

homebridge-electromagnetic-lock 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/homebridge-electromagnetic-lock 是否存在。

homebridge-electromagnetic-lock 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

homebridge-electromagnetic-lock 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.12.0 || ^24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

homebridge-electromagnetic-lock 可以商用吗?

homebridge-electromagnetic-lock 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

homebridge-electromagnetic-lock 还在维护吗?

最新版本 2.0.2 发布于 2026/11/04(1 天前);至今共发布 5 个版本,其中过去 12 个月发布了 2 个;首次发布于 2023/09/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。