Skip to content

Commit

Permalink
Added better zone support
Browse files Browse the repository at this point in the history
  • Loading branch information
mthiel committed Dec 5, 2024
1 parent fd52030 commit 9f24f2f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
34 changes: 27 additions & 7 deletions src/actions/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ import { AVRTracker } from "../modules/tracker";
/** @typedef {import("../modules/tracker").ReceiverList} ReceiverList */
/** @typedef {import("../modules/tracker").ReceiverInfo} ReceiverInfo */

/**
* @typedef {Object} ReceiverDetail
* @property {ReceiverUUID} uuid - The receiver UUID
* @property {number} [zone] - The zone to control on the receiver
*/
/** @typedef {string} ActionUUID */
/** @typedef {string} ReceiverUUID */
/** @typedef {Record<ActionUUID, ReceiverUUID>} ActionReceiverMap */
/** @typedef {Record<ActionUUID, ReceiverDetail>} ActionReceiverMap */

/**
* @typedef {Object} ActionSettings
Expand Down Expand Up @@ -87,6 +92,8 @@ export class PluginAction extends SingletonAction {
return;
}

const zone = /** @type {number} */ (ev.payload.settings.zone) || 0;

// If a connection doesn't exist yet, try to create one
if (receiverId in this.avrConnections === false) {
// Should we wait for the tracker to be updated first?
Expand All @@ -103,12 +110,15 @@ export class PluginAction extends SingletonAction {
}

// Add listener for receiver events if we haven't already
if (Object.values(this.actionReceiverMap).includes(receiverId) === false) {
if (Object.values(this.actionReceiverMap).some((detail) => detail.uuid === receiverId) === false) {
this.avrConnections[receiverId].on(this.routeReceiverEvent.bind(this));
}

// Update the map with the selected receiver ID for this action
this.actionReceiverMap[ev.action.id] = receiverId;
this.actionReceiverMap[ev.action.id] = {
uuid: receiverId,
zone: zone,
};
}

/**
Expand All @@ -135,6 +145,7 @@ export class PluginAction extends SingletonAction {
async onUserChoseReceiver(ev) {
/** @type {ActionSettings} */
const settings = await ev.action.getSettings();
const zone = /** @type {number} */ (settings.zone) || 0;

let statusMsg = "";

Expand All @@ -145,7 +156,10 @@ export class PluginAction extends SingletonAction {
const connection = await this.connectReceiver(settings.uuid);
if (connection !== undefined) {
// Add the connection to the map if we were successful
this.actionReceiverMap[ev.action.id] = settings.uuid;
this.actionReceiverMap[ev.action.id] = {
uuid: settings.uuid,
zone: zone,
};
statusMsg = connection.status.statusMsg;
} else {
// If we failed to connect, clear the association
Expand All @@ -154,7 +168,10 @@ export class PluginAction extends SingletonAction {
}
} else {
// We already have a connection, so just update the receiver map
this.actionReceiverMap[ev.action.id] = settings.uuid;
this.actionReceiverMap[ev.action.id] = {
uuid: settings.uuid,
zone: zone,
};
statusMsg = this.avrConnections[settings.uuid].status.statusMsg;
}
} else {
Expand Down Expand Up @@ -235,7 +252,10 @@ export class PluginAction extends SingletonAction {
*/
routeReceiverEvent(ev) {
// Get the list of actions to inform of the event and add them to the event object
ev.actions = this.actions.filter((action) => this.actionReceiverMap[action.id] === ev.connection.uuid);
ev.actions = this.actions.filter((action) =>
this.actionReceiverMap[action.id]?.uuid === ev.connection.uuid &&
this.actionReceiverMap[action.id]?.zone === ev.zone
);

switch (ev.type) {
case "connected":
Expand Down Expand Up @@ -281,7 +301,7 @@ export class PluginAction extends SingletonAction {
let statusMsg = "";

if (action && action.id in this.actionReceiverMap) {
statusMsg = this.avrConnections[this.actionReceiverMap[action.id]]?.status.statusMsg || "";
statusMsg = this.avrConnections[this.actionReceiverMap[action.id].uuid]?.status.statusMsg || "";
}

this.updateStatusMessage(statusMsg);
Expand Down
29 changes: 5 additions & 24 deletions src/actions/power.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class PowerAction extends PluginAction {
await super.onWillAppear(ev);

// If there's no connection yet, there's nothing to do
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id]];
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id].uuid];
if (!connection) return;

// Set the initial state of the action based on the receiver's power status
Expand All @@ -38,7 +38,7 @@ export class PowerAction extends PluginAction {
* @param {KeyDownEvent} ev - The event object.
*/
onKeyDown(ev) {
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id]];
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id].uuid];
if (!connection) return;

const settings = ev.payload.settings;
Expand All @@ -61,30 +61,11 @@ export class PowerAction extends PluginAction {
onReceiverPowerChanged(ev) {
Promise.all(
ev.actions.map(async (action) => {
// Filter any non-key actions and zones that don't match the event zone
// Filter any non-key actions that don't match the event zone
if (action.isKey() === false) return;
const actionZone = (/** @type {ActionSettings} */ (await action.getSettings())).zone || 0;
if (actionZone !== ev.zone) return;

action.setState(ev.connection.status.zones[actionZone].power ? 0 : 1);
action.setState(ev.connection.status.zones[ev.zone || 0].power ? 0 : 1);
})
);
}
}

/**
* Update the state of an action based on the receiver's power status.
* @param {Action} action - The action object.
* @param {AVRConnection} connection - The receiver connection object.
* @param {number} [zone] - The zone that the power status changed for
*/
async function updateActionState(action, connection, zone) {
const actionZone = (/** @type {ActionSettings} */ (await action.getSettings())).zone || 0;
if (action.isKey() === false) return;

if (zone !== undefined && zone === actionZone) {
action.setState(connection.status.zones[zone].power ? 0 : 1);
} else {
action.setState(connection.status.zones[actionZone].power ? 0 : 1);
}
}
}
4 changes: 2 additions & 2 deletions src/actions/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export class SourceAction extends PluginAction {
}

