Class: WifiDevice

WifiDevice()

The WifiDevice class handles common functionalities for WiFi-devices in Homey. It exposes methods for handling polling intervals and backoff strategies (basically an advanced setInterval() for retrying certain things).

Constructor

new WifiDevice()

Example
const WifiDevice = require('homey-wifidriver').WifiDevice;

class myDevice extends WifiDevice {

 onInit() {

     // Create initialization back off strategy
     if(this.backOffStrategy === 'undefined') {
         this.registerBackOffStrategy({
             initialDelay: 10000, // 10 seconds
             maxDelay: 1000 * 60 * 60, // 1 hour
             maxTries: 10, // Optional, max back off tries, if not provided it will not end until reset
             onBackOffReady: this.onInit.bind(this), // Optional, if provided this method will be called on each back off
             onBackOffFailed: (() => {}), // Optional, this method will be called when back off failed; maxTries was exceeded
         })
     }

     // 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');

     if(initializationSuccess === true) {
         this.backOffStrategy.abort()
     } else {
         this.backOffStrategy.backoff()
     }
 }

 onDeleted() {
     // Clean up registered polling intervals
     super.onDeleted();
 }
}

Extends

  • Homey.Device

Methods

deregisterPollInterval(id)

Method that clears the poll interval and removes the registered interval from the list.

Parameters:
Name Type Description
id string

poll interval identifier

onDeleted()

This method will be called when the device has been deleted, it makes sure the client is properly destroyed and left over settings are removed.

registerBackOffStrategy(options) → {BackOffStrategy|Error}

Method that creates and returns a back off strategy (Fibonacci).

Parameters:
Name Type Description
options Object

Specification for initialization backoff strategy

Properties
Name Type Attributes Default Description
onBackOffReady function

Function will be called on each back off tick

onBackOffFailed function <optional>

Function will be called when back off failed (maxTries exceeded)

randomisationFactor number <optional>
0

Randomize the interval (0 - 1)

initialDelay number <optional>
10000

After first failed init wait this long (ms)

maxDelay number <optional>
300000

Max length of time between init retries (ms)

maxTries number <optional>
infinite

Max number of initialization retries

startImmediately boolean <optional>
false

Backoff called on creation

Returns:

backOffStrategy - Back off strategy instance

Type
BackOffStrategy | Error

registerPollInterval(options)

Method that adds a poll interval to the list, and starts polling on the provided interval.

Parameters:
Name Type Description
options Object
Properties
Name Type Description
id string

Custom id of poll interval

fn function

Method that will be called when polling

interval number

Time in ms between polling intervals