UNPKG

homebridge-pioneer-avr-2025

Version:

A Pioneer AVR plugin for homebridge. This plugin is designed for Pioneer models that use Telnet Commands (e.g., VSX-922). It may not be compatible with newer models (e.g., VSX-LX304).

130 lines 5.44 kB
"use strict"; // src/pioneer-avr/power.ts var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PowerManagementMixin = PowerManagementMixin; const exitHandler_1 = require("../exitHandler"); /** * This mixin adds power management methods to a base class, including power control, * status checking, and handling disconnections. * @param Base - The base class to extend with power management methods. * @returns A new class that extends the base class with added power management functionality. */ function PowerManagementMixin(Base) { return class extends Base { constructor(...args) { super(...args); // Handle disconnection by setting power state to off and invoking callback this.telnetAvr.addOnDisconnectCallback(() => { this.state.on = false; try { this.functionSetPowerState(this.state.on); } catch (e) { this.log.debug('functionSetPowerState', e); } }); // Handle connection by restoring the last known power state this.telnetAvr.addOnConnectCallback(() => __awaiter(this, void 0, void 0, function* () { try { this.functionSetPowerState(this.state.on); } catch (e) { this.log.debug('functionSetPowerState', e); } })); // Handle exit event by setting power state to off before shutdown (0, exitHandler_1.addExitHandler)(() => { this.state.on = false; this.functionSetPowerState(this.state.on); }, this); } /** * Placeholder for setting the power state of the AVR. * Intended to be overridden externally. */ functionSetPowerState(_state) { // Placeholder logic for setting power state, should be overridden externally } /** * Sends a power status query to the AVR and updates the power state. * @param callback - Function to run after updating power state. */ __updatePower(callback) { return __awaiter(this, void 0, void 0, function* () { this.telnetAvr.sendMessage('?P', 'PWR', () => { this.state.lastGetPowerStatus = Date.now(); try { callback(); } catch (e) { this.log.debug('__updatePower callback', e); } }); }); } /** * Retrieves the power status of the AVR. * @param callback - The function to handle the power status result. */ powerStatus(callback) { return __awaiter(this, void 0, void 0, function* () { if (this.state.on !== null) { try { callback(null, this.state.on); } catch (e) { this.log.debug('powerStatus', e); } return; } // If state is not known, query AVR for current power status this.__updatePower(() => { try { callback(null, this.state.on); } catch (e) { this.log.debug('powerStatus2', e); } }); }); } /** * Sends a power-on command to the AVR and updates the power status. */ powerOn() { return __awaiter(this, void 0, void 0, function* () { this.log.debug('Power on'); this.lastUserInteraction = Date.now(); this.telnetAvr.sendMessage('PO'); // Allow some time for the command to take effect, then check status setTimeout(() => { this.powerStatus(() => { }); }, 500); }); } /** * Sends a power-off command to the AVR and updates the power status. */ powerOff() { return __awaiter(this, void 0, void 0, function* () { this.log.debug('Power off'); this.lastUserInteraction = Date.now(); this.telnetAvr.sendMessage('PF'); // Allow some time for the command to take effect, then check status setTimeout(() => { this.powerStatus(() => { }); }, 500); }); } }; } //# sourceMappingURL=power.js.map