Skip to content

Commit

Permalink
fix: Allow setting the client id
Browse files Browse the repository at this point in the history
Fixes #95
  • Loading branch information
Stephan van Rooij committed Jun 3, 2020
1 parent aab0228 commit e2242b1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ A smarthome bridge between your sonos system and a mqtt server.
Usage: index.js [options]

Options:
--prefix instance name. used as mqtt client id and as prefix for
connected topic [default: "sonos"]
--prefix instance name. used as prefix for all topics
[default: "sonos"]
--mqtt mqtt broker url. See
https://github.com/svrooij/sonos2mqtt#mqtt-url
[default: "mqtt://127.0.0.1"]
--clientid Specify the client id to be used
--log Set the loglevel
[choices: "warning", "information", "debug"] [default: "information"]
--distinct Publish distinct track states [boolean] [default: false]
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Config {
discovery: boolean;
discoveryprefix: string;
log: string;
clientid?: string;
}

const defaultConfig: Config = {
Expand Down Expand Up @@ -56,8 +57,9 @@ export class ConfigLoader {
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json')).toString())
return yargs
.usage(pkg.name + ' ' + pkg.version + '\n' + pkg.description + '\n\nUsage: $0 [options]')
.describe('prefix', 'instance name. used as mqtt client id and as prefix for connected topic')
.describe('prefix', 'instance name. used as prefix for all topics')
.describe('mqtt', 'mqtt broker url. See https://github.com/svrooij/sonos2mqtt#mqtt-url')
.describe('clientid', 'Specify the client id to be used')
.describe('log', 'Set the loglevel')
.describe('d', 'Publish distinct track states')
.describe('h', 'show help')
Expand Down
5 changes: 3 additions & 2 deletions src/smarthome-mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class SmarthomeMqtt{
private readonly uri: URL
private mqttClient?: MqttClient;
public readonly Events: StrictEventEmitter<EventEmitter, MqttEvents> = new EventEmitter();
constructor(mqttUrl: string, private readonly prefix: string = 'sonos') {
constructor(mqttUrl: string, private readonly prefix: string = 'sonos', private readonly clientId?: string) {
this.uri = new URL(mqttUrl)
}

Expand All @@ -28,7 +28,8 @@ export class SmarthomeMqtt{
qos: 0,
retain: true
},
keepalive: 60000
keepalive: 60000,
clientId: this.clientId
});
this.mqttClient.on('connect',() => {
this.log.debug('Connected to server {server}', this.uri.host)
Expand Down
2 changes: 1 addition & 1 deletion src/sonos-to-mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class SonosToMqtt {
private readonly stateTimers: {[key: string]: NodeJS.Timeout} = {};
constructor(private config: Config) {
this.sonosManager = new SonosManager();
this.mqtt = new SmarthomeMqtt(config.mqtt, config.prefix);
this.mqtt = new SmarthomeMqtt(config.mqtt, config.prefix, config.clientid);
}

async start(): Promise<boolean> {
Expand Down

0 comments on commit e2242b1

Please sign in to comment.