Skip to content

Commit

Permalink
Update fan.js (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnicolson authored Aug 13, 2023
1 parent af430a4 commit e9c7757
Showing 1 changed file with 79 additions and 4 deletions.
83 changes: 79 additions & 4 deletions accessories/fan.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
const ServiceManagerTypes = require('../helpers/serviceManagerTypes');
const SwitchAccessory = require('./switch');
const BroadlinkRMAccessory = require('./accessory');
const catchDelayCancelError = require('../helpers/catchDelayCancelError');
const delayForDuration = require('../helpers/delayForDuration');
const ping = require('../helpers/ping');

class FanAccessory extends BroadlinkRMAccessory {
constructor(log, config = {}, serviceManagerType) {
super(log, config, serviceManagerType);

if (!config.isUnitTest) {this.checkPing(ping);}
}

class FanAccessory extends SwitchAccessory {
setDefaults() {
super.setDefaults();
let { config, state } = this;
config.pingFrequency = config.pingFrequency || 1;
config.pingGrace = config.pingGrace || 10;

config.offDuration = config.offDuration || 60;
config.onDuration = config.onDuration || 60;

// Defaults
config.showSwingMode = config.hideSwingMode === true || config.showSwingMode === false ? false : true;
Expand Down Expand Up @@ -47,17 +58,79 @@ class FanAccessory extends SwitchAccessory {
this.autoOnTimeoutPromise = null;
}

if (this.pingGraceTimeout) {
this.pingGraceTimeout.cancel();
this.pingGraceTimeout = null;
}

if (this.serviceManager.getCharacteristic(Characteristic.Active) === undefined) {
this.serviceManager.setCharacteristic(Characteristic.Active, false);
}
}

checkAutoOnOff() {
this.reset();
this.checkPingGrace();
this.checkAutoOn();
this.checkAutoOff();
}

checkPing(ping) {
const { config } = this;
let { pingIPAddress, pingFrequency, pingUseArp } = config;

if (!pingIPAddress) {return;}

// Setup Ping/Arp-based State
if(!pingUseArp) {ping(pingIPAddress, pingFrequency, this.pingCallback.bind(this))}
else {arp(pingIPAddress, pingFrequency, this.pingCallback.bind(this))}
}

pingCallback(active) {
const { config, state, serviceManager } = this;

if (this.stateChangeInProgress){
return;
}

if (config.pingIPAddressStateOnly) {
state.switchState = active ? true : false;
serviceManager.refreshCharacteristicUI(Characteristic.Active);

return;
}

const value = active ? true : false;
serviceManager.setCharacteristic(Characteristic.Active, value);
}

async setSwitchState(hexData) {
const { data, host, log, name, logLevel } = this;

this.stateChangeInProgress = true;
this.reset();

if (hexData) {await this.performSend(hexData);}

this.checkAutoOnOff();
}

async checkPingGrace () {
await catchDelayCancelError(async () => {
const { config, log, name, state, serviceManager } = this;

let { pingGrace } = config;

if (pingGrace) {

this.pingGraceTimeoutPromise = delayForDuration(pingGrace);
await this.pingGraceTimeoutPromise;

this.stateChangeInProgress = false;
}
});
}

async checkAutoOff() {
await catchDelayCancelError(async () => {
const { config, log, logLevel, name, state, serviceManager } = this;
Expand Down Expand Up @@ -106,7 +179,9 @@ class FanAccessory extends SwitchAccessory {
serviceManager.setCharacteristic(Characteristic.RotationSpeed, state.fanSpeed);
}

super.setSwitchState(hexData, previousValue);
this.reset();

if (hexData) {await this.performSend(hexData);}
}

async setFanSpeed(hexData) {
Expand Down

0 comments on commit e9c7757

Please sign in to comment.