AlCalzone / node-g-homa

Node.js library to control G-Homa WiFi plugs
MIT License
3 stars 0 forks source link

node-g-homa

NodeJS module to control G-Homa WiFi plugs. Functions include:

Credits

Protocol information was adapted from

Big thanks for the original work decoding the protocol!

Usage

Inclusion of new plugs (only works on wireless devices):

const gHoma = require("g-homa");

const discovery = new gHoma.Discovery();
discovery
    .on("inclusion finished", (devices) => {
        // do something with included devices
    })
    .once("ready", () => {
        // start inclusion
        discovery.beginInclusion("psk");
    })
;

You have to supply the WiFi key to the beginInclusion method. By default, inclusion stops after one device was found. The overload beginInclusion("psk", false) keeps finding new plugs for 60s and then returns. Discovery works by sending packets to the broadcast address of a network interface. If you have multiple ones, e.g. LAN and WiFi, you can select the one to use by providing options to the constructor:

const discovery = new gHoma.Discovery({
    networkInterfaceIndex: 0,
});

The devices object contains a table of IP and MAC addresses of the found plugs:

{
    "ip#1": "AABBCCDDEEFF",
    "ip#2": "FFEEDDCCBBAA",
    // and so on...
}

Discovery and configuration of included plugs

const manager = new gHoma.Manager();
manager
    .once("ready", () => {
        // find plugs (promise version)
        manager.findAllPlugs(/* optional duration in ms */)
            .then(plugs => {
                // do something with the plugs
            })
        ;

        // find plugs (async version)
        const plugs = manager.findAllPlugs(/* optional duration in ms */);

        // configure a plug to use the local C&C server
        // async version:
        let success /* boolean */ = manager.configurePlug("plug IP", "server IP", serverPort);

        // restore a plug to use the default external C&C server
        let success /* boolean */ = manager.restorePlug("plug IP");
    })
;

Like with Discovery, you can select the network interface the Manager uses by providing options to the constructor:

const manager = new gHoma.Manager({
    networkInterfaceIndex: 0,
});

Control of configured devices with the local C&C server

const server = new gHoma.Server(/* optional port number */);
// ...
// close the server when you're done
server.close();

The server emits a number of events:

The Plug object looks as follows:

{
    id: string,        // ID of this plug
    ip: string,        // remote IP address
    port: number,      // remote port number
    lastSeen: number,  // last seen (UNIX time)
    online: boolean,   // if the plug is alive or dead
    lastSwitchSource: "unknown" | "remote" | "local", // where the plug was last switched from
    state: boolean,    // if the plug is on or off
    shortmac: string,  // last 3 bytes of the MAC, e.g. DD:EE:FF
    mac: string,       // e.g. AA:BB:CC:DD:EE:FF
}

Manual serial interface

  1. run it from the command line: node build/serial.js
  2. enter an ip address to talk to, or leave the line blank to broadcast to all plugs
    1. enter password HF-A11ASSISTHREAD (default, can be changed)
    2. if the plug responds, confirm the receipt with +ok
    3. get a list of commands with AT+H\r (all commands need to include \r at the end)
    4. ...?
    5. profit

Changelog

1.2.1 (2019-08-15)

1.2.0 (2019-08-09)

1.1.3 (2018-06-28)

1.1.2 (2018-01-28)

1.0.0 (2018-01-11)

0.0.4 (2017-08-31)

0.0.3 (2017-08-07)

0.0.2 (2017-08-05)

0.0.1 (2017-08-05)

License

The MIT License (MIT)

Copyright (c) 2017 AlCalzone d.griesel@gmx.net

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.