Skip to content

Commit

Permalink
Sync channel selection when tv is powered on. (#529)
Browse files Browse the repository at this point in the history
* Sync channel selection when powered on.
  • Loading branch information
banboobee committed Jun 16, 2023
1 parent 451d24f commit 45ace6a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -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
Expand Down
81 changes: 50 additions & 31 deletions accessories/tv.js
Expand Up @@ -10,6 +10,7 @@ class TVAccessory extends BroadlinkRMAccessory {
super(log, config, serviceManagerType);

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

setDefaults() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -292,7 +305,7 @@ class TVAccessory extends BroadlinkRMAccessory {
return;
}

this.performSend(hexData);
await this.performSend(hexData);
callback(null);
});

Expand All @@ -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.`
Expand All @@ -333,7 +346,7 @@ class TVAccessory extends BroadlinkRMAccessory {
return;
}

this.performSend(hexData);
await this.performSend(hexData);
callback(null);
});

Expand All @@ -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.`
Expand All @@ -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.`
Expand All @@ -400,7 +418,8 @@ class TVAccessory extends BroadlinkRMAccessory {
return;
}

this.performSend(hexData);
this.state.Mute = newValue;
await this.performSend(hexData);
callback(null);
});

Expand Down

0 comments on commit 45ace6a

Please sign in to comment.