From 45ace6ae13e24ef951dc9eb6d3b92a04a58258bd Mon Sep 17 00:00:00 2001 From: banboobee <98196664+banboobee@users.noreply.github.com> Date: Fri, 16 Jun 2023 11:57:52 +0900 Subject: [PATCH] Sync channel selection when tv is powered on. (#529) * Sync channel selection when powered on. --- CHANGELOG.md | 2 +- accessories/tv.js | 81 +++++++++++++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68211729..665a917f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [4.4.11] - 2022-06-08 ### Added - w1 and file temperatures will return a battery level of 100 if none found - Serializes the simultaneous IR/RF commands. (Thanks @banboobee) #520 diff --git a/accessories/tv.js b/accessories/tv.js index b4fc68de..1812336a 100644 --- a/accessories/tv.js +++ b/accessories/tv.js @@ -10,6 +10,7 @@ class TVAccessory extends BroadlinkRMAccessory { super(log, config, serviceManagerType); if (!config.isUnitTest) {this.checkPing(ping);} + this.lastPingResponse = undefined; } setDefaults() { @@ -90,13 +91,19 @@ class TVAccessory extends BroadlinkRMAccessory { else {arp(pingIPAddress, pingFrequency, this.pingCallback.bind(this))} } - pingCallback(active) { + async pingCallback(active) { const { config, state, serviceManager } = this; if (this.stateChangeInProgress){ return; } + if (this.lastPingResponse !== undefined && this.lastPingResponse !== active) { + if (config.syncInputSourceWhenOn && active && this.state.currentInput !== undefined) { + await this.setInputSource(); // sync if asynchronously turned on + } + } + this.lastPingResponse = active; if (config.pingIPAddressStateOnly) { state.switchState = active ? true : false; serviceManager.refreshCharacteristicUI(Characteristic.Active); @@ -180,6 +187,23 @@ class TVAccessory extends BroadlinkRMAccessory { return services; } + async setInputSource() { + const { data, host, log, name, logLevel } = this; + const newValue = this.state.currentInput; + + if ( + !data || + !data.inputs || + !data.inputs[newValue] || + !data.inputs[newValue].data + ) { + log(`${name} Input: No input data found. Ignoring request.`); + return; + } + + await this.performSend(data.inputs[newValue].data); + } + setupServiceManager() { const { data, name, config, serviceManagerType, log } = this; const { on, off } = data || {}; @@ -211,32 +235,21 @@ class TVAccessory extends BroadlinkRMAccessory { } }); - this.serviceManager.setCharacteristic(Characteristic.ActiveIdentifier, 1); - - this.serviceManager - .getCharacteristic(Characteristic.ActiveIdentifier) - .on('get', (callback) => callback(null, this.state.input || 0)) - .on('set', (newValue, callback) => { - if ( - !data || - !data.inputs || - !data.inputs[newValue] || - !data.inputs[newValue].data - ) { - log(`${name} Input: No input data found. Ignoring request.`); - callback(null); - return; - } - - this.state.input = newValue; - this.performSend(data.inputs[newValue].data); - - callback(null); - }); + this.serviceManager.addToggleCharacteristic({ + name: 'currentInput', + type: Characteristic.ActiveIdentifier, + getMethod: this.getCharacteristicValue, + setMethod: this.setCharacteristicValue, + bind: this, + props: { + setValuePromise: this.setInputSource.bind(this), + ignorePreviousValue: true + } + }); this.serviceManager .getCharacteristic(Characteristic.RemoteKey) - .on('set', (newValue, callback) => { + .on('set', async (newValue, callback) => { if (!data || !data.remote) { log(`${name} RemoteKey: No remote keys found. Ignoring request.`); callback(null); @@ -292,7 +305,7 @@ class TVAccessory extends BroadlinkRMAccessory { return; } - this.performSend(hexData); + await this.performSend(hexData); callback(null); }); @@ -306,7 +319,7 @@ class TVAccessory extends BroadlinkRMAccessory { this.serviceManager .getCharacteristic(Characteristic.PowerModeSelection) - .on('set', (newValue, callback) => { + .on('set', async (newValue, callback) => { if (!data || !data.powerMode) { log( `${name} PowerModeSelection: No settings data found. Ignoring request.` @@ -333,7 +346,7 @@ class TVAccessory extends BroadlinkRMAccessory { return; } - this.performSend(hexData); + await this.performSend(hexData); callback(null); }); @@ -350,7 +363,7 @@ class TVAccessory extends BroadlinkRMAccessory { speakerService .getCharacteristic(Characteristic.VolumeSelector) - .on('set', (newValue, callback) => { + .on('set', async (newValue, callback) => { if (!data || !data.volume) { log( `${name} VolumeSelector: No settings data found. Ignoring request.` @@ -377,12 +390,17 @@ class TVAccessory extends BroadlinkRMAccessory { return; } - this.performSend(hexData); + await this.performSend(hexData); callback(null); }); + + speakerService.setCharacteristic(Characteristic.Mute, false); speakerService .getCharacteristic(Characteristic.Mute) - .on('set', (newValue, callback) => { + .on('get', (callback) => { + callback(null, this.state.Mute || false); + }) + .on('set', async (newValue, callback) => { if (!data || !data.volume || !data.volume.mute) { log( `${name} VolumeSelector: No mute data found. Ignoring request.` @@ -400,7 +418,8 @@ class TVAccessory extends BroadlinkRMAccessory { return; } - this.performSend(hexData); + this.state.Mute = newValue; + await this.performSend(hexData); callback(null); });