Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Donavan Becker committed Oct 2, 2020
2 parents ae5c691 + 59f6164 commit a1f878e
Showing 1 changed file with 159 additions and 1 deletion.
160 changes: 159 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Meross {
config: any;
service: any;
isOn: any;
brightness: any;
currentState: any;
lastSetTime!: number;
checkStateInterval!: NodeJS.Timeout;
Expand Down Expand Up @@ -111,11 +112,13 @@ class Meross {
case 'MSS510':
case 'MSS510M':
case 'MSS550':
case 'MSS560':
case 'MSS570':
case 'MSS5X0':
this.service = new Service.Switch(this.config.name);
break;
case 'MSS560':
this.service = new Service.Lightbulb(this.config.name);
break;
case 'MSG100':
this.service = new Service.GarageDoorOpener(this.config.name);
this.startUpdatingDoorState();
Expand Down Expand Up @@ -155,6 +158,16 @@ class Meross {
.getCharacteristic(Characteristic.ObstructionDetected)
.on('get', this.getObstructionDetectedHandler.bind(this));
break;
case 'MSS560':
this.service
.getCharacteristic(Characteristic.Brightness)
.on('get', this.getBrightnessCharacteristicHandler.bind(this))
.on('set', this.setBrightnessCharacteristicHandler.bind(this));
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOnCharacteristicHandler.bind(this))
.on('set', this.setOnCharacteristicHandler.bind(this));
break;
default:
this.service
.getCharacteristic(Characteristic.On)
Expand Down Expand Up @@ -356,6 +369,151 @@ class Meross {
callback(null, this.isOn);
}


async setBrightnessCharacteristicHandler(value, callback) {
/* this is called when HomeKit wants to update the value of the characteristic as defined in our getServices() function */
/* deviceUrl only requires ip address */

//this.log(this.config, this.config.deviceUrl);
let response;

/* Log to the console whenever this function is called */
this.log.debug(
`calling setBrightnessCharacteristicHandler for ${this.config.model} at ${this.config.deviceUrl}...`,
);

/*
* Differentiate requests based on device model.
*/

switch (this.config.model) {
default:
try {
response = await doRequest({
json: true,
method: 'POST',
strictSSL: false,
url: `http://${this.config.deviceUrl}/config`,
headers: {
'Content-Type': 'application/json',
},
body: {
payload: {
light: {
luminance: value,
},
},
header: {
messageId: `${this.config.messageId}`,
method: 'SET',
from: `http://${this.config.deviceUrl}\/config`,
namespace: 'Appliance.Control.Light',
timestamp: this.config.timestamp,
sign: `${this.config.sign}`,
payloadVersion: 1,
},
},
});
} catch (e) {
this.log(
`Failed to POST to the Meross Device ${this.config.model} at ${this.config.deviceUrl}:`,
e,
);
}
}

if (response) {
this.brightness = value;
this.log.debug('Set succeeded:', response);
this.log(`${this.config.model} set brightness to`, value);
} else {
this.brightness = this.isOn ? 100 : 0;
this.log('Set failed:', this.brightness);
}

/* Log to the console the value whenever this function is called */
this.log.debug('setBrightnessCharacteristicHandler:', value);

/*
* The callback function should be called to return the value
* The first argument in the function should be null unless and error occured
*/
callback(null, this.brightness);
}

async getBrightnessCharacteristicHandler(callback) {
/*
* this is called when HomeKit wants to retrieve the current state of the characteristic as defined in our getServices() function
* it's called each time you open the Home app or when you open control center
*/

//this.log(this.config, this.config.deviceUrl);
let response;

/* Log to the console whenever this function is called */
this.log.debug(
`calling getBrightnessCharacteristicHandler for ${this.config.model} at ${this.config.deviceUrl}...`,
);

try {
response = await doRequest({
json: true,
method: 'POST',
strictSSL: false,
url: `http://${this.config.deviceUrl}/config`,
headers: {
'Content-Type': 'application/json',
},
body: {
payload: {},
header: {
messageId: `${this.config.messageId}`,
method: 'GET',
from: `http://${this.config.deviceUrl}/config`,
namespace: 'Appliance.System.All',
timestamp: this.config.timestamp,
sign: `${this.config.sign}`,
payloadVersion: 1,
},
},
});
} catch (e) {
this.log(
`Failed to POST to the Meross Device ${this.config.model} at ${this.config.deviceUrl}:`,
e,
);
}

/*
* Differentiate response based on device model.
*/

switch (this.config.model) {
default:
if (response) {
const luminance =
response.payload.all.digest.light.luminance;

this.log.debug('Retrieved status successfully: ', luminance);
this.brightness = luminance;
} else {
this.log.debug('Retrieved status unsuccessfully.');
this.brightness = this.isOn ? 100 : 0;
}
}

/* Log to the console the value whenever this function is called */
this.log.debug('getBrightnessCharacteristicHandler:', this.brightness);

/*
* The callback function should be called to return the value
* The first argument in the function should be null unless and error occured
* The second argument in the function should be the current value of the characteristic
* This is just an example so we will return the value from `this.brightness` which is where we stored the value in the set handler
*/
callback(null, this.brightness);
}

async getDoorStateHandler(callback) {
/*
* this is called when HomeKit wants to retrieve the current state of the characteristic as defined in our getServices() function
Expand Down

0 comments on commit a1f878e

Please sign in to comment.