/* eslint unicorn/filename-case: "off", func-names: "off", camelcase: "off", no-unused-vars: "off" */

module.exports = function (iface) {
    const {mqttPub, mqttSub, mqttStatus, log, newAccessory, Service, Characteristic} = iface;

    return function createAccessory_Speaker(settings) {
        const speaker = newAccessory(settings);

        speaker.addService(Service.Speaker, settings.name)
            .getCharacteristic(Characteristic.Mute)
            .on('set', (value, callback) => {
                log.debug('< hap set', settings.name, 'Mute', value);
                const mute = value ? settings.payload.muteTrue : settings.payload.muteFalse;
                log.debug('> mqtt', settings.topic.setOn, mute);
                mqttPub(settings.topic.setMute, mute);
                callback();
            });

        // Update status in homekit if exernal status gets updated
        mqttSub(settings.topic.statusMute, val => {
            const mute = val !== settings.topic.muteFalse;
            log.debug('> hap update', settings.name, 'Mute', mute);
            speaker.getService(Service.Speaker)
                .updateCharacteristic(Characteristic.Mute, mute);
        });

        speaker.getService(Service.Speaker)
            .getCharacteristic(Characteristic.Mute)
            .on('get', callback => {
                log.debug('< hap get', settings.name, 'Mute');
                const mute = mqttStatus[settings.topic.statusMute] !== settings.payload.muteFalse;
                log.debug('> hap re_get', settings.name, 'Mute', mute);
                callback(null, mute);
            });

        if (settings.topic.setVolume) {
            speaker.getService(Service.Speaker)
                .addCharacteristic(Characteristic.Volume)
                .on('set', (value, callback) => {
                    log.debug('< hap set', settings.name, 'Volume', value);
                    const volume = (value * (settings.payload.volumeFactor || 1)) || 0;
                    log.debug('> mqtt', settings.topic.setVolume, volume);
                    mqttPub(settings.topic.setVolume, volume);
                    callback();
                });

            if (settings.topic.statusVolume) {
                mqttSub(settings.topic.statusVolume, val => {
                    log.debug('> hap update', settings.name, 'Volume', mqttStatus[settings.topic.statusVolume]);
                    speaker.getService(Service.Speaker)
                        .updateCharacteristic(Characteristic.Volume);
                });

                speaker.getService(Service.Speaker)
                    .getCharacteristic(Characteristic.Volume)
                    .on('get', callback => {
                        log.debug('< hap get', settings.name, 'Volume');
                        const volume = (mqttStatus[settings.topic.statusVolume] / (settings.payload.volumeFactor || 1)) || 0;

                        log.debug('> hap re_get', settings.name, 'Volume', volume);
                        callback(null, volume);
                    });
            }
        }

        return speaker;
    };
};

@@ -809,32 +809,77 @@
            %7D);%0A%0A
+        if (settings.topic.statusMute) %7B%0A    
         // Updat
@@ -929,32 +929,36 @@
 updated%0A        
+    
 mqttSub(settings
@@ -989,32 +989,36 @@
 %3E %7B%0A            
+ 
+   
 const mute = val
@@ -1040,32 +1040,36 @@
 opic.muteFalse;%0A
+    
             log.
@@ -1124,32 +1124,36 @@
 e);%0A            
+   
+ 
 speaker.getServi
@@ -1164,32 +1164,36 @@
 ervice.Speaker)%0A
+    
                 
@@ -1242,35 +1242,49 @@
  mute);%0A        
-%7D);
+    %7D);%0A        %7D
 %0A%0A        speake

fix #35