-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
sonos-to-mqtt.ts
268 lines (244 loc) · 10.4 KB
/
sonos-to-mqtt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { Config } from './config'
import { StaticLogger } from './static-logger'
import { SonosManager, SonosEvents } from '@svrooij/sonos'
import { SmarthomeMqtt } from './smarthome-mqtt';
import { SonosCommandMapping } from './sonos-command-mapping';
import { SonosState } from './sonos-state';
import { AVTransportServiceEvent, RenderingControlServiceEvent } from '@svrooij/sonos/lib/services';
import { SonosCommands } from './sonos-commands';
export class SonosToMqtt {
private readonly sonosManager: SonosManager;
private readonly mqtt: SmarthomeMqtt;
private readonly log = StaticLogger.CreateLoggerForSource('Sonos2mqtt.main')
private readonly states: Array<Partial<SonosState>> = [];
private readonly stateTimers: {[key: string]: NodeJS.Timeout} = {};
constructor(private config: Config) {
this.sonosManager = new SonosManager();
this.mqtt = new SmarthomeMqtt(config.mqtt, config.prefix, config.clientid);
}
async start(): Promise<boolean> {
let success: boolean
if(this.config.device !== undefined) {
success = await this.sonosManager.InitializeFromDevice(this.config.device);
} else {
success = await this.sonosManager.InitializeWithDiscovery();
}
success = success && this.sonosManager.Devices.length > 0;
if (success) {
this.log.info('Found {numDevices} sonos speakers', this.sonosManager.Devices.length)
this.setupMqttEvents()
this.setupSonosEvents()
this.mqtt.connect()
this.mqtt.publishStatus('2');
if(this.config.discovery === true) {
this.publishDiscoveryMessages();
}
}
return success;
}
stop(): void {
this.mqtt.publishStatus('1');
this.mqtt.close();
this.sonosManager.Devices.forEach(d => {
d.CancelEvents()
})
}
/**
* Will setup all events from mqtt with the correct handler.
*/
private setupMqttEvents(): void {
this.log.debug('Setting up mqtt events')
this.mqtt.Events.on('connected', (connected) => {
this.log.info('Mqtt connection changed to connected: {connected}', connected)
})
this.mqtt.Events.on('generic', async (command, payload) => {
this.log.debug('Got generic command {command} from mqtt', command)
switch (command) {
case 'notify':
return this.sonosManager.PlayNotification(payload);
case 'speak':
return this.sonosManager.PlayTTS(payload);
case 'pauseall':
return Promise.all(this.sonosManager.Devices.map(d => d.Pause()));
case 'listalarm': // This typ-o is still there for backward compatibility
case 'listalarms':
const alarms = await this.sonosManager.Devices[0].AlarmClockService.ListAndParseAlarms()
this.mqtt.publish('alarms', alarms);
break;
case 'setalarm':
return this.sonosManager.Devices[0].AlarmClockService.PatchAlarm(payload);
case 'setlogging':
return StaticLogger.setLevel(payload);
case 'check-subscriptions':
return this.sonosManager.CheckAllEventSubscriptions();
}
})
this.mqtt.Events.on('deviceControl', async (uuid, payload)=> {
const correctDevice = this.sonosManager.Devices.find(d => d.Uuid.toLocaleLowerCase() === uuid || SonosToMqtt.CleanName(d.Name) === uuid);
if(correctDevice === undefined) {
this.log.warn('Device {uuid} not found', uuid)
return;
}
try {
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) {
if (e instanceof Error) {
this.log.warn(e, 'Error executing {command} for {device} ({uuid})', payload.command ?? payload.sonosCommand, correctDevice.Name, correctDevice.Uuid)
} else {
this.log.warn('Error executing {command} for {device} ({uuid})', payload.command ?? payload.sonosCommand, correctDevice.Name, correctDevice.Uuid)
}
this.mqtt.publish(`${correctDevice.Uuid}/error`, {
command: payload.command ?? payload.sonosCommand,
error: e
});
}
})
}
/**
* Will setup event handlers from sonos devices, and what to publish
*/
private setupSonosEvents(): void {
this.sonosManager.Devices.forEach(async (d) => {
const deviceDescription = await d.GetDeviceDescription();
this.states.push({uuid: d.Uuid, model: deviceDescription.modelName, name: d.Name, groupName: d.GroupName, coordinatorUuid: d.Coordinator.Uuid})
d.Events.on(SonosEvents.AVTransport, (data) => {
this.updateStateWithAv(d.Uuid, data);
this.mqtt.publish(`status/${this.topicId(d.Name, d.Uuid)}/avtransport`,data)
})
d.Events.on(SonosEvents.RenderingControl, (data) => {
this.updateStateWithRenderingControl(d.Uuid, data);
this.mqtt.publish(`status/${this.topicId(d.Name, d.Uuid)}/renderingcontrol`,data)
})
d.Events.on(SonosEvents.GroupName, (groupName) => {
this.updateState(d.Uuid, { groupName });
if(this.config.distinct === true) {
this.mqtt.publish(`status/${this.topicId(d.Name, d.Uuid)}/group`, groupName)
}
})
d.Events.on(SonosEvents.Coordinator, (coordinatorUuid) => {
this.updateState(d.Uuid, { coordinatorUuid });
if(this.config.distinct === true) {
this.mqtt.publish(`status/${this.topicId(d.Name, d.Uuid)}/coordinator`, coordinatorUuid)
}
})
d.Events.on('transportState', (transportState) => {
this.updateState(d.Uuid, { transportState } );
if (this.config.distinct === true) {
this.mqtt.publish(`status/${this.topicId(d.Name, d.Uuid)}/state`, transportState)
}
})
if(this.config.distinct === true) {
d.Events.on(SonosEvents.CurrentTrackMetadata, (track) => {
this.mqtt.publish(`status/${this.topicId(d.Name, d.Uuid)}/track`, track)
})
d.Events.on(SonosEvents.CurrentTrackUri, (trackUri) => {
this.mqtt.publish(`status/${this.topicId(d.Name, d.Uuid)}/trackUri`, trackUri)
})
d.Events.on(SonosEvents.Mute, (mute) => {
this.mqtt.publish(`status/${this.topicId(d.Name, d.Uuid)}/muted`, mute)
})
d.Events.on(SonosEvents.Volume, (volume) => {
this.mqtt.publish(`status/${this.topicId(d.Name, d.Uuid)}/volume`, volume)
})
}
})
}
private publishDiscoveryMessages(): void {
this.sonosManager.Devices.forEach(d => {
const payload = {
available_commands: Object.values(SonosCommands),
command_topic: `${this.config.prefix}/${d.Uuid}/control`,
device: {
identifiers: [d.Uuid],
manufacturer: 'Sonos',
// model: '', Model not available at the moment (but would be nice if added in the sonos lib)
name: d.Name
},
device_class: 'speaker',
icon: 'mdi:speaker',
json_attributes: true,
json_attributes_topic: `${this.config.prefix}/${d.Uuid}`,
name: d.Name,
state_topic: `${this.config.prefix}/${d.Uuid}`,
unique_id: `sonos2mqtt_${d.Uuid}_speaker`,
availability_topic: `${this.config.prefix}/connected`,
payload_available: '2'
};
this.mqtt.publishAutodiscovery(this.config.discoveryprefix ?? 'homeassistant', d.Uuid, payload);
});
}
/**
* The data from the AVTransportService event is merged with the state object, and the published to mqtt.
*
* @private
* @param {string} uuid The UUID of the player to lookup the correct player
* @param {AVTransportServiceEvent} data All event data
* @memberof SonosToMqtt
*/
private updateStateWithAv(uuid: string, data: AVTransportServiceEvent): void {
this.updateState(uuid, {
currentTrack: data.CurrentTrackMetaData,
enqueuedMetadata: data.EnqueuedTransportURIMetaData,
nextTrack: data.NextTrackMetaData,
// transportState: data.TransportState,
playmode: data.CurrentPlayMode,
})
}
/**
* The data from the RenderingControlService event is merged with the state object, and the published to mqtt.
*
* @private
* @param {string} uuid The UUID of the player to lookup the correct player
* @param {RenderingControlServiceEvent} data All event data
* @memberof SonosToMqtt
*/
private updateStateWithRenderingControl(uuid: string, data: RenderingControlServiceEvent): void {
this.updateState(uuid, {
volume: data.Volume,
mute: data.Mute,
bass: data.Bass,
treble: data.Treble
})
}
/**
* Take partial state to update the current and publish to mqtt.
*
* @private
* @param {string} uuid The UUID of the player to update.
* @param {Partial<SonosState>} update partial state.
* @memberof SonosToMqtt
*/
private updateState(uuid: string, update: Partial<SonosState>): void {
const index = this.states.findIndex(s => s.uuid === uuid)
if(index !== -1) {
// Merge update over the current state (only fields with value are updated)
// The following would also remove values if undefined in update. Anyone has a better solution, please?
//this.states[index] = {...this.states[index], ...update};
// Enumarate update object entries and only copy values where it has a value
for(const [key, value] of Object.entries(update)) {
if(value !== undefined) {
this.states[index][key] = value;
}
}
// Change timestamp to now
this.states[index].ts = Date.now();
// Clear publish timeout if set. This also means that if a player keeps sending updates within 400 ms there will not be an event (possible bug).
if(this.stateTimers[uuid] !== undefined) clearTimeout(this.stateTimers[uuid]);
// Publish new state on a 400 ms delay. For changes that happen in rapid succession.
this.stateTimers[uuid] = setTimeout(() => {
this.log.verbose('Publishing state for {uuid}', this.states[index].uuid)
this.mqtt.publish(this.states[index].uuid ?? '', this.states[index], { qos: 0, retain: true });
}, 400)
}
}
private topicId(name: string, uuid: string): string {
return this.config.friendlynames === 'name' ? SonosToMqtt.CleanName(name) : uuid;
}
private static CleanName (name: string): string {
return name.toLowerCase().replace(/\s/g, '-')
}
}