UNPKG

homebridge-philips-pita-paka

Version:

Homebridge Plugin for Philips Android TV API v6+

288 lines (252 loc) 8.61 kB
"use strict"; const pkg = require("./package.json"); const PhilipsTV = require("./PhilipsTV.js"); const pluginName = pkg.name; const accessoryName = "PhilipsTV"; let Service, Characteristic; class PhilipsTvAccessory { state = { power: true, ambilight: true, source: 0, volume: 0, currentBrightness: 0, }; config = {}; services = []; tvService = null; constructor(log, config) { this.config = { ...this.config, ...config }; this.PhilipsTV = new PhilipsTV(config); this.registerAccessoryInformationService(); this.registerTelevisionService(); // this.registerVolumeService(); if (config.has_ambilight) { this.registerAmbilightService(); // this.registerAmbilightService1(); this.registerBrightnessService(); } if (config.inputs) { this.registerInputService(); } } identify(callback) { callback(); // success } registerAccessoryInformationService = () => { const { name, model_year } = this.config; const { Name, Manufacturer, Model, FirmwareRevision } = Characteristic; const infoService = new Service.AccessoryInformation(); infoService .setCharacteristic(Name, name) .setCharacteristic(Manufacturer, "Philips") .setCharacteristic(Model, "Year " + model_year) .setCharacteristic(FirmwareRevision, pkg.version); this.services.push(infoService); }; registerTelevisionService = () => { const { name, poll_status_interval } = this.config; const { ConfiguredName, SleepDiscoveryMode, Active } = Characteristic; const tvService = new Service.Television(name, "Television"); const power = tvService.getCharacteristic(Active); tvService.setCharacteristic(ConfiguredName, name); tvService.setCharacteristic( SleepDiscoveryMode, SleepDiscoveryMode.ALWAYS_DISCOVERABLE ); // power.on("get", this.PhilipsTV.getPowerState); power.on("get", (callback) => { this.PhilipsTV.getPowerState((err, value) => { callback(null, this.state.power); }); }); power.on("set", (value, callback) => { callback(null); this.state.power = value; power.updateValue(value); this.PhilipsTV.setPowerState(value, (err) => { if (err) { console.error("Error powering TV:", err); } }); }); // tvService.getCharacteristic(Active).onSet((newValue) => { // this.log.info("set Active => setNewValue: " + newValue); // tvService.updateCharacteristic(Active, 1); // }); // .onGet(() => { // this.PhilipsTV.getPowerState((err, value) => { // callback(null, this.state.power); // }); // }); if (poll_status_interval) { setInterval(() => { this.PhilipsTV.getPowerState((err, value) => { if (this.state.power !== value) { this.state.power = value; power.updateValue(value); } }); }, poll_status_interval * 1000); } this.tvService = tvService; this.services.push(tvService); }; registerInputService = () => { const { inputs } = this.config; const { ActiveIdentifier } = Characteristic; this.tvService.setCharacteristic(ActiveIdentifier, 1); this.tvService .getCharacteristic(ActiveIdentifier) .on("get", (callback) => { this.PhilipsTV.getCurrentSource(inputs).then((source) => { this.state.source = source; callback(null, this.state.source); }); }) .on("set", (value, callback) => { this.state.source = value; const input = inputs[value]; this.PhilipsTV.setSource(input, callback); }); inputs.forEach((item, index) => { const input = this.createInputSource(item.name, item.name, index); this.tvService.addLinkedService(input); this.services.push(input); }); }; registerAmbilightService = () => { const { name, poll_status_interval } = this.config; this.ambilightService = new Service.Lightbulb( name + " Ambilight", "tvAmbilight" ); const ambilightPower = this.ambilightService.getCharacteristic( Characteristic.On ); ambilightPower .on("get", this.PhilipsTV.getAmbilightState) .on("set", (value, callback) => { this.state.ambilight = value; this.PhilipsTV.setAmbilightState(value, callback); }); this.services.push(this.ambilightService); if (poll_status_interval) { setInterval(() => { this.PhilipsTV.getAmbilightState((err, value) => { if (this.state.ambilight !== value) { this.state.ambilight = value; ambilightPower.updateValue(value); } }); }, poll_status_interval * 1000); } }; // registerAmbilightService1 = () => { // const { name, poll_status_interval } = this.config; // this.ambilightService2 = new Service.Lightbulb( // name + " Ambilight2", // "tvAmbilight1" // ); // const ambilightPower = this.ambilightService2.getCharacteristic( // Characteristic.On // ); // ambilightPower // .on("get", this.PhilipsTV.getAmbilightState) // .on("set", (value, callback) => { // this.state.ambilight = value; // this.PhilipsTV.setAmbilightLounge(value, callback); // }); // this.services.push(this.ambilightService2); // if (poll_status_interval) { // setInterval(() => { // this.PhilipsTV.getAmbilightState((err, value) => { // if (this.state.ambilight !== value) { // this.state.ambilight = value; // ambilightPower.updateValue(value); // } // }); // }, poll_status_interval * 1000); // } // }; registerBrightnessService = () => { const { name } = this.config; this.lightService = new Service.Lightbulb( name + " Brightness", "tvLightBrightness" ); const ambiBrightness = this.lightService .getCharacteristic(Characteristic.Brightness) .on("get", this.PhilipsTV.getAmbilightBrightness) .on("set", (value, callback) => { this.state.currentBrightness = Math.min(100, Math.max(0, value)); this.PhilipsTV.setAmbilightLounge(Math.round(value * 2.55), callback); ambiBrightness.updateValue(Math.min(100, Math.max(0, value))); }); this.services.push(this.lightService); }; // registerVolumeService = () => { // const { name, poll_status_interval } = this.config; // this.volumeService = new Service.TelevisionSpeaker( // name + " Volume", // "tvVolume" // ); // const volumeLevel = this.volumeService.getCharacteristic( // Characteristic.Volume // ); // const mute = this.volumeService.getCharacteristic(Characteristic.Mute); // mute // .on("get", (callback) => { // callback(null, this.PhilipsTV.getMuteState); // }) // .on("set", (value, callback) => { // this.PhilipsTV.setMuteState(value, callback); // mute.updateValue(value); // }); // volumeLevel // .on("get", this.PhilipsTV.getVolumeState) // .on("set", (value, callback) => { // this.state.volume = value; // this.PhilipsTV.setVolumeState(value, callback); // volumeLevel.updateValue(value); // }); // if (poll_status_interval) { // setInterval(() => { // this.PhilipsTV.getMuteState((err, value) => { // mute.updateValue(value); // }); // this.PhilipsTV.getVolumeState((err, value) => { // if (this.state.volume !== value) { // this.state.volume = value; // volumeLevel.updateValue(value); // } // }); // }, poll_status_interval * 1000); // } // this.services.push(this.volumeService); // }; createInputSource( id, name, number, type = Characteristic.InputSourceType.TV ) { const { Identifier, ConfiguredName, IsConfigured, InputSourceType } = Characteristic; const input = new Service.InputSource(id, name); input .setCharacteristic(Identifier, number) .setCharacteristic(ConfiguredName, name) .setCharacteristic(IsConfigured, IsConfigured.CONFIGURED) .setCharacteristic(InputSourceType, type); return input; } getServices() { return this.services; } } module.exports = function (homebridge) { Service = homebridge.hap.Service; Characteristic = homebridge.hap.Characteristic; homebridge.registerAccessory(pluginName, accessoryName, PhilipsTvAccessory); };