Skip to content

Commit

Permalink
fix: Initial version of command reply (#103)
Browse files Browse the repository at this point in the history
Fixes #101
  • Loading branch information
svrooij authored and Stephan van Rooij committed Jun 13, 2020
1 parent da31cf0 commit 5737176
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/device-control.ts
@@ -1,11 +1,16 @@
import { SonosCommands } from "./sonos-commands";

export class DeviceControl {
public readonly command?: SonosCommands
public readonly command?: SonosCommands;
public readonly replyTopic?: string;
constructor(command?: string, public readonly sonosCommand?: string, public readonly input?: any) {
if(command !== undefined && Object.values(SonosCommands).some(v => v === command.toLowerCase())) {
this.command = command.toLowerCase() as SonosCommands;
}

if(command === SonosCommands.AdvancedCommand && input.reply) {
this.replyTopic = input.reply;
}
}

isValid(): boolean {
Expand Down
5 changes: 4 additions & 1 deletion src/sonos-to-mqtt.ts
Expand Up @@ -87,7 +87,10 @@ export class SonosToMqtt {
return;
}
try {
await SonosCommandMapping.ExecuteControl(correctDevice, payload)
const response = await SonosCommandMapping.ExecuteControl(correctDevice, payload);
if(payload.replyTopic) {
this.mqtt.publish(`${correctDevice.Uuid}/${payload.replyTopic}`, JSON.stringify(response));
}
this.log.debug('Executed {command} for {device} ({uuid})', payload.command ?? payload.sonosCommand, correctDevice.Name, correctDevice.Uuid)
} catch (e) {
this.log.warn(e, 'Error executing {command} for {device} ({uuid})', payload.command ?? payload.sonosCommand, correctDevice.Name, correctDevice.Uuid)
Expand Down

0 comments on commit 5737176

Please sign in to comment.