Skip to content

Commit

Permalink
Actively set visibility of inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
LeLunZ committed May 31, 2024
1 parent 6b90758 commit d381978
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/platform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic } from 'homebridge';
import {API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic, Categories} from 'homebridge';

import { PLUGIN_NAME } from './settings';
import { HiSenseTVAccessory } from './platformAccessory';
Expand Down Expand Up @@ -101,7 +101,7 @@ export class HiSenseTVPlatform implements DynamicPlatformPlugin {
this.log.info('Adding new accessory:', device.name);

// create a new accessory
const accessory = new this.api.platformAccessory(device.name, uuid);
const accessory = new this.api.platformAccessory(device.name, uuid, Categories.TELEVISION);

// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
Expand Down
56 changes: 37 additions & 19 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ export class HiSenseTVAccessory {
private speakerService: Service;

private deviceState = {
isConnected: false, hasFetchedInputs: false, currentSourceName: '',
isConnected: false,
hasFetchedInputs: false,
currentSourceName: '',
};

private inputSources: InputSource[] = [];

constructor(private readonly platform: HiSenseTVPlatform, private readonly accessory: PlatformAccessory) {
constructor(
private readonly platform: HiSenseTVPlatform,
private readonly accessory: PlatformAccessory,
) {
// Start the asynchronous check of the TV status.
this.checkTVStatus();

Expand All @@ -34,8 +39,6 @@ export class HiSenseTVAccessory {

// Create the service.
this.service = this.accessory.getService(this.platform.Service.Television) || this.accessory.addService(this.platform.Service.Television);
// TODO check if needed
// accessory.category = this.platform.api.hap.Categories.TELEVISION;

// Configure the service.
this.service
Expand Down Expand Up @@ -110,7 +113,7 @@ export class HiSenseTVAccessory {

async setRemoteKey(newValue: CharacteristicValue) {
let keyName = '';
switch (newValue) {
switch(newValue) {
case this.platform.Characteristic.RemoteKey.REWIND: {
this.platform.log.debug('set Remote Key Pressed: REWIND');
keyName = 'rewind';
Expand Down Expand Up @@ -209,7 +212,7 @@ export class HiSenseTVAccessory {
if (value === 0) {
this.platform.log.debug('Switching to the Other input is unsupported. This input is only used when the plugin is unable to identify the current input on the TV (i.e. you are using an app).');
} else if (this.deviceState.hasFetchedInputs) {
const inputSource = this.inputSources[(value as number) - 1];
const inputSource = this.inputSources[(value as number)-1];
await this.sendCommand(['--key', 'source_' + inputSource.sourceid]);
this.service.updateCharacteristic(this.platform.Characteristic.ActiveIdentifier, value);
} else {
Expand Down Expand Up @@ -242,7 +245,8 @@ export class HiSenseTVAccessory {
this.inputSources.forEach((inputSource, index) => {
this.platform.log.debug('Adding input: ' + JSON.stringify(inputSource));

const inputService = this.accessory.getService('input' + inputSource.sourceid) || this.accessory.addService(this.platform.Service.InputSource, 'input' + inputSource.sourceid, 'input' + inputSource.sourceid);
const inputService = this.accessory.getService('input'+inputSource.sourceid)
|| this.accessory.addService(this.platform.Service.InputSource, 'input'+inputSource.sourceid, 'input'+inputSource.sourceid);

inputService.setCharacteristic(this.platform.Characteristic.IsConfigured, this.platform.Characteristic.IsConfigured.CONFIGURED);
inputService.setCharacteristic(this.platform.Characteristic.ConfiguredName, inputSource.displayname);
Expand All @@ -259,11 +263,9 @@ export class HiSenseTVAccessory {
}

inputService.setCharacteristic(this.platform.Characteristic.InputSourceType, inputType);
inputService.setCharacteristic(this.platform.Characteristic.Identifier, (index + 1));
inputService.setCharacteristic(this.platform.Characteristic.Identifier, (index+1));

inputService.getCharacteristic(this.platform.Characteristic.CurrentVisibilityState).onGet(() => {
return this.deviceState.isConnected ? this.platform.Characteristic.CurrentVisibilityState.SHOWN : this.platform.Characteristic.CurrentVisibilityState.HIDDEN;
});
inputService.updateCharacteristic(this.platform.Characteristic.CurrentVisibilityState, this.platform.Characteristic.CurrentVisibilityState.SHOWN);

inputSource.service = inputService;

Expand All @@ -289,22 +291,26 @@ export class HiSenseTVAccessory {
createHomeSource() {
this.platform.log.debug('Adding unknown source...');

const inputService = this.accessory.getService('inputhome') || this.accessory.addService(this.platform.Service.InputSource, 'inputhome', 'inputhome');
const inputService = this.accessory.getService('inputhome')
|| this.accessory.addService(this.platform.Service.InputSource, 'inputhome', 'inputhome');

inputService
.setCharacteristic(this.platform.Characteristic.IsConfigured, this.platform.Characteristic.IsConfigured.CONFIGURED)
.setCharacteristic(this.platform.Characteristic.ConfiguredName, 'Unknown')
.setCharacteristic(this.platform.Characteristic.InputSourceType, this.platform.Characteristic.InputSourceType.OTHER)
.setCharacteristic(this.platform.Characteristic.Identifier, 0);

inputService.getCharacteristic(this.platform.Characteristic.CurrentVisibilityState).onGet(() => {
return this.deviceState.isConnected && this.deviceState.currentSourceName != '' ?
this.platform.Characteristic.CurrentVisibilityState.SHOWN : this.platform.Characteristic.CurrentVisibilityState.HIDDEN;
});
inputService.updateCharacteristic(this.platform.Characteristic.CurrentVisibilityState, this.platform.Characteristic.CurrentVisibilityState.HIDDEN);

this.service.addLinkedService(inputService);
}

public setVisibilityState(state: CharacteristicValue) {
this.inputSources.forEach((inputSource) => {
inputSource.service?.updateCharacteristic(this.platform.Characteristic.CurrentVisibilityState, state);
});
}

/**
* Check the current TV status by attempting to telnet the MQTT service directly.
*
Expand All @@ -330,20 +336,23 @@ export class HiSenseTVAccessory {
this.getSources();
} else {
this.getCurrentInput();
this.setVisibilityState(this.platform.Characteristic.CurrentVisibilityState.SHOWN);
}
});

socket.on('timeout', () => {
this.platform.log.debug('Connection to TV timed out.');
this.deviceState.isConnected = false;
this.service.updateCharacteristic(this.platform.Characteristic.Active, this.deviceState.isConnected);
this.setVisibilityState(this.platform.Characteristic.CurrentVisibilityState.HIDDEN);
socket.destroy();
});

socket.on('error', (err) => {
this.platform.log.debug('An error occurred while connecting to TV: ' + err);
this.deviceState.isConnected = false;
this.service.updateCharacteristic(this.platform.Characteristic.Active, this.deviceState.isConnected);
this.setVisibilityState(this.platform.Characteristic.CurrentVisibilityState.HIDDEN);
socket.destroy();
});
}
Expand Down Expand Up @@ -394,7 +403,7 @@ export class HiSenseTVAccessory {
for (let index = 0; index < this.inputSources.length; index++) {
const inputSource = this.inputSources[index];
if (inputSource.sourcename === this.deviceState.currentSourceName) {
return index + 1;
return index+1;
}
}

Expand All @@ -412,7 +421,11 @@ export class HiSenseTVAccessory {

const pythonScript = path.resolve(__dirname, '../bin/hisensetv.py');

let pythonArgs = args.concat([this.accessory.context.device.ipaddress, '--ifname', this.platform.config.ifname]);
let pythonArgs = args.concat([
this.accessory.context.device.ipaddress,
'--ifname',
this.platform.config.ifname,
]);
if (sslParameter !== null) {
pythonArgs = pythonArgs.concat(sslParameter);
}
Expand Down Expand Up @@ -445,7 +458,12 @@ export class HiSenseTVAccessory {
sslParameter = ['--no-ssl'];
break;
case 'custom':
sslParameter = ['--certfile', this.accessory.context.device.sslcertificate.trim(), '--keyfile', this.accessory.context.device.sslprivatekey.trim()];
sslParameter = [
'--certfile',
this.accessory.context.device.sslcertificate.trim(),
'--keyfile',
this.accessory.context.device.sslprivatekey.trim(),
];
break;
}

Expand Down

0 comments on commit d381978

Please sign in to comment.