homebridge-philips-hue-sync-box
Version:
Homebridge plugin for the Philips Hue Sync Box.
87 lines (77 loc) • 2.74 kB
text/typescript
import { PlatformAccessory, Service } from 'homebridge';
import type { HueSyncBoxPlatform } from '../platform.js';
import type { HdmiInput, State } from '../state.js';
import { BaseTvDevice } from './baseTv.js';
import { HDMI_INPUT_MAX, HDMI_INPUT_MIN } from '../lib/constants.js';
export class TvDevice extends BaseTvDevice {
constructor(
protected readonly platform: HueSyncBoxPlatform,
public readonly accessory: PlatformAccessory,
protected state: State,
protected mainAccessory?: PlatformAccessory
) {
super(platform, accessory, state, mainAccessory);
this.service
.getCharacteristic(this.platform.api.hap.Characteristic.ActiveIdentifier)
.onSet(value => {
const identifier = value as number;
if (
!Number.isInteger(identifier) ||
identifier < HDMI_INPUT_MIN ||
identifier > HDMI_INPUT_MAX
) {
this.platform.log.warn(
'Ignoring out-of-range HDMI input identifier: ' + identifier
);
return;
}
return this.updateExecution({
hdmiSource: 'input' + identifier,
});
});
}
updateTv(): void {
this.platform.log.debug(
'Updated HDMI input to ' + this.state.execution.hdmiSource
);
this.service.updateCharacteristic(
this.platform.api.hap.Characteristic.ActiveIdentifier,
parseInt(this.state.execution.hdmiSource.replace('input', ''))
);
}
protected getSuffix(): string {
return '-T';
}
protected getServiceSubType(): string | undefined {
return 'TV';
}
protected getServiceName(): string | undefined {
return 'TV';
}
protected getConfiguredNamePropertyName(): string {
return 'tvAccessoryConfiguredName';
}
protected isLightbulbEnabled(): boolean {
return this.platform.config.tvAccessoryLightbulb;
}
protected createInputServices(): void {
const services: Service[] = [];
for (let i = HDMI_INPUT_MIN; i <= HDMI_INPUT_MAX; i++) {
// Sets the TV name
const hdmiState: HdmiInput = this.state.hdmi[`input${i}`];
const hdmiPosition = 'HDMI ' + i;
const hdmiName = hdmiState.name ?? hdmiPosition;
const hdmiInputService = this.getInputService(hdmiName, hdmiPosition);
hdmiInputService
.getCharacteristic(
this.platform.api.hap.Characteristic.TargetVisibilityState
)
.onSet(this.setVisibility(hdmiInputService));
// Adds the input as a linked service, which is important so that the input is properly displayed in the Home app
this.service.addLinkedService(hdmiInputService);
services.push(hdmiInputService);
}
this.inputServices = services;
this.updateSources(services);
}
}