module.exports = function(HAPnode, config, functions)
{
    var Accessory       = HAPnode.Accessory;
    var Service         = HAPnode.Service;
    var Characteristic  = HAPnode.Characteristic;
    var uuid            = HAPnode.uuid;
    var debug           = HAPnode.debug;

    var module  = {};

    module.newDevice = function(device)
    {
        var Dimmer = {

            setPowerOn: function(state)
            {
                debug("Making POWER request for device %s", device.name);
                state = state ? 1:0;
                var service = 'urn:upnp-org:serviceId:SwitchPower1',
                    action = 'SetTarget',
                    value = state;
                return functions.executeAction({
                  action: action,
                  serviceId: service,
                  newTargetValue: value,
                  DeviceNum: device.id
                }).then(function(response){
                  debug("Light Power set");
                  var index = functions.cache.devices.findIndex(function(cacheDevice, index){
                    return (device.id === cacheDevice.id);
                  });
                  functions.cache.devices[index].status = value;
                })
            },
            setBrightness: function(brightness)
            {
                debug("Making BRIGHTNESS request for device %s", device.name, brightness, parseInt(brightness));
                var service = 'urn:upnp-org:serviceId:Dimming1',
                    action = 'SetLoadLevelTarget',
                    value = parseInt(brightness);
                return functions.executeAction({
                  action: action,
                  serviceId: service,
                  newLoadlevelTarget: value,
                  DeviceNum: device.id
                }).then(function(response){
                  debug("Light Brightness set");
                  var index = functions.cache.devices.findIndex(function(cacheDevice, index){
                    return (device.id === cacheDevice.id);
                  });
                  functions.cache.devices[index].level = value;
                  // debug(response['u:SetTargetResponse']);
                });
            },
            getStatus: function()
            {
                debug("Making status request for device %s", device.name);
                var status = parseInt(functions.getVariable(device.id, 'status'));
                debug("Status is ", status);
                return status;
            },
            getBrightness: function()
            {
              debug("Making level request for device %s", device.name);
              level = parseInt(functions.getVariable(device.id, 'level'));
              debug("Level is ", level);
              return level;
            },
            identify: function()
            {
                debug("Identify the light %s", device.name);
            }
        };

        var lightUUID = uuid.generate('device:dimmer:'+config.cardinality+':'+device.id);

        var light = new Accessory(device.name, lightUUID);

        light.username  = functions.genMac('device:'+config.cardinality+':'+device.id);
        light.pincode   = config.pincode;
        light.deviceid  = device.id;

        light
            .getService(Service.AccessoryInformation)
            .setCharacteristic(Characteristic.Manufacturer, "Oltica")
            .setCharacteristic(Characteristic.Model, "Rev-1")
            .setCharacteristic(Characteristic.SerialNumber, "A1S2NASF88EW");

        light.on('identify', function(paired, callback) {
            Dimmer.identify();
            callback(); // success
        });

        light
            .addService(Service.Lightbulb, device.name)
            .getCharacteristic(Characteristic.On)
            .on('set', function(value, callback) {
                debug("STATUS:", Dimmer.getStatus());
                if(Dimmer.getStatus() !== value){
                  Dimmer.setPowerOn(value).then(function(value){
                    callback();
                  }).catch(function(){
                    callback();
                  })
                }else{
                  callback();
                }

            });

        light
            .getService(Service.Lightbulb)
            .getCharacteristic(Characteristic.On)
            .on('get', function(callback) {
                var err = null;
                callback(err, Dimmer.getStatus());
                });

        light
            .getService(Service.Lightbulb)
            .addCharacteristic(Characteristic.Brightness)
            .on('get', function(callback) {
                var err = null;
                callback(err, Dimmer.getBrightness());
            })
            .on('set', function(value, callback) {
                debug("BRIGHTNESS:", Dimmer.getBrightness());
                Dimmer.setBrightness(value).then(function(value){
                  callback();
                }).catch(function(){
                  callback();
                })

            });

        return light;

    };

    return module;
};

@@ -3849,95 +3849,218 @@
     
-debug(%22STATUS:%22, Dimmer.getStatus());%0A                if(Dimmer.getStatus() !== 
+if(value)%7B%0A                  var bulb = light.getService(Service.Lightbulb),%0A                      brightness = bulb.getCharacteristic(Characteristic.Brightness),%0A                      value = brightness.
 value
-)%7B
+;
 %0A   
@@ -4084,23 +4084,26 @@
 mmer.set
-PowerOn
+Brightness
 (value).
@@ -4145,32 +4145,43 @@
        callback(
+null, value
 );%0A             
@@ -4187,34 +4187,16 @@
       %7D)
-.catch(function()%7B
 %0A       
@@ -4200,39 +4200,30 @@
             
-    callback();
+%7Delse%7B
 %0A           
@@ -4233,33 +4233,97 @@
     
-%7D)%0A                %7Delse%7B
+Dimmer.setPowerOn(value).then(function(value)%7B%0A                    callback(null, value);
 %0A   
@@ -4333,35 +4333,26 @@
             
-callback();
+%7D)
 %0A           
@@ -4358,17 +4358,16 @@
       %7D%0A
-%0A
         
@@ -4946,70 +4946,8 @@
 ) %7B%0A
-                debug(%22BRIGHTNESS:%22, Dimmer.getBrightness());%0A
     
@@ -5027,32 +5027,43 @@
        callback(
+null, value
 );%0A             
@@ -5113,16 +5113,27 @@
 allback(
+null, value
 );%0A     

refactor dimmers to remember dimmer value for power-on, potentially resolve zone issues