Fixing bug where volume would restart the first activity. ControlUnit now starts with last used activity
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Brandon Watson 2021-12-28 22:08:59 -05:00
parent 4519edf679
commit 128bb933bd
8 changed files with 59 additions and 21 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@watsonb8/homebridge-harmony-control",
"version": "1.2.0",
"version": "1.2.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "@watsonb8/homebridge-harmony-control",
"version": "1.2.0",
"version": "1.2.1",
"description": "Homebridge platform to control smart home equipment by room.",
"main": "bin/index.js",
"publishConfig": {

View File

@ -137,7 +137,10 @@ export class ControlUnit {
break;
//Turn on with first activity
case 1:
this._activityService.startActivity(
if (this._activityService.getIsActive(this._accessory.displayName)) {
break;
}
this._activityService.startLastOrDefaultActivity(
this._accessory.displayName,
this._activities[0]
);
@ -150,7 +153,8 @@ export class ControlUnit {
*/
private onGetAccessoryActive = async () => {
//@ts-ignore
return this._activityService.getIsActive(this._accessory.displayName)
return this._activityService.getCurrentActivity(this._accessory.displayName)
?.DisplayName
? this._platform.Characteristic.Active.ACTIVE
: this._platform.Characteristic.Active.INACTIVE;
};
@ -176,7 +180,7 @@ export class ControlUnit {
* Event handler for GET active identifier characteristic
*/
private onGetActiveIdentifier = async () => {
let currentActivity: IActivity = this._activityService.getIsActive(
let currentActivity: IActivity = this._activityService.getCurrentActivity(
this._accessory.displayName
)!;
let identifier: number = 0;

View File

@ -9,18 +9,30 @@ export class StateDataProvider {
} = {};
public updateState(activity: IActivity, controlUnitName: string): void {
this._states[controlUnitName] = { currentActivity: activity };
const oldState = this._states[controlUnitName];
this._states[controlUnitName] = {
currentActivity: activity,
lastActivity: oldState?.lastActivity,
};
}
public deactivateState(controlUnitName: string): void {
const oldState = this._states[controlUnitName];
this._states[controlUnitName] = {
currentActivity: undefined,
lastActivity: oldState?.currentActivity,
};
}
public removeState(controlUnitName: string): void {
this._states[controlUnitName] = undefined;
}
public getState(controlUnitName: string) {
public getState(controlUnitName: string): IActivityState | undefined {
if (!this._states[controlUnitName]) {
return undefined;
}
return this._states[controlUnitName]!.currentActivity;
return this._states[controlUnitName];
}
public get states(): {

View File

@ -1,5 +1,6 @@
import { IActivity } from "./config";
export interface IActivityState {
currentActivity: IActivity;
currentActivity?: IActivity;
lastActivity?: IActivity;
}

View File

@ -22,6 +22,18 @@ export class ActivityService {
@inject("log") private _log: Logging
) {}
public startLastOrDefaultActivity = async (
controlUnitName: string,
defaultActivity: IActivity
) => {
const lastActivity = this.getLastActivity(controlUnitName);
if (!lastActivity) {
return this.startActivity(controlUnitName, defaultActivity);
}
return this.startActivity(controlUnitName, lastActivity);
};
public startActivity = async (
controlUnitName: string,
activity: IActivity
@ -68,19 +80,24 @@ export class ActivityService {
);
await this._harmonyDataProvider.powerOffDevices(devicesToTurnOff);
this._stateDataProvider.removeState(controlUnitName);
this._stateDataProvider.deactivateState(controlUnitName);
};
public getCurrentActivity(controlUnitName: string): IActivity | undefined {
return this._stateDataProvider.getState(controlUnitName);
return this._stateDataProvider.getState(controlUnitName)?.currentActivity;
}
public getLastActivity(controlUnitName: string): IActivity | undefined {
return this._stateDataProvider.getState(controlUnitName)?.lastActivity;
}
/**
* Return if a control unit is active
* @param controlUnitName
*/
public getIsActive(controlUnitName: string): IActivity | undefined {
return this._stateDataProvider.getState(controlUnitName);
public getIsActive(controlUnitName: string): boolean {
const state = this._stateDataProvider.getState(controlUnitName);
return state != undefined && state.currentActivity != undefined;
}
/**
@ -104,7 +121,7 @@ export class ActivityService {
return devicesToTurnOn;
}
currentOtherState.currentActivity.DeviceSetupList.forEach(
currentOtherState.currentActivity!.DeviceSetupList.forEach(
(value: IDeviceSetupItem) => {
//there are devices to remove
if (devicesToTurnOn.some((e) => e && e.name === value.DeviceName)) {

View File

@ -24,7 +24,7 @@ export class CommandService {
let commandName: string = "";
let device: HarmonyDevice = this._harmonyDataProvider.getDeviceByName(
currentActivity.ControlDevice
currentActivity.currentActivity!.ControlDevice
);
switch (key) {
case RemoteKey.ARROW_UP: {

View File

@ -16,10 +16,12 @@ export class VolumeService {
*/
public volumeUp = async (controlUnitName: string) => {
let volumeUpCommand: string = "Volume Up";
let currentActivity = this._stateDataProvider.getState(controlUnitName);
if (currentActivity) {
let currentState = this._stateDataProvider.getState(controlUnitName);
if (currentState) {
let volumeDevice: HarmonyDevice =
this._harmonyDataProvider.getDeviceByName(currentActivity.VolumeDevice);
this._harmonyDataProvider.getDeviceByName(
currentState.currentActivity!.VolumeDevice
);
await this._harmonyDataProvider.sendCommand(
volumeUpCommand,
volumeDevice
@ -32,10 +34,12 @@ export class VolumeService {
*/
public volumeDown = async (controlUnitName: string) => {
let volumeDownCommand: string = "Volume Down";
let currentActivity = this._stateDataProvider.getState(controlUnitName);
if (currentActivity) {
let currentState = this._stateDataProvider.getState(controlUnitName);
if (currentState) {
let volumeDevice: HarmonyDevice =
this._harmonyDataProvider.getDeviceByName(currentActivity.VolumeDevice);
this._harmonyDataProvider.getDeviceByName(
currentState.currentActivity!.VolumeDevice
);
await this._harmonyDataProvider.sendCommand(
volumeDownCommand,
volumeDevice