matterbridge-leviton 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +134 -2
  3. package/dist/handlers/DeviceHandler.d.ts +19 -0
  4. package/dist/handlers/DeviceHandler.d.ts.map +1 -0
  5. package/dist/handlers/DeviceHandler.js +2 -0
  6. package/dist/handlers/DeviceHandler.js.map +1 -0
  7. package/dist/handlers/DimmerHandler.d.ts +11 -0
  8. package/dist/handlers/DimmerHandler.d.ts.map +1 -0
  9. package/dist/handlers/DimmerHandler.js +33 -0
  10. package/dist/handlers/DimmerHandler.js.map +1 -0
  11. package/dist/handlers/SwitchHandler.d.ts +26 -0
  12. package/dist/handlers/SwitchHandler.d.ts.map +1 -0
  13. package/dist/handlers/SwitchHandler.js +60 -0
  14. package/dist/handlers/SwitchHandler.js.map +1 -0
  15. package/dist/handlers/handlerRegistry.d.ts +4 -0
  16. package/dist/handlers/handlerRegistry.d.ts.map +1 -0
  17. package/dist/handlers/handlerRegistry.js +15 -0
  18. package/dist/handlers/handlerRegistry.js.map +1 -0
  19. package/dist/index.d.ts +5 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +5 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/leviton/LevitonClient.d.ts +30 -0
  24. package/dist/leviton/LevitonClient.d.ts.map +1 -0
  25. package/dist/leviton/LevitonClient.js +111 -0
  26. package/dist/leviton/LevitonClient.js.map +1 -0
  27. package/dist/leviton/LevitonSocket.d.ts +38 -0
  28. package/dist/leviton/LevitonSocket.d.ts.map +1 -0
  29. package/dist/leviton/LevitonSocket.js +129 -0
  30. package/dist/leviton/LevitonSocket.js.map +1 -0
  31. package/dist/leviton/types.d.ts +52 -0
  32. package/dist/leviton/types.d.ts.map +1 -0
  33. package/dist/leviton/types.js +2 -0
  34. package/dist/leviton/types.js.map +1 -0
  35. package/dist/mapper/DeviceMapper.d.ts +4 -0
  36. package/dist/mapper/DeviceMapper.d.ts.map +1 -0
  37. package/dist/mapper/DeviceMapper.js +27 -0
  38. package/dist/mapper/DeviceMapper.js.map +1 -0
  39. package/dist/mapper/ValueConverter.d.ts +8 -0
  40. package/dist/mapper/ValueConverter.d.ts.map +1 -0
  41. package/dist/mapper/ValueConverter.js +19 -0
  42. package/dist/mapper/ValueConverter.js.map +1 -0
  43. package/dist/platform.d.ts +26 -0
  44. package/dist/platform.d.ts.map +1 -0
  45. package/dist/platform.js +137 -0
  46. package/dist/platform.js.map +1 -0
  47. package/matterbridge-leviton.schema.json +31 -0
  48. package/package.json +61 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andy Alm
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,135 @@
1
- # Temporary Holding Version
1
+ # matterbridge-leviton
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm version](https://img.shields.io/npm/v/matterbridge-leviton)](https://www.npmjs.com/package/matterbridge-leviton)
4
+ [![npm downloads](https://img.shields.io/npm/dm/matterbridge-leviton)](https://www.npmjs.com/package/matterbridge-leviton)
5
+ [![CI](https://github.com/andyalm/matterbridge-leviton/actions/workflows/ci.yml/badge.svg)](https://github.com/andyalm/matterbridge-leviton/actions/workflows/ci.yml)
6
+ [![license](https://img.shields.io/github/license/andyalm/matterbridge-leviton)](https://github.com/andyalm/matterbridge-leviton/blob/main/LICENSE)
7
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
8
+
9
+ A [Matterbridge](https://github.com/Luligu/matterbridge) plugin that exposes first-generation Leviton Decora Smart Wi-Fi switches and dimmers as Matter devices.
10
+
11
+ Leviton's second-generation Decora Smart devices support Matter natively, but the first-generation `DW` models never will. This plugin bridges them so they can be controlled through Matter-compatible ecosystems (Apple Home, Google Home, Amazon Alexa).
12
+
13
+ ## This plugin uses Leviton's cloud
14
+
15
+ First-generation Decora Wi-Fi devices cannot be controlled over the local network. They have no open ports and talk only to Leviton's cloud, so this plugin uses the same My Leviton cloud API as the My Leviton app. That means:
16
+
17
+ - It needs your My Leviton email and password.
18
+ - It needs an internet connection, and it stops working whenever Leviton's service is down.
19
+ - The API is undocumented and Leviton could change it at any time.
20
+
21
+ ## Features
22
+
23
+ - Bridges first-generation dimmers, switches, plug-in modules and receptacles
24
+ - Bidirectional state sync: changes made at the wall or in the My Leviton app show up in Matter within moments through push notifications
25
+ - Devices that lose their connection to Leviton are shown as unreachable
26
+ - Auto-reconnect with exponential backoff if the push connection drops, with a once-a-minute poll as a safety net
27
+ - Device include/exclude filtering to control which devices are bridged
28
+
29
+ ## Prerequisites
30
+
31
+ - [Matterbridge](https://github.com/Luligu/matterbridge) installed and running
32
+ - A My Leviton account with your devices added to it
33
+ - **Two-factor authentication turned off** on the My Leviton account (the plugin cannot enter the emailed code)
34
+
35
+ ## Installation
36
+
37
+ ### Matterbridge Frontend (recommended)
38
+
39
+ Open the Matterbridge web UI, go to the Plugins page, search for `matterbridge-leviton`, and click Install.
40
+
41
+ ### CLI
42
+
43
+ ```bash
44
+ matterbridge -add matterbridge-leviton
45
+ ```
46
+
47
+ ### From Source
48
+
49
+ See the [Development Guide](docs/development.md).
50
+
51
+ ## Configuration
52
+
53
+ Configuration is set through the Matterbridge web UI plugin settings page.
54
+
55
+ | Setting | Default | Description |
56
+ |---------|---------|-------------|
57
+ | `email` | | My Leviton account email |
58
+ | `password` | | My Leviton account password |
59
+ | `includeDevices` | `[]` | Device names or serial numbers to include (empty = all) |
60
+ | `excludeDevices` | `[]` | Device names or serial numbers to exclude |
61
+ | `debug` | `false` | Enable debug logging |
62
+
63
+ Example configuration:
64
+
65
+ ```json
66
+ {
67
+ "email": "you@example.com",
68
+ "password": "your-password",
69
+ "includeDevices": [],
70
+ "excludeDevices": ["Garage Outlet"]
71
+ }
72
+ ```
73
+
74
+ The password is stored in Matterbridge's plugin configuration file in plain text.
75
+
76
+ ## Supported Devices
77
+
78
+ | Leviton model | Description | Matter Device Type |
79
+ |--------------|-------------|-------------------|
80
+ | DW6HD | 600W in-wall dimmer | Dimmable Light |
81
+ | DW1KD | 1000W in-wall dimmer | Dimmable Light |
82
+ | DWVAA | Voice dimmer | Dimmable Light |
83
+ | DW3HL | Plug-in dimmer | Dimmable Light |
84
+ | DW15S | 15A in-wall switch | On/Off Light |
85
+ | DW15P | Mini plug-in switch | On/Off Plug-in Unit |
86
+ | DW15A | Plug-in switch | On/Off Plug-in Unit |
87
+ | DW15R | Tamper-resistant receptacle | On/Off Plug-in Unit |
88
+
89
+ Everything else in the account is ignored, including second-generation devices (`D2xxx` models, which you can add to Matter directly).
90
+
91
+ ## How It Works
92
+
93
+ 1. The plugin logs in to My Leviton and lists the devices in every residence on the account
94
+ 2. Each supported device is mapped to a Matter device type based on its model
95
+ 3. Matter endpoints are created with the appropriate clusters and registered with Matterbridge
96
+ 4. State changes flow bidirectionally:
97
+ - **Leviton to Matter**: push notifications from Leviton's websocket are translated and written to Matter attributes
98
+ - **Matter to Leviton**: commands from Matter controllers (on/off, brightness) are sent to Leviton's API
99
+
100
+ ## Inspecting Your Leviton Devices
101
+
102
+ A utility script is included to list the devices in your account, which is useful for finding names and serial numbers for include/exclude filtering:
103
+
104
+ ```bash
105
+ LEVITON_EMAIL=you@example.com LEVITON_PASSWORD=... pnpm exec jiti scripts/list-leviton-devices.ts
106
+ ```
107
+
108
+ ## Development
109
+
110
+ ```bash
111
+ pnpm install
112
+ pnpm run dev:link # One-time: link matterbridge for local development
113
+ pnpm run build
114
+ pnpm test
115
+ ```
116
+
117
+ See the [Development Guide](docs/development.md) for full details on project setup, architecture, and contributing.
118
+
119
+ ## Troubleshooting
120
+
121
+ - **"Leviton rejected the email or password"**: fix the credentials in the plugin settings and restart the plugin. The plugin deliberately does not retry a rejected login, because Leviton locks accounts after repeated failures.
122
+ - **"Two-factor authentication is enabled"**: turn 2FA off in the My Leviton app, then restart the plugin.
123
+ - **A device is missing**: check the Matterbridge log for a `Skipping` line, which says why. Only the models in [Supported Devices](#supported-devices) are bridged.
124
+ - **A device shows as unreachable**: it has lost its Wi-Fi or its connection to Leviton. It will also show as offline in the My Leviton app.
125
+
126
+ ## Known Limitations
127
+
128
+ - No local control: everything goes through Leviton's cloud
129
+ - Accounts with two-factor authentication are not yet supported
130
+ - The DW4SF fan speed controller and DW4BC scene controller are not supported
131
+ - A device removed from the My Leviton account stays in Matter until the plugin is restarted
132
+
133
+ ## License
134
+
135
+ MIT
@@ -0,0 +1,19 @@
1
+ import type { MatterbridgeEndpoint } from 'matterbridge';
2
+ import type { AnsiLogger } from 'matterbridge/logger';
3
+ import type { LevitonClient } from '../leviton/LevitonClient.js';
4
+ import type { IotSwitch, SwitchState } from '../leviton/types.js';
5
+ export interface DeviceHandlerContext {
6
+ endpoint: MatterbridgeEndpoint;
7
+ device: IotSwitch;
8
+ log: AnsiLogger;
9
+ client: LevitonClient;
10
+ }
11
+ export interface DeviceHandler {
12
+ /** Add Matter clusters to the endpoint (called before endpoint registration). */
13
+ addClusters(endpoint: MatterbridgeEndpoint): void;
14
+ /** Register command handlers and set initial state (called after endpoint registration). */
15
+ setup(): Promise<void>;
16
+ /** Push a state change reported by Leviton to Matter. */
17
+ handleUpdate(state: SwitchState): Promise<void>;
18
+ }
19
+ //# sourceMappingURL=DeviceHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeviceHandler.d.ts","sourceRoot":"","sources":["../../src/handlers/DeviceHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AACjE,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElE,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,MAAM,EAAE,SAAS,CAAC;IAClB,GAAG,EAAE,UAAU,CAAC;IAChB,MAAM,EAAE,aAAa,CAAC;CACvB;AAED,MAAM,WAAW,aAAa;IAC5B,iFAAiF;IACjF,WAAW,CAAC,QAAQ,EAAE,oBAAoB,GAAG,IAAI,CAAC;IAElD,4FAA4F;IAC5F,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB,yDAAyD;IACzD,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACjD"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=DeviceHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeviceHandler.js","sourceRoot":"","sources":["../../src/handlers/DeviceHandler.ts"],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ import type { MatterbridgeEndpoint } from 'matterbridge';
2
+ import type { SwitchState } from '../leviton/types.js';
3
+ import { SwitchHandler } from './SwitchHandler.js';
4
+ /** Handler for dimmers: on/off plus brightness. */
5
+ export declare class DimmerHandler extends SwitchHandler {
6
+ addClusters(endpoint: MatterbridgeEndpoint): void;
7
+ protected registerCommandHandlers(): void;
8
+ protected applyState(state: SwitchState): Promise<void>;
9
+ private toBrightness;
10
+ }
11
+ //# sourceMappingURL=DimmerHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DimmerHandler.d.ts","sourceRoot":"","sources":["../../src/handlers/DimmerHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,mDAAmD;AACnD,qBAAa,aAAc,SAAQ,aAAa;IACrC,WAAW,CAAC,QAAQ,EAAE,oBAAoB,GAAG,IAAI;cAKvC,uBAAuB,IAAI,IAAI;cAgBzB,UAAU,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAQtE,OAAO,CAAC,YAAY;CAGrB"}
@@ -0,0 +1,33 @@
1
+ import { levitonBrightnessToMatterLevel, matterLevelToLevitonBrightness } from '../mapper/ValueConverter.js';
2
+ import { SwitchHandler } from './SwitchHandler.js';
3
+ /** Handler for dimmers: on/off plus brightness. */
4
+ export class DimmerHandler extends SwitchHandler {
5
+ addClusters(endpoint) {
6
+ super.addClusters(endpoint);
7
+ endpoint.createDefaultLevelControlClusterServer();
8
+ }
9
+ registerCommandHandlers() {
10
+ super.registerCommandHandlers();
11
+ // Changes brightness without changing whether the light is on
12
+ this.endpoint.addCommandHandler('moveToLevel', async (data) => {
13
+ await this.send({ brightness: this.toBrightness(data.request.level) });
14
+ });
15
+ // Power and brightness go in one request. Sent separately, Leviton dimmers
16
+ // can turn back on after being switched off.
17
+ this.endpoint.addCommandHandler('moveToLevelWithOnOff', async (data) => {
18
+ const level = data.request.level;
19
+ await this.send(level <= 0 ? { power: 'OFF' } : { power: 'ON', brightness: this.toBrightness(level) });
20
+ });
21
+ }
22
+ async applyState(state) {
23
+ await super.applyState(state);
24
+ if (state.brightness !== undefined) {
25
+ const level = levitonBrightnessToMatterLevel(state.brightness);
26
+ await this.endpoint.setAttribute('levelControl', 'currentLevel', level, this.log);
27
+ }
28
+ }
29
+ toBrightness(matterLevel) {
30
+ return matterLevelToLevitonBrightness(matterLevel, this.device.minLevel, this.device.maxLevel);
31
+ }
32
+ }
33
+ //# sourceMappingURL=DimmerHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DimmerHandler.js","sourceRoot":"","sources":["../../src/handlers/DimmerHandler.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,8BAA8B,EAAE,8BAA8B,EAAE,MAAM,6BAA6B,CAAC;AAC7G,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,mDAAmD;AACnD,MAAM,OAAO,aAAc,SAAQ,aAAa;IACrC,WAAW,CAAC,QAA8B;QACjD,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;QAC5B,QAAQ,CAAC,sCAAsC,EAAE,CAAC;IACpD,CAAC;IAEkB,uBAAuB;QACxC,KAAK,CAAC,uBAAuB,EAAE,CAAC;QAEhC,8DAA8D;QAC9D,IAAI,CAAC,QAAQ,CAAC,iBAAiB,CAAC,aAAa,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;YAC5D,MAAM,IAAI,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACzE,CAAC,CAAC,CAAC;QAEH,2EAA2E;QAC3E,6CAA6C;QAC7C,IAAI,CAAC,QAAQ,CAAC,iBAAiB,CAAC,sBAAsB,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;YACrE,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YACjC,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACzG,CAAC,CAAC,CAAC;IACL,CAAC;IAEkB,KAAK,CAAC,UAAU,CAAC,KAAkB;QACpD,MAAM,KAAK,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAC9B,IAAI,KAAK,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YACnC,MAAM,KAAK,GAAG,8BAA8B,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;YAC/D,MAAM,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,cAAc,EAAE,cAAc,EAAE,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;QACpF,CAAC;IACH,CAAC;IAEO,YAAY,CAAC,WAAmB;QACtC,OAAO,8BAA8B,CAAC,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACjG,CAAC;CACF"}
@@ -0,0 +1,26 @@
1
+ import type { MatterbridgeEndpoint } from 'matterbridge';
2
+ import type { AnsiLogger } from 'matterbridge/logger';
3
+ import type { IotSwitch, SwitchCommand, SwitchState } from '../leviton/types.js';
4
+ import type { DeviceHandler, DeviceHandlerContext } from './DeviceHandler.js';
5
+ /**
6
+ * Handler for on/off devices: the in-wall switch, plug-in modules and receptacle.
7
+ *
8
+ * Commands are only sent to Leviton. Matter attributes change when Leviton reports
9
+ * the new state back, so Matter always reflects what the device actually did.
10
+ */
11
+ export declare class SwitchHandler implements DeviceHandler {
12
+ protected readonly endpoint: MatterbridgeEndpoint;
13
+ /** The last state Leviton reported for this device. */
14
+ protected readonly device: IotSwitch;
15
+ protected readonly log: AnsiLogger;
16
+ private readonly client;
17
+ constructor(ctx: DeviceHandlerContext);
18
+ addClusters(endpoint: MatterbridgeEndpoint): void;
19
+ setup(): Promise<void>;
20
+ handleUpdate(state: SwitchState): Promise<void>;
21
+ protected registerCommandHandlers(): void;
22
+ /** Write Leviton state to the Matter attributes. */
23
+ protected applyState(state: SwitchState): Promise<void>;
24
+ protected send(command: SwitchCommand): Promise<void>;
25
+ }
26
+ //# sourceMappingURL=SwitchHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SwitchHandler.d.ts","sourceRoot":"","sources":["../../src/handlers/SwitchHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEtD,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACjF,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAI9E;;;;;GAKG;AACH,qBAAa,aAAc,YAAW,aAAa;IACjD,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,oBAAoB,CAAC;IAClD,uDAAuD;IACvD,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IACrC,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,UAAU,CAAC;IACnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;gBAE3B,GAAG,EAAE,oBAAoB;IAOrC,WAAW,CAAC,QAAQ,EAAE,oBAAoB,GAAG,IAAI;IAI3C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAKtB,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAcrD,SAAS,CAAC,uBAAuB,IAAI,IAAI;IAMzC,oDAAoD;cACpC,UAAU,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;cAS7C,IAAI,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAI5D"}
@@ -0,0 +1,60 @@
1
+ const STATE_KEYS = ['power', 'brightness', 'connected'];
2
+ /**
3
+ * Handler for on/off devices: the in-wall switch, plug-in modules and receptacle.
4
+ *
5
+ * Commands are only sent to Leviton. Matter attributes change when Leviton reports
6
+ * the new state back, so Matter always reflects what the device actually did.
7
+ */
8
+ export class SwitchHandler {
9
+ endpoint;
10
+ /** The last state Leviton reported for this device. */
11
+ device;
12
+ log;
13
+ client;
14
+ constructor(ctx) {
15
+ this.endpoint = ctx.endpoint;
16
+ this.device = { ...ctx.device };
17
+ this.log = ctx.log;
18
+ this.client = ctx.client;
19
+ }
20
+ addClusters(endpoint) {
21
+ endpoint.createDefaultOnOffClusterServer();
22
+ }
23
+ async setup() {
24
+ this.registerCommandHandlers();
25
+ await this.applyState(this.device);
26
+ }
27
+ async handleUpdate(state) {
28
+ const changes = {};
29
+ for (const key of STATE_KEYS) {
30
+ const value = state[key];
31
+ if (value === undefined || value === this.device[key])
32
+ continue;
33
+ Object.assign(changes, { [key]: value });
34
+ }
35
+ if (Object.keys(changes).length === 0)
36
+ return;
37
+ Object.assign(this.device, changes);
38
+ this.log.debug(`${this.device.name}: Leviton reported ${JSON.stringify(changes)}`);
39
+ await this.applyState(changes);
40
+ }
41
+ registerCommandHandlers() {
42
+ this.endpoint.addCommandHandler('on', () => this.send({ power: 'ON' }));
43
+ this.endpoint.addCommandHandler('off', () => this.send({ power: 'OFF' }));
44
+ this.endpoint.addCommandHandler('toggle', () => this.send({ power: this.device.power === 'ON' ? 'OFF' : 'ON' }));
45
+ }
46
+ /** Write Leviton state to the Matter attributes. */
47
+ async applyState(state) {
48
+ if (state.connected !== undefined) {
49
+ await this.endpoint.setAttribute('bridgedDeviceBasicInformation', 'reachable', state.connected, this.log);
50
+ }
51
+ if (state.power !== undefined) {
52
+ await this.endpoint.setAttribute('onOff', 'onOff', state.power === 'ON', this.log);
53
+ }
54
+ }
55
+ async send(command) {
56
+ this.log.debug(`${this.device.name}: sending ${JSON.stringify(command)}`);
57
+ await this.client.updateSwitch(this.device.id, command);
58
+ }
59
+ }
60
+ //# sourceMappingURL=SwitchHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SwitchHandler.js","sourceRoot":"","sources":["../../src/handlers/SwitchHandler.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,GAAG,CAAC,OAAO,EAAE,YAAY,EAAE,WAAW,CAAU,CAAC;AAEjE;;;;;GAKG;AACH,MAAM,OAAO,aAAa;IACL,QAAQ,CAAuB;IAClD,uDAAuD;IACpC,MAAM,CAAY;IAClB,GAAG,CAAa;IAClB,MAAM,CAAgB;IAEvC,YAAY,GAAyB;QACnC,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;QAC7B,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC;QAChC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;IAC3B,CAAC;IAED,WAAW,CAAC,QAA8B;QACxC,QAAQ,CAAC,+BAA+B,EAAE,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,uBAAuB,EAAE,CAAC;QAC/B,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACrC,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,KAAkB;QACnC,MAAM,OAAO,GAAgB,EAAE,CAAC;QAChC,KAAK,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;YAC7B,M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,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;YAClC,MAAM,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,+BAA+B,EAAE,WAAW,EAAE,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;QAC5G,CAAC;QACD,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YAC9B,MAAM,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,KAAK,KAAK,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;QACrF,CAAC;IACH,CAAC;IAES,KAAK,CAAC,IAAI,CAAC,OAAsB;QACzC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,aAAa,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAC1E,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IAC1D,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ import { type DeviceTypeDefinition } from 'matterbridge';
2
+ import type { DeviceHandler, DeviceHandlerContext } from './DeviceHandler.js';
3
+ export declare function createHandler(deviceType: DeviceTypeDefinition, ctx: DeviceHandlerContext): DeviceHandler;
4
+ //# sourceMappingURL=handlerRegistry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handlerRegistry.d.ts","sourceRoot":"","sources":["../../src/handlers/handlerRegistry.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8C,KAAK,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACrG,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAY9E,wBAAgB,aAAa,CAAC,UAAU,EAAE,oBAAoB,EAAE,GAAG,EAAE,oBAAoB,GAAG,aAAa,CAMxG"}
@@ -0,0 +1,15 @@
1
+ import { dimmableLight, onOffLight, onOffPlugInUnit } from 'matterbridge';
2
+ import { DimmerHandler } from './DimmerHandler.js';
3
+ import { SwitchHandler } from './SwitchHandler.js';
4
+ const registry = new Map();
5
+ registry.set(onOffLight, (ctx) => new SwitchHandler(ctx));
6
+ registry.set(onOffPlugInUnit, (ctx) => new SwitchHandler(ctx));
7
+ registry.set(dimmableLight, (ctx) => new DimmerHandler(ctx));
8
+ export function createHandler(deviceType, ctx) {
9
+ const factory = registry.get(deviceType);
10
+ if (!factory) {
11
+ throw new Error(`No handler registered for device type: ${deviceType.name}`);
12
+ }
13
+ return factory(ctx);
14
+ }
15
+ //# sourceMappingURL=handlerRegistry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handlerRegistry.js","sourceRoot":"","sources":["../../src/handlers/handlerRegistry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,eAAe,EAA6B,MAAM,cAAc,CAAC;AAErG,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAInD,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAwC,CAAC;AAEjE,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC;AAC1D,QAAQ,CAAC,GAAG,CAAC,eAAe,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC;AAC/D,QAAQ,CAAC,GAAG,CAAC,aAAa,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC;AAE7D,MAAM,UAAU,aAAa,CAAC,UAAgC,EAAE,GAAyB;IACvF,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACzC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,0CAA0C,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;IAC/E,CAAC;IACD,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC;AACtB,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { PlatformConfig, PlatformMatterbridge } from 'matterbridge';
2
+ import type { AnsiLogger } from 'matterbridge/logger';
3
+ import { LevitonPlatform } from './platform.js';
4
+ export default function initializePlugin(matterbridge: PlatformMatterbridge, log: AnsiLogger, config: PlatformConfig): LevitonPlatform;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACzE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACtC,YAAY,EAAE,oBAAoB,EAClC,GAAG,EAAE,UAAU,EACf,MAAM,EAAE,cAAc,GACrB,eAAe,CAEjB"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import { LevitonPlatform } from './platform.js';
2
+ export default function initializePlugin(matterbridge, log, config) {
3
+ return new LevitonPlatform(matterbridge, log, config);
4
+ }
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACtC,YAAkC,EAClC,GAAe,EACf,MAAsB;IAEtB,OAAO,IAAI,eAAe,CAAC,YAAY,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;AACxD,CAAC"}
@@ -0,0 +1,30 @@
1
+ import type { AnsiLogger } from 'matterbridge/logger';
2
+ import type { IotSwitch, LoginResponse, SwitchCommand } from './types.js';
3
+ /**
4
+ * Leviton rejected the account's credentials. Retrying will not help and repeated
5
+ * failures lock the account, so callers must stop instead of trying again.
6
+ */
7
+ export declare class LevitonAuthError extends Error {
8
+ }
9
+ /** REST client for the My Leviton cloud API. */
10
+ export declare class LevitonClient {
11
+ private readonly email;
12
+ private readonly password;
13
+ private readonly log;
14
+ private session;
15
+ private pendingLogin;
16
+ private loginRejection;
17
+ constructor(email: string, password: string, log: AnsiLogger);
18
+ /** Returns the current login session, logging in first if there is none. */
19
+ getSession(): Promise<LoginResponse>;
20
+ /** Discards the current session and logs in again. */
21
+ refreshSession(): Promise<LoginResponse>;
22
+ /** Lists every device in every residence the account can access. */
23
+ listSwitches(): Promise<IotSwitch[]>;
24
+ /** Sends a power and/or brightness change to a device. */
25
+ updateSwitch(id: number, command: SwitchCommand): Promise<void>;
26
+ private login;
27
+ private request;
28
+ private send;
29
+ }
30
+ //# sourceMappingURL=LevitonClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LevitonClient.d.ts","sourceRoot":"","sources":["../../src/leviton/LevitonClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAoC,aAAa,EAAE,MAAM,YAAY,CAAC;AAK5G;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;CAAG;AAQ9C,gDAAgD;AAChD,qBAAa,aAAa;IAMtB,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,GAAG;IAPtB,OAAO,CAAC,OAAO,CAA8B;IAC7C,OAAO,CAAC,YAAY,CAAuC;IAC3D,OAAO,CAAC,cAAc,CAAiC;gBAGpC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,UAAU;IAGlC,4EAA4E;IACtE,UAAU,IAAI,OAAO,CAAC,aAAa,CAAC;IAS1C,sDAAsD;IAChD,cAAc,IAAI,OAAO,CAAC,aAAa,CAAC;IAK9C,oEAAoE;IAC9D,YAAY,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;IAuB1C,0DAA0D;IACpD,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;YAIvD,KAAK;YAuBL,OAAO;IAerB,OAAO,CAAC,IAAI;CAWb"}
@@ -0,0 +1,111 @@
1
+ const BASE_URL = 'https://my.leviton.com/api';
2
+ const REQUEST_TIMEOUT_MS = 15_000;
3
+ /**
4
+ * Leviton rejected the account's credentials. Retrying will not help and repeated
5
+ * failures lock the account, so callers must stop instead of trying again.
6
+ */
7
+ export class LevitonAuthError extends Error {
8
+ }
9
+ const LOGIN_REJECTIONS = {
10
+ 401: 'Leviton rejected the email or password.',
11
+ 403: 'Leviton has locked the account after too many failed logins. Wait before trying again.',
12
+ 406: 'Two-factor authentication is enabled on the My Leviton account, which this plugin does not support.',
13
+ };
14
+ /** REST client for the My Leviton cloud API. */
15
+ export class LevitonClient {
16
+ email;
17
+ password;
18
+ log;
19
+ session = null;
20
+ pendingLogin = null;
21
+ loginRejection = null;
22
+ constructor(email, password, log) {
23
+ this.email = email;
24
+ this.password = password;
25
+ this.log = log;
26
+ }
27
+ /** Returns the current login session, logging in first if there is none. */
28
+ async getSession() {
29
+ if (this.session)
30
+ return this.session;
31
+ // Share one login between concurrent callers
32
+ this.pendingLogin ??= this.login().finally(() => {
33
+ this.pendingLogin = null;
34
+ });
35
+ return this.pendingLogin;
36
+ }
37
+ /** Discards the current session and logs in again. */
38
+ async refreshSession() {
39
+ this.session = null;
40
+ return this.getSession();
41
+ }
42
+ /** Lists every device in every residence the account can access. */
43
+ async listSwitches() {
44
+ const { userId } = await this.getSession();
45
+ const permissions = await this.request('GET', `/Person/${userId}/residentialPermissions`);
46
+ const residenceIds = new Set();
47
+ for (const permission of permissions) {
48
+ if (permission.residenceId != null)
49
+ residenceIds.add(permission.residenceId);
50
+ if (permission.residentialAccountId != null) {
51
+ const residences = await this.request('GET', `/ResidentialAccounts/${permission.residentialAccountId}/residences`);
52
+ for (const residence of residences)
53
+ residenceIds.add(residence.id);
54
+ }
55
+ }
56
+ const switches = [];
57
+ for (const residenceId of residenceIds) {
58
+ switches.push(...(await this.request('GET', `/Residences/${residenceId}/iotSwitches`)));
59
+ }
60
+ return switches;
61
+ }
62
+ /** Sends a power and/or brightness change to a device. */
63
+ async updateSwitch(id, command) {
64
+ await this.request('PUT', `/IotSwitches/${id}`, command);
65
+ }
66
+ async login() {
67
+ // Never repeat a login Leviton has already rejected: repeated failures lock the account
68
+ if (this.loginRejection)
69
+ throw this.loginRejection;
70
+ this.log.info('Logging in to My Leviton');
71
+ const response = await this.send('POST', '/Person/login?include=user', {
72
+ email: this.email,
73
+ password: this.password,
74
+ });
75
+ const rejection = LOGIN_REJECTIONS[response.status];
76
+ if (rejection) {
77
+ this.loginRejection = new LevitonAuthError(rejection);
78
+ throw this.loginRejection;
79
+ }
80
+ if (!response.ok) {
81
+ throw new Error(`My Leviton login failed with HTTP ${response.status}`);
82
+ }
83
+ this.session = (await response.json());
84
+ return this.session;
85
+ }
86
+ async request(method, path, body) {
87
+ let session = await this.getSession();
88
+ let response = await this.send(method, path, body, session.id);
89
+ if (response.status === 401) {
90
+ this.log.info('My Leviton session expired, logging in again');
91
+ session = await this.refreshSession();
92
+ response = await this.send(method, path, body, session.id);
93
+ }
94
+ if (!response.ok) {
95
+ throw new Error(`My Leviton request ${method} ${path} failed with HTTP ${response.status}`);
96
+ }
97
+ return (await response.json());
98
+ }
99
+ send(method, path, body, token) {
100
+ const headers = { 'content-type': 'application/json' };
101
+ if (token)
102
+ headers.authorization = token;
103
+ return fetch(`${BASE_URL}${path}`, {
104
+ method,
105
+ headers,
106
+ body: body === undefined ? undefined : JSON.stringify(body),
107
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
108
+ });
109
+ }
110
+ }
111
+ //# sourceMappingURL=LevitonClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LevitonClient.js","sourceRoot":"","sources":["../../src/leviton/LevitonClient.ts"],"names":[],"mappings":"AAGA,MAAM,QAAQ,GAAG,4BAA4B,CAAC;AAC9C,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAElC;;;GAGG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;CAAG;AAE9C,MAAM,gBAAgB,GAA2B;IAC/C,GAAG,EAAE,yCAAyC;IAC9C,GAAG,EAAE,wFAAwF;IAC7F,GAAG,EAAE,qGAAqG;CAC3G,CAAC;AAEF,gDAAgD;AAChD,MAAM,OAAO,aAAa;IAML;IACA;IACA;IAPX,OAAO,GAAyB,IAAI,CAAC;IACrC,YAAY,GAAkC,IAAI,CAAC;IACnD,cAAc,GAA4B,IAAI,CAAC;IAEvD,YACmB,KAAa,EACb,QAAgB,EAChB,GAAe;QAFf,UAAK,GAAL,KAAK,CAAQ;QACb,aAAQ,GAAR,QAAQ,CAAQ;QAChB,QAAG,GAAH,GAAG,CAAY;IAC/B,CAAC;IAEJ,4EAA4E;IAC5E,KAAK,CAAC,UAAU;QACd,IAAI,IAAI,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC,OAAO,CAAC;QACtC,6CAA6C;QAC7C,IAAI,CAAC,YAAY,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE;YAC9C,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;QAC3B,CAAC,CAAC,CAAC;QACH,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED,sDAAsD;IACtD,KAAK,CAAC,cAAc;QAClB,IAAI,CAAC,OAAO,GAAG,IAAI,C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AAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC;QAC7D,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,sBAAsB,MAAM,IAAI,IAAI,qBAAqB,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;QAC9F,CAAC;QACD,OAAO,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAM,CAAC;IACtC,CAAC;IAEO,IAAI,CAAC,MAAc,EAAE,IAAY,EAAE,IAAc,EAAE,KAAc;QACvE,MAAM,OAAO,GAA2B,EAAE,cAAc,EAAE,kBAAkB,EAAE,CAAC;QAC/E,IAAI,KAAK;YAAE,OAAO,CAAC,aAAa,GAAG,KAAK,CAAC;QAEzC,OAAO,KAAK,CAAC,GAAG,QAAQ,GAAG,IAAI,EAAE,EAAE;YACjC,MAAM;YACN,OAAO;YACP,IAAI,EAAE,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;YAC3D,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,kBAAkB,CAAC;SAChD,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,38 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import type { AnsiLogger } from 'matterbridge/logger';
3
+ import { type LevitonClient } from './LevitonClient.js';
4
+ import type { SwitchState } from './types.js';
5
+ export interface LevitonSocketEvents {
6
+ /** The socket is authenticated and subscribed. Fires again after every reconnect. */
7
+ ready: [];
8
+ deviceUpdated: [deviceId: number, state: SwitchState];
9
+ }
10
+ export interface LevitonSocket {
11
+ on<K extends keyof LevitonSocketEvents>(event: K, listener: (...args: LevitonSocketEvents[K]) => void): this;
12
+ emit<K extends keyof LevitonSocketEvents>(event: K, ...args: LevitonSocketEvents[K]): boolean;
13
+ }
14
+ /** Receives push notifications about device state changes from the My Leviton cloud. */
15
+ export declare class LevitonSocket extends EventEmitter {
16
+ private readonly client;
17
+ private readonly log;
18
+ private ws;
19
+ private ready;
20
+ private stopped;
21
+ private needsFreshLogin;
22
+ private reconnectAttempts;
23
+ private reconnectTimer;
24
+ private pingTimer;
25
+ private readonly deviceIds;
26
+ constructor(client: LevitonClient, log: AnsiLogger);
27
+ /** Opens the socket and keeps it open, reconnecting as needed, until `stop()` is called. */
28
+ start(): void;
29
+ stop(): void;
30
+ /** Asks for push notifications about a device, now and after every reconnect. */
31
+ subscribe(deviceId: number): void;
32
+ private open;
33
+ private handleMessage;
34
+ private sendSubscribe;
35
+ private stopPinging;
36
+ private scheduleReconnect;
37
+ }
38
+ //# sourceMappingURL=LevitonSocket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LevitonSocket.d.ts","sourceRoot":"","sources":["../../src/leviton/LevitonSocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAoB,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,KAAK,EAAgC,WAAW,EAAE,MAAM,YAAY,CAAC;AAS5E,MAAM,WAAW,mBAAmB;IAClC,qFAAqF;IACrF,KAAK,EAAE,EAAE,CAAC;IACV,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC;CACvD;AAGD,MAAM,WAAW,aAAa;IAC5B,EAAE,CAAC,CAAC,SAAS,MAAM,mBAAmB,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,mBAAmB,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,IAAI,CAAC;IAC7G,IAAI,CAAC,CAAC,SAAS,MAAM,mBAAmB,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;CAC/F;AAED,wFAAwF;AAExF,qBAAa,aAAc,SAAQ,YAAY;IAW3C,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,GAAG;IAXtB,OAAO,CAAC,EAAE,CAA0B;IACpC,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,OAAO,CAAQ;IACvB,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,iBAAiB,CAAK;IAC9B,OAAO,CAAC,cAAc,CAA8C;IACpE,OAAO,CAAC,SAAS,CAA+C;IAChE,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;gBAG5B,MAAM,EAAE,aAAa,EACrB,GAAG,EAAE,UAAU;IAKlC,4FAA4F;IAC5F,KAAK,IAAI,IAAI;IAKb,IAAI,IAAI,IAAI;IAgBZ,iFAAiF;IACjF,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;YAKnB,IAAI;IA8BlB,OAAO,CAAC,aAAa;IAsBrB,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,WAAW;IAKnB,OAAO,CAAC,iBAAiB;CAY1B"}
@@ -0,0 +1,129 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import WebSocket from 'ws';
3
+ import { LevitonAuthError } from './LevitonClient.js';
4
+ const SOCKET_URL = 'wss://my.leviton.com/socket/websocket';
5
+ const SOCKET_ORIGIN = 'https://my.leviton.com';
6
+ const PING_INTERVAL_MS = 30_000;
7
+ const MAX_RECONNECT_DELAY_MS = 60_000;
8
+ /** Close code Leviton uses when it rejects the session token. */
9
+ const CLOSE_CODE_AUTH_REJECTED = 1008;
10
+ /** Receives push notifications about device state changes from the My Leviton cloud. */
11
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
12
+ export class LevitonSocket extends EventEmitter {
13
+ client;
14
+ log;
15
+ ws = null;
16
+ ready = false;
17
+ stopped = true;
18
+ needsFreshLogin = false;
19
+ reconnectAttempts = 0;
20
+ reconnectTimer = null;
21
+ pingTimer = null;
22
+ deviceIds = new Set();
23
+ constructor(client, log) {
24
+ super();
25
+ this.client = client;
26
+ this.log = log;
27
+ }
28
+ /** Opens the socket and keeps it open, reconnecting as needed, until `stop()` is called. */
29
+ start() {
30
+ this.stopped = false;
31
+ void this.open();
32
+ }
33
+ stop() {
34
+ this.stopped = true;
35
+ if (this.reconnectTimer)
36
+ clearTimeout(this.reconnectTimer);
37
+ this.reconnectTimer = null;
38
+ this.stopPinging();
39
+ this.ready = false;
40
+ if (this.ws) {
41
+ this.ws.removeAllListeners();
42
+ // terminate() can emit 'error' while the socket is still connecting
43
+ this.ws.on('error', () => { });
44
+ this.ws.terminate();
45
+ this.ws = null;
46
+ }
47
+ }
48
+ /** Asks for push notifications about a device, now and after every reconnect. */
49
+ subscribe(deviceId) {
50
+ this.deviceIds.add(deviceId);
51
+ if (this.ready)
52
+ this.sendSubscribe(deviceId);
53
+ }
54
+ async open() {
55
+ let session;
56
+ try {
57
+ session = this.needsFreshLogin ? await this.client.refreshSession() : await this.client.getSession();
58
+ this.needsFreshLogin = false;
59
+ }
60
+ catch (err) {
61
+ this.log.error(`Cannot open the My Leviton socket: ${err.message}`);
62
+ if (!(err instanceof LevitonAuthError))
63
+ this.scheduleReconnect();
64
+ return;
65
+ }
66
+ if (this.stopped)
67
+ return;
68
+ this.log.debug(`Connecting to ${SOCKET_URL}`);
69
+ const ws = new WebSocket(SOCKET_URL, { origin: SOCKET_ORIGIN });
70
+ this.ws = ws;
71
+ ws.on('open', () => {
72
+ this.pingTimer = setInterval(() => ws.ping(), PING_INTERVAL_MS);
73
+ });
74
+ ws.on('message', (data) => this.handleMessage(data.toString(), session));
75
+ ws.on('error', (err) => this.log.warn(`My Leviton socket error: ${err.message}`));
76
+ ws.on('close', (code) => {
77
+ this.log.warn(`My Leviton socket closed (code ${code})`);
78
+ this.ready = false;
79
+ this.stopPinging();
80
+ if (code === CLOSE_CODE_AUTH_REJECTED)
81
+ this.needsFreshLogin = true;
82
+ this.scheduleReconnect();
83
+ });
84
+ }
85
+ handleMessage(text, session) {
86
+ let message;
87
+ try {
88
+ message = JSON.parse(text);
89
+ }
90
+ catch {
91
+ this.log.debug(`Ignoring unparseable socket message: ${text}`);
92
+ return;
93
+ }
94
+ if (message.type === 'challenge') {
95
+ this.ws?.send(JSON.stringify({ token: session }));
96
+ }
97
+ else if (message.type === 'status' && message.status === 'ready') {
98
+ this.log.info('My Leviton socket ready');
99
+ this.ready = true;
100
+ this.reconnectAttempts = 0;
101
+ for (const deviceId of this.deviceIds)
102
+ this.sendSubscribe(deviceId);
103
+ this.emit('ready');
104
+ }
105
+ else if (message.type === 'notification') {
106
+ this.emit('deviceUpdated', message.notification.modelId, message.notification.data ?? {});
107
+ }
108
+ }
109
+ sendSubscribe(deviceId) {
110
+ this.ws?.send(JSON.stringify({ type: 'subscribe', subscription: { modelName: 'IotSwitch', modelId: deviceId } }));
111
+ }
112
+ stopPinging() {
113
+ if (this.pingTimer)
114
+ clearInterval(this.pingTimer);
115
+ this.pingTimer = null;
116
+ }
117
+ scheduleReconnect() {
118
+ if (this.stopped || this.reconnectTimer)
119
+ return;
120
+ const delay = Math.min(1000 * 2 ** this.reconnectAttempts, MAX_RECONNECT_DELAY_MS);
121
+ this.reconnectAttempts++;
122
+ this.log.info(`Reconnecting to the My Leviton socket in ${delay}ms (attempt ${this.reconnectAttempts})`);
123
+ this.reconnectTimer = setTimeout(() => {
124
+ this.reconnectTimer = null;
125
+ void this.open();
126
+ }, delay);
127
+ }
128
+ }
129
+ //# sourceMappingURL=LevitonSocket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LevitonSocket.js","sourceRoot":"","sources":["../../src/leviton/LevitonSocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,SAAS,MAAM,IAAI,CAAC;AAE3B,OAAO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AI,CAAC,GAAG,CAAC,IAAI,CAAC,4CAA4C,KAAK,eAAe,IAAI,CAAC,iBAAiB,GAAG,CAAC,CAAC;QAEzG,IAAI,CAAC,cAAc,GAAG,UAAU,CAAC,GAAG,EAAE;YACpC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;YAC3B,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC;QACnB,CAAC,EAAE,KAAK,CAAC,CAAC;IACZ,CAAC;CACF"}
@@ -0,0 +1,52 @@
1
+ /** Response from `POST /Person/login`. The whole object is also the websocket auth token. */
2
+ export interface LoginResponse {
3
+ /** The access token, sent as the raw `authorization` header. */
4
+ id: string;
5
+ userId: string;
6
+ ttl?: number;
7
+ [key: string]: unknown;
8
+ }
9
+ export type Power = 'ON' | 'OFF';
10
+ /** A device record from the My Leviton API. Every device type is an "IotSwitch". */
11
+ export interface IotSwitch {
12
+ id: number;
13
+ name: string;
14
+ model: string;
15
+ serial?: string;
16
+ mac?: string;
17
+ version?: string;
18
+ power: Power;
19
+ /** 1-100. Only meaningful for dimmers. */
20
+ brightness?: number;
21
+ minLevel?: number;
22
+ maxLevel?: number;
23
+ /** False when the device has lost its connection to Leviton's cloud. */
24
+ connected?: boolean;
25
+ }
26
+ /** The state fields this plugin tracks. Push notifications and polls both produce this shape. */
27
+ export type SwitchState = Partial<Pick<IotSwitch, 'power' | 'brightness' | 'connected'>>;
28
+ /** Body of `PUT /IotSwitches/{id}`. */
29
+ export interface SwitchCommand {
30
+ power?: Power;
31
+ brightness?: number;
32
+ }
33
+ export interface ResidentialPermission {
34
+ residentialAccountId?: number | null;
35
+ residenceId?: number | null;
36
+ }
37
+ export interface Residence {
38
+ id: number;
39
+ }
40
+ export type SocketMessage = {
41
+ type: 'challenge';
42
+ } | {
43
+ type: 'status';
44
+ status: string;
45
+ } | {
46
+ type: 'notification';
47
+ notification: {
48
+ modelId: number;
49
+ data: SwitchState;
50
+ };
51
+ };
52
+ //# sourceMappingURL=types.d.ts.map
@@ -0,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,aAAa,GACrB;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,GACrB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,YAAY,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,WAAW,CAAA;KAAE,CAAA;CAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/leviton/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import { type DeviceTypeDefinition } from 'matterbridge';
2
+ /** Returns the Matter device type for a Leviton model, or undefined if the model is not supported. */
3
+ export declare function mapModel(model: string): DeviceTypeDefinition | undefined;
4
+ //# sourceMappingURL=DeviceMapper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeviceMapper.d.ts","sourceRoot":"","sources":["../../src/mapper/DeviceMapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8C,KAAK,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAsBrG,sGAAsG;AACtG,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,oBAAoB,GAAG,SAAS,CAIxE"}
@@ -0,0 +1,27 @@
1
+ import { dimmableLight, onOffLight, onOffPlugInUnit } from 'matterbridge';
2
+ /**
3
+ * First-generation Decora Smart Wi-Fi models this plugin bridges, and the Matter
4
+ * device type each one becomes. Anything not listed is ignored: second-generation
5
+ * devices speak Matter natively, and the fan and scene controllers are not supported.
6
+ */
7
+ const DEVICE_TYPES_BY_MODEL = {
8
+ // In-wall dimmers
9
+ DW6HD: dimmableLight,
10
+ DW1KD: dimmableLight,
11
+ DWVAA: dimmableLight,
12
+ // Plug-in dimmer
13
+ DW3HL: dimmableLight,
14
+ // In-wall switch
15
+ DW15S: onOffLight,
16
+ // Plug-in modules and receptacle
17
+ DW15P: onOffPlugInUnit,
18
+ DW15A: onOffPlugInUnit,
19
+ DW15R: onOffPlugInUnit,
20
+ };
21
+ /** Returns the Matter device type for a Leviton model, or undefined if the model is not supported. */
22
+ export function mapModel(model) {
23
+ // The API can report a model with a revision suffix, e.g. "DW6HD-1BZ"
24
+ const baseModel = model.trim().toUpperCase().split('-')[0];
25
+ return DEVICE_TYPES_BY_MODEL[baseModel];
26
+ }
27
+ //# sourceMappingURL=DeviceMapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeviceMapper.js","sourceRoot":"","sources":["../../src/mapper/DeviceMapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,eAAe,EAA6B,MAAM,cAAc,CAAC;AAErG;;;;GAIG;AACH,MAAM,qBAAqB,GAAyC;IAClE,kBAAkB;IAClB,KAAK,EAAE,aAAa;IACpB,KAAK,EAAE,aAAa;IACpB,KAAK,EAAE,aAAa;IACpB,iBAAiB;IACjB,KAAK,EAAE,aAAa;IACpB,iBAAiB;IACjB,KAAK,EAAE,UAAU;IACjB,iCAAiC;IACjC,KAAK,EAAE,eAAe;IACtB,KAAK,EAAE,eAAe;IACtB,KAAK,EAAE,eAAe;CACvB,CAAC;AAEF,sGAAsG;AACtG,MAAM,UAAU,QAAQ,CAAC,KAAa;IACpC,sEAAsE;IACtE,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3D,OAAO,qBAAqB,CAAC,SAAS,CAAC,CAAC;AAC1C,CAAC"}
@@ -0,0 +1,8 @@
1
+ /** Convert Leviton brightness (1-100) to Matter level (1-254). Matter's minimum level is 1. */
2
+ export declare function levitonBrightnessToMatterLevel(brightness: number): number;
3
+ /**
4
+ * Convert Matter level (1-254) to Leviton brightness, kept within the dimmer's
5
+ * configured range (`minLevel`..`maxLevel`, at most 1-100).
6
+ */
7
+ export declare function matterLevelToLevitonBrightness(level: number, minLevel?: number, maxLevel?: number): number;
8
+ //# sourceMappingURL=ValueConverter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ValueConverter.d.ts","sourceRoot":"","sources":["../../src/mapper/ValueConverter.ts"],"names":[],"mappings":"AAGA,+FAA+F;AAC/F,wBAAgB,8BAA8B,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAGzE;AAED;;;GAGG;AACH,wBAAgB,8BAA8B,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,SAAI,EAAE,QAAQ,SAAyB,GAAG,MAAM,CAGrH"}
@@ -0,0 +1,19 @@
1
+ const MATTER_MAX_LEVEL = 254;
2
+ const LEVITON_MAX_BRIGHTNESS = 100;
3
+ /** Convert Leviton brightness (1-100) to Matter level (1-254). Matter's minimum level is 1. */
4
+ export function levitonBrightnessToMatterLevel(brightness) {
5
+ const level = Math.round((brightness / LEVITON_MAX_BRIGHTNESS) * MATTER_MAX_LEVEL);
6
+ return clamp(level, 1, MATTER_MAX_LEVEL);
7
+ }
8
+ /**
9
+ * Convert Matter level (1-254) to Leviton brightness, kept within the dimmer's
10
+ * configured range (`minLevel`..`maxLevel`, at most 1-100).
11
+ */
12
+ export function matterLevelToLevitonBrightness(level, minLevel = 1, maxLevel = LEVITON_MAX_BRIGHTNESS) {
13
+ const brightness = Math.round((level / MATTER_MAX_LEVEL) * LEVITON_MAX_BRIGHTNESS);
14
+ return clamp(brightness, Math.max(minLevel, 1), Math.min(maxLevel, LEVITON_MAX_BRIGHTNESS));
15
+ }
16
+ function clamp(value, min, max) {
17
+ return Math.min(Math.max(value, min), max);
18
+ }
19
+ //# sourceMappingURL=ValueConverter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ValueConverter.js","sourceRoot":"","sources":["../../src/mapper/ValueConverter.ts"],"names":[],"mappings":"AAAA,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAEnC,+FAA+F;AAC/F,MAAM,UAAU,8BAA8B,CAAC,UAAkB;IAC/D,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,UAAU,GAAG,sBAAsB,CAAC,GAAG,gBAAgB,CAAC,CAAC;IACnF,OAAO,KAAK,CAAC,KAAK,EAAE,CAAC,EAAE,gBAAgB,CAAC,CAAC;AAC3C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,8BAA8B,CAAC,KAAa,EAAE,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,sBAAsB;IAC3G,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,gBAAgB,CAAC,GAAG,sBAAsB,CAAC,CAAC;IACnF,OAAO,KAAK,CAAC,UAAU,EAAE,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,sBAAsB,CAAC,CAAC,CAAC;AAC9F,CAAC;AAED,SAAS,KAAK,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW;IACpD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,26 @@
1
+ import { MatterbridgeDynamicPlatform, type PlatformConfig, type PlatformMatterbridge } from 'matterbridge';
2
+ import type { AnsiLogger } from 'matterbridge/logger';
3
+ export declare class LevitonPlatform extends MatterbridgeDynamicPlatform {
4
+ private client;
5
+ private socket;
6
+ private pollTimer;
7
+ private refreshing;
8
+ /** Registered devices by Leviton device id. */
9
+ private devices;
10
+ /** Leviton device ids already reported as not bridged, so each is only logged once. */
11
+ private skippedDeviceIds;
12
+ constructor(matterbridge: PlatformMatterbridge, log: AnsiLogger, config: PlatformConfig);
13
+ onStart(reason?: string): Promise<void>;
14
+ onShutdown(reason?: string): Promise<void>;
15
+ /**
16
+ * Fetches every device from Leviton, registers new ones and syncs the state of known ones.
17
+ * Returns false when Leviton rejected the credentials, which means updates must stop.
18
+ */
19
+ private refresh;
20
+ private stopUpdates;
21
+ private registerSwitch;
22
+ /** Returns why a device should not be bridged, or undefined if it should be. */
23
+ private getSkipReason;
24
+ private onDeviceUpdated;
25
+ }
26
+ //# sourceMappingURL=platform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../src/platform.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAE3B,KAAK,cAAc,EACnB,KAAK,oBAAoB,EAC1B,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAkBtD,qBAAa,eAAgB,SAAQ,2BAA2B;IAC9D,OAAO,CAAC,MAAM,CAA8B;IAC5C,OAAO,CAAC,MAAM,CAA8B;IAC5C,OAAO,CAAC,SAAS,CAA+C;IAChE,OAAO,CAAC,UAAU,CAAS;IAC3B,+CAA+C;IAC/C,OAAO,CAAC,OAAO,CAAyC;IACxD,uFAAuF;IACvF,OAAO,CAAC,gBAAgB,CAAqB;gBAEjC,YAAY,EAAE,oBAAoB,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,cAAc;IAKxE,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBvC,UAAU,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOzD;;;OAGG;YACW,OAAO;IA2BrB,OAAO,CAAC,WAAW;YAOL,cAAc;IAwC5B,gFAAgF;IAChF,OAAO,CAAC,aAAa;IAYrB,OAAO,CAAC,eAAe;CAQxB"}
@@ -0,0 +1,137 @@
1
+ import { MatterbridgeDynamicPlatform, MatterbridgeEndpoint, } from 'matterbridge';
2
+ import { LevitonAuthError, LevitonClient } from './leviton/LevitonClient.js';
3
+ import { LevitonSocket } from './leviton/LevitonSocket.js';
4
+ import { mapModel } from './mapper/DeviceMapper.js';
5
+ import { createHandler } from './handlers/handlerRegistry.js';
6
+ /** Push notifications are the main source of updates. Polling catches anything they miss. */
7
+ const POLL_INTERVAL_MS = 60_000;
8
+ /** Matter test vendor id. Leviton's bridged devices have no vendor id of their own here. */
9
+ const VENDOR_ID = 0xfff1;
10
+ export class LevitonPlatform extends MatterbridgeDynamicPlatform {
11
+ client = null;
12
+ socket = null;
13
+ pollTimer = null;
14
+ refreshing = false;
15
+ /** Registered devices by Leviton device id. */
16
+ devices = new Map();
17
+ /** Leviton device ids already reported as not bridged, so each is only logged once. */
18
+ skippedDeviceIds = new Set();
19
+ constructor(matterbridge, log, config) {
20
+ super(matterbridge, log, config);
21
+ this.log.info('Leviton Platform created');
22
+ }
23
+ async onStart(reason) {
24
+ this.log.info(`Starting Leviton Platform (reason: ${reason ?? 'none'})`);
25
+ const email = this.config.email;
26
+ const password = this.config.password;
27
+ if (!email || !password) {
28
+ this.log.error('The My Leviton email and password must be set in the plugin configuration.');
29
+ return;
30
+ }
31
+ this.client = new LevitonClient(email, password, this.log);
32
+ this.socket = new LevitonSocket(this.client, this.log);
33
+ this.socket.on('deviceUpdated', (deviceId, state) => this.onDeviceUpdated(deviceId, state));
34
+ this.socket.on('ready', () => void this.refresh());
35
+ if (await this.refresh()) {
36
+ this.socket.start();
37
+ this.pollTimer = setInterval(() => void this.refresh(), POLL_INTERVAL_MS);
38
+ }
39
+ }
40
+ async onShutdown(reason) {
41
+ this.log.info(`Shutting down Leviton Platform (reason: ${reason ?? 'none'})`);
42
+ this.stopUpdates();
43
+ this.client = null;
44
+ this.devices.clear();
45
+ }
46
+ /**
47
+ * Fetches every device from Leviton, registers new ones and syncs the state of known ones.
48
+ * Returns false when Leviton rejected the credentials, which means updates must stop.
49
+ */
50
+ async refresh() {
51
+ if (!this.client)
52
+ return false;
53
+ // A poll and a socket reconnect can ask at the same time; one fetch serves both
54
+ if (this.refreshing)
55
+ return true;
56
+ this.refreshing = true;
57
+ try {
58
+ for (const device of await this.client.listSwitches()) {
59
+ const registration = this.devices.get(device.id);
60
+ if (registration) {
61
+ await registration.handler.handleUpdate(device);
62
+ }
63
+ else {
64
+ await this.registerSwitch(device);
65
+ }
66
+ }
67
+ }
68
+ catch (err) {
69
+ this.log.error(`Failed to fetch devices from My Leviton: ${err.message}`);
70
+ if (err instanceof LevitonAuthError) {
71
+ this.log.error('Fix the plugin configuration and restart the plugin.');
72
+ this.stopUpdates();
73
+ return false;
74
+ }
75
+ }
76
+ finally {
77
+ this.refreshing = false;
78
+ }
79
+ return true;
80
+ }
81
+ stopUpdates() {
82
+ if (this.pollTimer)
83
+ clearInterval(this.pollTimer);
84
+ this.pollTimer = null;
85
+ this.socket?.stop();
86
+ this.socket = null;
87
+ }
88
+ async registerSwitch(device) {
89
+ const skipReason = this.getSkipReason(device);
90
+ if (skipReason) {
91
+ if (!this.skippedDeviceIds.has(device.id)) {
92
+ this.skippedDeviceIds.add(device.id);
93
+ this.log.info(`Skipping ${device.name} (${device.model}): ${skipReason}`);
94
+ }
95
+ return;
96
+ }
97
+ const deviceType = mapModel(device.model);
98
+ const serialNumber = `leviton-${device.serial ?? device.id}`;
99
+ const uniqueId = serialNumber;
100
+ if (this.getDeviceByUniqueId(uniqueId)) {
101
+ this.log.debug(`Device ${uniqueId} already registered, skipping`);
102
+ return;
103
+ }
104
+ const endpoint = new MatterbridgeEndpoint(deviceType, { id: uniqueId }, this.config.debug);
105
+ endpoint.createDefaultBridgedDeviceBasicInformationClusterServer(device.name, serialNumber, VENDOR_ID, 'Leviton', device.model);
106
+ const handler = createHandler(deviceType, { endpoint, device, log: this.log, client: this.client });
107
+ handler.addClusters(endpoint);
108
+ // Register with matterbridge BEFORE setting initial state (endpoint must be active)
109
+ await this.registerDevice(endpoint);
110
+ await handler.setup();
111
+ this.devices.set(device.id, { endpoint, handler });
112
+ this.socket?.subscribe(device.id);
113
+ this.log.info(`Registered: ${device.name} (${device.model} as ${deviceType.name}) [${uniqueId}]`);
114
+ }
115
+ /** Returns why a device should not be bridged, or undefined if it should be. */
116
+ getSkipReason(device) {
117
+ if (!mapModel(device.model))
118
+ return 'not a supported first-generation Decora Wi-Fi model';
119
+ const includeDevices = this.config.includeDevices ?? [];
120
+ const excludeDevices = this.config.excludeDevices ?? [];
121
+ const matches = (list) => list.includes(device.name) || (!!device.serial && list.includes(device.serial));
122
+ if (includeDevices.length > 0 && !matches(includeDevices))
123
+ return 'not in includeDevices';
124
+ if (matches(excludeDevices))
125
+ return 'listed in excludeDevices';
126
+ return undefined;
127
+ }
128
+ onDeviceUpdated(deviceId, state) {
129
+ const registration = this.devices.get(deviceId);
130
+ if (!registration)
131
+ return;
132
+ registration.handler.handleUpdate(state).catch((err) => {
133
+ this.log.error(`Error handling update for device ${deviceId}: ${err.message}`);
134
+ });
135
+ }
136
+ }
137
+ //# sourceMappingURL=platform.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platform.js","sourceRoot":"","sources":["../src/platform.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,GAGrB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAE9D,6FAA6F;AAC7F,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAChC,4FAA4F;AAC5F,MAAM,SAAS,GAAG,MAAM,CAAC;AAOzB,MAAM,OAAO,eAAgB,SAAQ,2BAA2B;IACtD,MAAM,GAAyB,IAAI,CAAC;IACpC,MAAM,GAAyB,IAAI,CAAC;IACpC,SAAS,GAA0C,IAAI,CAAC;IACxD,UAAU,GAAG,KAAK,CAAC;I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AAI,EAAE,CAAC;QAClF,MAAM,OAAO,GAAG,CAAC,IAAc,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;QAEpH,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC;YAAE,OAAO,uBAAuB,CAAC;QAC1F,IAAI,OAAO,CAAC,cAAc,CAAC;YAAE,OAAO,0BAA0B,CAAC;QAC/D,OAAO,SAAS,CAAC;IACnB,CAAC;IAEO,eAAe,CAAC,QAAgB,EAAE,KAAkB;QAC1D,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY;YAAE,OAAO;QAE1B,YAAY,CAAC,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACrD,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,oCAAoC,QAAQ,KAAM,GAAa,CAAC,OAAO,EAAE,CAAC,CAAC;QAC5F,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,31 @@
1
+ {
2
+ "type": "object",
3
+ "properties": {
4
+ "email": {
5
+ "type": "string",
6
+ "description": "Email address of your My Leviton account"
7
+ },
8
+ "password": {
9
+ "type": "string",
10
+ "description": "Password of your My Leviton account (two-factor authentication must be off)",
11
+ "ui:widget": "password"
12
+ },
13
+ "includeDevices": {
14
+ "type": "array",
15
+ "items": { "type": "string" },
16
+ "description": "Only bridge the devices with these names or serial numbers (leave empty to include all)",
17
+ "default": []
18
+ },
19
+ "excludeDevices": {
20
+ "type": "array",
21
+ "items": { "type": "string" },
22
+ "description": "Do not bridge the devices with these names or serial numbers",
23
+ "default": []
24
+ },
25
+ "debug": {
26
+ "type": "boolean",
27
+ "description": "Enable debug logging for the plugin",
28
+ "default": false
29
+ }
30
+ }
31
+ }
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "matterbridge-leviton",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "description": "Leviton plugin for Matterbridge",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "packageManager": "pnpm@12.23.0",
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "watch": "tsc --watch",
12
+ "lint": "eslint src/",
13
+ "lint:fix": "eslint --fix src/",
14
+ "format": "prettier --write \"src/**/*.ts\"",
15
+ "format:check": "prettier --check \"src/**/*.ts\"",
16
+ "test": "vitest run",
17
+ "test:watch": "vitest",
18
+ "dev:link": "mkdir -p node_modules && ln -sfn \"$(npm root -g)/matterbridge\" node_modules/matterbridge"
19
+ },
20
+ "keywords": [
21
+ "matterbridge",
22
+ "matter",
23
+ "leviton",
24
+ "decora",
25
+ "smart-home",
26
+ "iot"
27
+ ],
28
+ "author": "Andy Alm",
29
+ "license": "MIT",
30
+ "files": [
31
+ "dist/",
32
+ "matterbridge-leviton.schema.json",
33
+ "CHANGELOG.md",
34
+ "LICENSE",
35
+ "README.md"
36
+ ],
37
+ "engines": {
38
+ "node": ">=22.13.0"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/andyalm/matterbridge-leviton.git"
43
+ },
44
+ "homepage": "https://github.com/andyalm/matterbridge-leviton#readme",
45
+ "bugs": {
46
+ "url": "https://github.com/andyalm/matterbridge-leviton/issues"
47
+ },
48
+ "dependencies": {
49
+ "ws": "^8.23.0"
50
+ },
51
+ "devDependencies": {
52
+ "@eslint/js": "^10.0.1",
53
+ "@types/node": "^22.20.7",
54
+ "@types/ws": "^8.18.2",
55
+ "eslint": "^10.15.0",
56
+ "eslint-config-prettier": "^10.1.8",
57
+ "jiti": "^2.7.0",
58
+ "prettier": "^3.10.1",
59
+ "typescript": "^6.0.3",
60
+ "typescript-eslint": "^8.74.0",
61
+ "vitest": "^5.1.2"
62
+ }
63
+ }