Skip to content

Commit

Permalink
MQTT updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kreso975 committed Sep 14, 2024
1 parent 84621bb commit 8b6c4b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@
[![Donate](https://img.shields.io/badge/donate-PayPal-blue.svg)](https://paypal.me/kreso975)

This plugin communicate with your devices over HTTP. Currently it supports Switches and Temperature/Humidity sensor.
Temperature and Humidity can also be read using basic MQTT functionality.<br><br>
Temperature, Humidity can also be read using basic MQTT functionality. Switch can be used with MQTT as well.<br><br>


## 💡 Switch
> [!NOTE]
> Read Status (On/Off), Turn ON (url), Turn OFF (url)
> HTTP - Read Status (On/Off), Turn ON (url), Turn OFF (url)
> MQTT - Turn ON/OFF
> [!TIP]
> If you don't have Manual switch and you don't mind when Homebridge is rebooted, your device is going to be set as OFF
> then you don't have to use Parameter urlStatus.
> [!IMPORTANT]
> Parameters required in Config:
> Use HTTP or MQTT not both for same accessory.
>
> Parameters required in Config:
>
> deviceType = 'Switch',
> deviceName = 'Name your Accessory',
Expand Down
2 changes: 1 addition & 1 deletion src/platformSensorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class platformSensors {
this.platform.log(this.deviceName,': Subscribed to: ', mqttSubscribedTopics.toString());
} else {
// Need to insert error handler
this.platform.log(err.toString());
this.platform.log(this.deviceName, err.toString());
}
});
});
Expand Down
27 changes: 14 additions & 13 deletions src/platformSwitchServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class platformSwitch {


if ( !this.deviceType) {
this.platform.log.warn('Ignoring accessory; No deviceType defined.');
this.platform.log.warn(this.deviceName,': Ignoring accessory; No deviceType defined.');
return;
}

Expand Down Expand Up @@ -125,6 +125,10 @@ export class platformSwitch {
}
}

getStatus(isOn: boolean): string {
return isOn ? 'ON' : 'OFF';
}

/**
* Handle "SET" requests from HomeKit
* These are sent when the user changes the state of an accessory, for example, turning on a Light bulb.
Expand All @@ -135,18 +139,18 @@ export class platformSwitch {
this.switchStates.On = value as boolean;

if (!this.urlON || !this.urlOFF) {
this.platform.log.warn('Ignoring request; No Switch trigger url defined.');
this.platform.log.warn(this.deviceName,': Ignoring request; No Switch trigger url defined.');
callback(new Error('No Switch trigger url defined.'));
return;
}

if (this.switchStates.On) {
this.url = this.urlON;
this.platform.log.debug('Setting power state to ON');
this.platform.log.debug(this.deviceName,': Setting power state to ON');
this.service.updateCharacteristic(this.platform.Characteristic.On, true);
} else {
this.url = this.urlOFF;
this.platform.log.debug('Setting power state to OFF');
this.platform.log.debug(this.deviceName,': Setting power state to OFF');
this.service.updateCharacteristic(this.platform.Characteristic.On, false);
}

Expand All @@ -162,22 +166,22 @@ export class platformSwitch {
this.service.updateCharacteristic(this.platform.Characteristic.On, !this.switchStates.On);
this.switchStates.On = !value;
//this.platform.log.debug('Setting power state to :', this.switchStates.On );
this.platform.log.warn('Setting power state to :', this.switchStates.On );
this.platform.log.warn(this.deviceName,': Setting power state to :', this.switchStates.On );
//this.platform.log.debug('Error: ', error);
this.platform.log.warn(this.deviceName,': Error: ', error.message);
//callback(error);
});

callback(null);
//this.platform.log.debug('Success: Switch ',this.deviceName,' is: ', value);
this.platform.log.info('Success: Switch ',this.deviceName,' is: ', this.switchStates.On);
this.platform.log.info('Success: Switch ',this.deviceName,' is: ', this.getStatus(this.switchStates.On));
}

async getOn() {
// Check if we have Status URL setup
// this.platform.log.debug(this.accessory.context.device.urlStatus);
if (!this.urlStatus) {
this.platform.log.warn('Ignoring request; No status url defined.');
this.platform.log.warn(this.deviceName,': Ignoring request; No status url defined.');
return;
}

Expand All @@ -197,14 +201,14 @@ export class platformSwitch {

if( this.switchStates.On !== true ) {
this.switchStates.On = true;
this.platform.log.info('Switch is ON');
this.platform.log.info(this.deviceName,': Switch is ON');
this.service.updateCharacteristic(this.platform.Characteristic.On, true);
}
} else if ( data[this.statusStateParam] === this.statusOffCheck ) {

if( this.switchStates.On !== false ) {
this.switchStates.On = false;
this.platform.log.info('Switch is OFF');
this.platform.log.info(this.deviceName,': Switch is OFF');
this.service.updateCharacteristic(this.platform.Characteristic.On, false);
}
} else {
Expand All @@ -219,9 +223,6 @@ export class platformSwitch {
}
}

getStatus(isOn: boolean): string {
return isOn ? 'ON' : 'OFF';
}
//
// Connect to MQTT and update Switches
initMQTT() {
Expand Down Expand Up @@ -252,7 +253,7 @@ export class platformSwitch {
this.platform.log(this.deviceName,': Subscribed to: ', mqttSubscribedTopics.toString());
} else {
// Need to insert error handler
this.platform.log(err.toString());
this.platform.log(this.deviceName, err.toString());
}
});
});
Expand Down

0 comments on commit 8b6c4b1

Please sign in to comment.