/**
* Toggle the power state when the key is pressed
* Change to the configured source when the key is pressed
* @param {KeyDownEvent} ev - The event object.
*/
onKeyDown(ev) {
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id]];
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id].uuid];
if (!connection) return;

/** @type {ActionSettings} */
Expand Down
10 changes: 5 additions & 5 deletions src/actions/volume.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class VolumeAction extends PluginAction {
await super.onWillAppear(ev);

// If there's no connection yet, there's nothing to do
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id]];
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id].uuid];
if (!connection) return;

// Set the initial state of the action based on the receiver's volume & mute status
Expand All @@ -47,7 +47,7 @@ export class VolumeAction extends PluginAction {
/** @type {ActionSettings} */
const settings = ev.payload.settings;

this.avrConnections[this.actionReceiverMap[ev.action.id]]?.changeVolume(ev.payload.ticks, settings.zone) || ev.action.showAlert();
this.avrConnections[this.actionReceiverMap[ev.action.id].uuid]?.changeVolume(ev.payload.ticks, settings.zone) || ev.action.showAlert();
}

/**
Expand All @@ -58,7 +58,7 @@ export class VolumeAction extends PluginAction {
/** @type {ActionSettings} */
const settings = ev.payload.settings;

this.avrConnections[this.actionReceiverMap[ev.action.id]]?.setMute(undefined, settings.zone) || ev.action.showAlert();
this.avrConnections[this.actionReceiverMap[ev.action.id].uuid]?.setMute(undefined, settings.zone) || ev.action.showAlert();
}

/**
Expand All @@ -69,15 +69,15 @@ export class VolumeAction extends PluginAction {
/** @type {ActionSettings} */
const settings = ev.payload.settings;

this.avrConnections[this.actionReceiverMap[ev.action.id]]?.setMute(undefined, settings.zone) || ev.action.showAlert();
this.avrConnections[this.actionReceiverMap[ev.action.id].uuid]?.setMute(undefined, settings.zone) || ev.action.showAlert();
}

/**
* Change the volume when the key is pressed.
* @param {KeyDownEvent} ev - The event object.
*/
onKeyDown(ev) {
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id]];
const connection = this.avrConnections[this.actionReceiverMap[ev.action.id].uuid];
if (!connection) {
ev.action.showAlert();
return;
Expand Down

0 comments on commit 9f24f2f

Please sign in to comment.