This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

homey-wifidriver

1.0.8 • Public • Published

WifiDriver

Library that extends HomeyDriver and HomeyDevice to provide a set of functionality that is commonly used in Homey Apps that support devices over WiFi.

Note: this library is still under development, use at your own risk.

Installation

cd /path/to/com.your.homeyapp/
npm install --save https://github.com/athombv/node-homey-wifidriver
cd node_modules/homey-wifidriver
npm install

Docs

See https://athombv.github.io/node-homey-wifidriver

Example

File: /drivers/mydriver/driver.js

const OAuth2Driver = require('homey-wifidriver').OAuth2Driver;
const ExampleAPI = require('example-api');
 
const oauth2ClientConfig = {
    url: `https://api.example.com/authorize?response_type=code&client_id=${Homey.env.API_CLIENT_ID}&redirect_uri=https://callback.athom.com/oauth2/callback/`,
    tokenEndpoint: 'https://api.example.com/token',
    key: Homey.env.API_CLIENT_ID,
    secret: Homey.env.API_CLIENT_SECRET,
    allowMultipleAccounts: false,
};
 
class myDriver extends OAuth2Driver { // Which extends WifiDriver
    
    onInit(){
        // This will override onPair() to handle OAuth2 pairing
        super.onInit({oauth2ClientConfig});
    }
    
    /**
    * This method will be called if a oauth2ClientConfig is provided onInit. The data object contains
    * an authenticated OAuth2Account instance which will be destroyed automatically after pairing
    * has ended.
        */
    onPairOAuth2ListDevices(data) {
    
            // Create temporary API client with temporary account
            const exampleApiClient = new ExampleAPI({
                oauth2Account: data.oauth2Account,
            });
    
            // Return promise that fetches devices from account
            return exampleApiClient.fetchDevices()
                .then(devices => {
                    exampleApiClient.destroy();
                    return devices;
                })
        }
}

File: /drivers/mydriver/device.js

const OAuth2Device = require('homey-wifidriver').OAuth2Device;
 
const backOffStrategy = {
    initialDelay: 10000, // 10 seconds
    maxDelay: 1000 * 60 * 60, // 1 hour
    maxTries: 10, // Optional, max backoff tries, if not provided it will not end until reset
    onBackOffReady: (() => {}), // Optional, if provided this method will be called on each backoff, else it will default to this.onInit()
    onBackOffFailed: (() => {}), // Optional, this method will be called when backoff failed; maxTries was exceeded
}
 
class myDevice extends OAuth2Device { // Which extends WifiDevice
 
    onInit() {
        super.onInit({backOffStrategy});
 
        // Create API client with OAuth2Account
        this.exampleApiClient = new ExampleAPI({
            oauth2Account: this.getOAuth2Account(), // OAuth2Account instance that handles fetching and refreshing access tokens
        })
        .on('initialization_failed', () => {
            this.nextBackoff()
        })
        .on('initialization_success', () => {
            this.resetBackOff();
        });
 
        // Method that will create an interval
        this.registerPollInterval({
            id: 'status',
            fn: this.exampleApiClient.getStatus.bind(this.exampleApiClient),
            interval: 30000,
        });
        
        // To stop polling
        this.deregisterPollInterval('status');
    }
    
    onDeleted() {
        // Clean up registered polling intervals
        super.onDeleted();
    }
}

Dependencies (6)

Dev Dependencies (3)

Package Sidebar

Install

npm i homey-wifidriver

Weekly Downloads

2

Version

1.0.8

License

ISC

Unpacked Size

1.26 MB

Total Files

64

Last publish

Collaborators

  • athombv-github-actions
  • robinbolscher
  • weejewel