GithubHelp home page GithubHelp logo

mr-dwight-schrute / node-homey-rfdriver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from athombv/node-homey-rfdriver

0.0 0.0 0.0 1.3 MB

This module helps developers create RF (433MHz, 868MHz, Infrared) apps for Homey.

Home Page: https://athombv.github.io/node-homey-rfdriver

JavaScript 71.09% HTML 28.91%

node-homey-rfdriver's Introduction

Homey RFDriver

npm Lint NPM Deploy Documentation To GitHub Pages

This module helps developers create RF (433MHz, 868MHz, Infrared) apps for Homey.

This module requires Homey Apps SDK v3.

Documentation

Documentation is available at https://athombv.github.io/node-homey-rfdriver/.

Related Modules

Installation

$ npm install homey-rfdriver

Requirements

Usage

Both your driver and device should extend RFDriver and RFDevice. RFDriver should then reference another class that extends RFSignal.

A typical app might have these files:

/.homeycompose/signals/433/my_signal.json
/lib/MySignal.js (extends RFSignal)
/drivers/my_driver/driver.js (extends RFDriver)
/drivers/my_driver/device.js (extends RFDevice)
/drivers/my_driver/pair/rf_receiver_learn.html
/drivers/my_driver/pair/rf_receiver_add.html
/drivers/my_driver/pair/image.svg

Your driver can be either a transmitter (e.g. a remote) or a receiver (e.g. a light or socket switch).

You should copy the corresponding files from this module's /pair folder to your driver's /pair folder.

RFSignal

Imagine we have a simple 433 MHz protocol that first sends the address (1 byte) and then the on/off state (1 byte, 0x00 for off, 0xFF for on).

Our signal definition must be added to /.homeycompose/signals/433/my_signal.json. Refer to the Signal Documentation for more information on how to create a signal.

Your JSON describes how to encode and decode the low & high signals into bits. The RFSignal you define here will parse these bits (or bytes) from and to JavaScript objects.

/lib/MySignal.js

'use strict';

const { RFSignal, RFError, RFUtil } = require('homey-rfdriver');

// Protocol:
// [ 0x00 - 0xFF, 0x00 | 0xFF ]
// [ address    , state       ]

module.exports = class MySignal extends RFSignal {

  static FREQUENCY = '433';
  static ID = 'my_signal';

  // This method converts a JavaScript object command
  // into a payload for our signal.
  static commandToPayload({ address, state }) {
    if( typeof address !== 'number' )
      throw new RFError('Invalid Address');

    if( typeof state !== 'boolean' )
      throw new RFError('Invalid State');
      
    return [ address, state ? 0x00 : 0xFF ];
  }

  // This method converts a received payload
  // into a JavaScript object command.
  static payloadToCommand(payload) {
    const address = Number(payload[0]);
    const state = Boolean(payload[1]);
    return { address, state };
  }

  // This method takes a command object
  // and returns a device-unique data object
  static commandToDeviceData(command) {
    return {
      address: command.address,
    };
  }

  // This method is invoked when a new receiver is added
  // We can generate a random address, as if someone pressed
  // the button on a remote.
  static createPairCommand() {
    return {
      address: Math.round(Math.random() * 255),
      state: true,
    };
  }

}

RFDriver

/drivers/my_driver/driver.js

const { RFDriver } = require('homey-rfdriver');
const MySignal = require('../../lib/MySignal');

module.exports = class MyDriver extends RFDriver {

  static SIGNAL = MySignal;

}

Transmitter

Copy /pair/rf_transmitter_learn.html to your driver's /pair folder.

Then add this to your /drivers/<driver_id>/driver.compose.json:

"rf433": { // Or `infrared`
  "satelliteMode": true
},
"pair": [
  {
    "id": "rf_transmitter_learn",
    "options": {
      "title": {
        "en": "Press any button"
      },
      "instruction": {
        "en": "Press any button on your device."
      }
    }
  }
]

Receiver

Copy /pair/rf_receiver_learn.html and /pair/rf_receiver_add.html to your driver's /pair folder.

Then add this to your /drivers/<driver_id>/driver.compose.json:

"pair": [
  {
    "id": "rf_receiver_learn",
    "navigation": {
      "next": "rf_receiver_add"
    },
    "options": {
      "title": {
        "en": "Press the button..."
      },
      "instruction": {
        "en": "Press the button on your device once."
      }
    }
  },
  {
    "id": "rf_receiver_add",
    "options": {
      "instruction": {
        "en": "Did the device turn off and on?"
      }
    }
  }
]

Receiver + Learn

Some devices, such as built-in modules, are hard to reach when pairing. For such devices, you can let the user choose whether to generate a new signal, or copy from an existing remote.

Follow the instructions for Receiver, but additionally copy /pair/rf_transmitter_learn.html to your driver's /pair folder.

Then, modify this to your /drivers/<driver_id>/driver.compose.json:

"pair": [
  {
    "id": "rf_receiver_learn",
    "navigation": {
      "next": "rf_receiver_add"
    },
    "options": {
      "title": {
        "en": "Press the button..."
      },
      "instruction": {
        "en": "Press the button on your device once."
      },
      "copyFromRemote": {
        "en": "Copy from Remote"
      }
    }
  },
  {
    "id": "rf_transmitter_learn",
    "options": {
      "instruction": {
        "en": "Press any button on your remote."
      }
    },
    "navigation": {
      "prev": "rf_receiver_learn"
    }
  },
  {
    "id": "rf_receiver_add",
    "navigation": {
      "prev": "rf_receiver_learn"
    },
    "options": {
      "instruction": {
        "en": "Did the device turn off and on?"
      }
    }
  }
]

IR Remote

Copy /pair/rf_ir_remote_learn.html and /pair/rf_ir_remote_add to your driver's /pair folder.

Then add this to your /drivers/<driver_id>/driver.compose.json:

"pair": [
  {
    "id": "rf_ir_remote_learn",
    "navigation": {
      "next": "rf_ir_remote_add"
    },
    "options": {
      "title": {
        "en": "Pair your IR remote",
        "nl": "Koppel je IR afstandsbediening"
      },
      "instruction": {
        "en": "Press next to pair your remote.",
        "nl": "Druk op volgende om je afstandsbediening te koppelen."
      }
    }
  },{
    "id": "rf_ir_remote_add"
  }
]

RFDevice

/drivers/my_driver/device.js

const { RFDevice } = require('homey-rfdriver');

module.exports = class MyDevice extends RFDevice {

  static RX_ENABLED = false; // Set to true for transmitter devices

  static CAPABILITIES = {

    // When the onoff capability is changed by the user,
    // you can assemble a command here.
    // 'data' is your device's data object
    onoff: ({ value, data }) => ({
      address: data.address,
      state: value === true,
    }),
  };

}

node-homey-rfdriver's People

Contributors

baskiers avatar weejewel avatar kyleathom avatar harwinborger avatar christian-athom avatar robinbol avatar tjallingt avatar mrlukasbos avatar matthewwaanders avatar woutervdbrink avatar dependabot[bot] avatar jeroenvollenbrock avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.