Skip to content

Commit

Permalink
fix: mute incorrect (#195)
Browse files Browse the repository at this point in the history
* fix: Mute parsed from events #194
* feat: Created some RenderingControl extensions
  • Loading branch information
svrooij authored May 1, 2024
1 parent c378673 commit c6d6e5d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
12 changes: 11 additions & 1 deletion src/services/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,15 @@ export default abstract class BaseService <TServiceEvent> {
}
}

/**
* Parse a single event property
*
* @param {string} name The name of the property
* @param {unknown} originalValue The original value of the property
* @param {string} type The expected type of the property
* @memberof BaseService
* @remarks This method can be overridden in subclasses to handle special cases, like ChannelValue for Mute and Volume
*/
protected ResolveEventPropertyValue(name: string, originalValue: unknown, type: string): unknown {
if (typeof originalValue === 'string' && (originalValue.startsWith('&lt;') || originalValue.startsWith('<'))) {
if (name.endsWith('MetaData')) {
Expand Down Expand Up @@ -573,10 +582,11 @@ export default abstract class BaseService <TServiceEvent> {
const keys = Object.keys(properties).filter((k) => inKeys.indexOf(k) > -1);

keys.forEach((k) => {
const expectedType = properties[k];
const originalValue = input[k].val ?? input[k];
// validate that originalValue is not undefined or empty or is not an empty object
if (originalValue === undefined || originalValue === '' || (typeof originalValue === 'object' && Object.keys(originalValue).length === 0)) return;
output[k] = this.ResolveEventPropertyValue(k, originalValue, properties[k]);
output[k] = this.ResolveEventPropertyValue(k, originalValue, expectedType);
});

if (Object.keys(output).length === 0) {
Expand Down
47 changes: 46 additions & 1 deletion src/services/rendering-control.service.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export class RenderingControlService extends RenderingControlServiceBase {
if (name === 'Mute') {
const output = {} as ChannelValue<boolean>;
(originalValue as Array<any>).forEach((v) => {
output[v.channel] = v.val === '1';
// The new xml parser will return the value as a number, converting to boolean
output[v.channel] = v.val === 1;
});
return output;
}
Expand All @@ -20,4 +21,48 @@ export class RenderingControlService extends RenderingControlServiceBase {
}
return super.ResolveEventPropertyValue(name, originalValue, type);
}

/**
* Get nightmode status of playbar.
*
* @returns {Promise<boolean>}
* @memberof RenderingControlService
*/
public async GetNightMode(): Promise<boolean> {
return (await this.GetEQ({ InstanceID: 0, EQType: 'NightMode' })).CurrentValue === 1;
}

/**
* Turn on/off night mode, on your playbar.
* shortcut to .RenderingControlService.SetEQ({ InstanceID: 0, EQType: 'NightMode', DesiredValue: dialogLevel === true ? 1 : 0 })
*
* @param {boolean} nightmode
* @returns {Promise<boolean>}
* @memberof RenderingControlService
*/
public SetNightMode(nightmode: boolean): Promise<boolean> {
return this.SetEQ({ InstanceID: 0, EQType: 'NightMode', DesiredValue: nightmode === true ? 1 : 0 });
}

/**
* Get Speech Enhancement status of playbar
*
* @returns {Promise<boolean>}
* @memberof RenderingControlService
*/
public async GetSpeechEnhancement(): Promise<boolean> {
return (await this.GetEQ({ InstanceID: 0, EQType: 'DialogLevel' })).CurrentValue === 1;
}

/**
* Turn on/off speech enhancement, on your playbar,
* shortcut to .RenderingControlService.SetEQ({ InstanceID: 0, EQType: 'DialogLevel', DesiredValue: dialogLevel === true ? 1 : 0 })
*
* @param {boolean} dialogLevel
* @returns {Promise<boolean>}
* @memberof RenderingControlService
*/
public SetSpeechEnhancement(dialogLevel: boolean): Promise<boolean> {
return this.SetEQ({ InstanceID: 0, EQType: 'DialogLevel', DesiredValue: dialogLevel === true ? 1 : 0 });
}
}
18 changes: 8 additions & 10 deletions src/sonos-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,8 @@ export default class SonosDevice extends SonosDeviceBase {
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async GetNightMode(): Promise<boolean> {
return (await this.RenderingControlService.GetEQ({ InstanceID: 0, EQType: 'NightMode' })).CurrentValue === 1;
public GetNightMode(): Promise<boolean> {
return this.RenderingControlService.GetNightMode();
}

/**
Expand Down Expand Up @@ -1165,8 +1165,8 @@ export default class SonosDevice extends SonosDeviceBase {
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async GetSpeechEnhancement(): Promise<boolean> {
return (await this.RenderingControlService.GetEQ({ InstanceID: 0, EQType: 'DialogLevel' })).CurrentValue === 1;
public GetSpeechEnhancement(): Promise<boolean> {
return this.RenderingControlService.GetSpeechEnhancement();
}

/**
Expand Down Expand Up @@ -1254,9 +1254,8 @@ export default class SonosDevice extends SonosDeviceBase {
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async SetNightMode(nightmode: boolean): Promise<boolean> {
return await this.RenderingControlService
.SetEQ({ InstanceID: 0, EQType: 'NightMode', DesiredValue: nightmode === true ? 1 : 0 });
public SetNightMode(nightmode: boolean): Promise<boolean> {
return this.RenderingControlService.SetNightMode(nightmode);
}

/**
Expand Down Expand Up @@ -1319,9 +1318,8 @@ export default class SonosDevice extends SonosDeviceBase {
* @returns {Promise<boolean>}
* @memberof SonosDevice
*/
public async SetSpeechEnhancement(dialogLevel: boolean): Promise<boolean> {
return await this.RenderingControlService
.SetEQ({ InstanceID: 0, EQType: 'DialogLevel', DesiredValue: dialogLevel === true ? 1 : 0 });
public SetSpeechEnhancement(dialogLevel: boolean): Promise<boolean> {
return this.RenderingControlService.SetSpeechEnhancement(dialogLevel);
}

/**
Expand Down

0 comments on commit c6d6e5d

Please sign in to comment.