Commit 242f80a8 authored by Samuel Elliott's avatar Samuel Elliott

Only poll power state when a client is subscribed to updates

parent 0e1e8c4b
......@@ -205,17 +205,25 @@ class TVAccessory {
.setCharacteristic(this.constructor.hap.Characteristic.FirmwareRevision, require('../package').version);
if (this.expose_power_service || this.expose_television_service) {
const updatePowerState = async () => {
const updatePowerState = async force => {
const on_characteristic = this.expose_power_service ?
this.power_service.getCharacteristic(this.constructor.hap.Characteristic.On) : null;
const active_characteristic = this.expose_television_service ?
this.tv_service.getCharacteristic(this.constructor.hap.Characteristic.Active) : null;
const subscriptions = (on_characteristic ? on_characteristic.subscriptions : 0) +
(active_characteristic ? active_characteristic.subscriptions : 0);
if (!force && !subscriptions) return;
const on = await this.getPowerState();
if (this.expose_power_service) this.power_service.getCharacteristic(this.constructor.hap.Characteristic.On)
.updateValue(on);
if (this.expose_television_service) this.tv_service.getCharacteristic(this.constructor.hap.Characteristic.Active)
.updateValue(on ? this.constructor.hap.Characteristic.Active.ACTIVE
: this.constructor.hap.Characteristic.Active.INACTIVE);
if (on_characteristic) on_characteristic.updateValue(on);
if (active_characteristic) active_characteristic.updateValue(on ?
this.constructor.hap.Characteristic.Active.ACTIVE :
this.constructor.hap.Characteristic.Active.INACTIVE);
};
updatePowerState();
updatePowerState(true);
this.update_power_state_interval = setInterval(updatePowerState, 60000);
}
}
......@@ -284,7 +292,9 @@ class TVAccessory {
this.constructor.hap.Characteristic.Active.INACTIVE);
}
callback(undefined, data);
}).catch(callback));
}).catch(callback))
.on('subscribe', () => this.log.debug('Subscribed to Switch/On characteristic'))
.on('unsubscribe', () => this.log.debug('Unsubscribed from Switch/On characteristic'));
return Object.defineProperty(this, 'power_service', {configurable: true, value: power_service}).power_service;
}
......@@ -454,7 +464,9 @@ class TVAccessory {
.updateValue(on === this.constructor.hap.Characteristic.Active.ACTIVE);
}
callback(undefined, data);
}).catch(callback));
}).catch(callback))
.on('subscribe', () => this.log.debug('Subscribed to Television/Active characteristic'))
.on('unsubscribe', () => this.log.debug('Unsubscribed from Television/Active characteristic'));
tv_service.getCharacteristic(this.constructor.hap.Characteristic.ActiveIdentifier)
.on('get', callback => this.getInputIdentifier().then(data => callback(undefined, data)).catch(callback))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment