steilerDev / homebridge-openhab2-complete

A homebridge plugin for openHAB, that has the expectation to fully support all Services offered by Apple's Homekit Accessory Protocol (HAP)
GNU General Public License v3.0
52 stars 16 forks source link

Fix for issue #13 and #43 #45

Closed grzegorz914 closed 4 years ago

grzegorz914 commented 4 years ago

Hi, this fixed issue #13 and #43 Tested with HomeKit app and Siri.

Rule in OH2:

rule "Oświetlenie kuchnia sufit dimmer on/off i zmiana poziomu"
when
    Item KuchniaSufitDimmer received command
then
      if (!(receivedCommand instanceof Number))
        switch receivedCommand {
            case ON :    if (PLC_KuchniaSufitSwiatlo.state == OFF) {
                                 PLC_KuchniaSufitTaster.sendCommand(ON)
                                 PLC_KuchniaSufitTaster.sendCommand(OFF)}
            case OFF  :  if (PLC_KuchniaSufitSwiatlo.state == ON) {
                                 PLC_KuchniaSufitTaster.sendCommand(ON)
                                 PLC_KuchniaSufitTaster.sendCommand(OFF)}   
                               }
      else 
           if (receivedCommand instanceof Number && (receivedCommand != 0 && receivedCommand != 100))
                       PLC_KuchniaSufitDimmer.sendCommand(receivedCommand)
end  

rule "Oświetlenie kuchnia sufit"
when
    Item PLC_KuchniaSufitSwiatlo received command
then
         PLC_KuchniaSufitTaster.sendCommand(ON)
         PLC_KuchniaSufitTaster.sendCommand(OFF)
end

rule "Oświetlenie kuchnia sufit ustawienie poziomu po załączeniu"
when
    Item PLC_KuchniaSufitSwiatlo changed
then
      if (PLC_KuchniaSufitSwiatlo.state == ON) {
          KuchniaSufitDimmer.postUpdate(PLC_KuchniaSufitDimmer.state)
        } else if (PLC_KuchniaSufitSwiatlo.state == OFF) {
                   KuchniaSufitDimmer.postUpdate(OFF) 
        }       
end

rule "Oświetlenie kuchnia sufit dimmer aktualizacja poziomu"
when
    Item PLC_KuchniaSufitDimmer changed
then
      var Number poziom1 = PLC_KuchniaSufitDimmer.state
      if (PLC_KuchniaSufitSwiatlo.state == ON && (KuchniaSufitDimmer.state != PLC_KuchniaSufitDimmer.state)) {
          KuchniaSufitDimmer.postUpdate(poziom1)
      } 
end

Command - HomeKit app:

Press button - switch ON the light:

2019-08-26 17:56:20.264 [ome.event.ItemCommandEvent] - Item 'KuchniaSufitDimmer' received command ON
2019-08-26 17:56:20.272 [vent.ItemStateChangedEvent] - KuchniaSufitDimmer changed from 0 to 100
2019-08-26 17:56:20.273 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitTaster' received command ON
2019-08-26 17:56:20.275 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitTaster' received command OFF
2019-08-26 17:56:23.815 [vent.ItemStateChangedEvent] - PLC_KuchniaSufitSwiatlo changed from OFF to ON

Update brightness to the last state:

2019-08-26 17:56:23.846 [vent.ItemStateChangedEvent] - KuchniaSufitDimmer changed from 100 to 30

Change brightness using dimer:

2019-08-26 17:56:35.145 [ome.event.ItemCommandEvent] - Item 'KuchniaSufitDimmer' received command 58
2019-08-26 17:56:35.155 [vent.ItemStateChangedEvent] - KuchniaSufitDimmer changed from 30 to 58
2019-08-26 17:56:35.163 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitDimmer' received command 58
2019-08-26 17:56:35.376 [ome.event.ItemCommandEvent] - Item 'KuchniaSufitDimmer' received command 47
2019-08-26 17:56:35.384 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitDimmer' received command 47
2019-08-26 17:56:35.581 [vent.ItemStateChangedEvent] - PLC_KuchniaSufitDimmer changed from 30 to 58
2019-08-26 17:56:36.082 [vent.ItemStateChangedEvent] - KuchniaSufitDimmer changed from 58 to 47
2019-08-26 17:56:36.085 [vent.ItemStateChangedEvent] - PLC_KuchniaSufitDimmer changed from 58 to 47

Press button - switch OFF the light:

2019-08-26 17:56:41.031 [ome.event.ItemCommandEvent] - Item 'KuchniaSufitDimmer' received command OFF
2019-08-26 17:56:41.036 [vent.ItemStateChangedEvent] - KuchniaSufitDimmer changed from 47 to 0
2019-08-26 17:56:41.039 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitTaster' received command ON
2019-08-26 17:56:41.042 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitTaster' received command OFF

Command - Siri:

Siri switch ON the light:

2019-08-27 07:48:12.828 [ome.event.ItemCommandEvent] - Item 'KuchniaSufitDimmer' received command ON
2019-08-27 07:48:12.840 [vent.ItemStateChangedEvent] - KuchniaSufitDimmer changed from 0 to 100
2019-08-27 07:48:12.843 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitTaster' received command ON
2019-08-27 07:48:12.848 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitTaster' received command OFF
2019-08-27 07:48:14.591 [vent.ItemStateChangedEvent] - PLC_KuchniaSufitSwiatlo changed from OFF to ON

Update the brightness to the last state:

2019-08-27 07:48:14.627 [vent.ItemStateChangedEvent] - KuchniaSufitDimmer changed from 100 to 47

Siri set the light to 20%:

2019-08-27 07:48:24.005 [ome.event.ItemCommandEvent] - Item 'KuchniaSufitDimmer' received command 20
2019-08-27 07:48:24.014 [vent.ItemStateChangedEvent] - KuchniaSufitDimmer changed from 47 to 20
2019-08-27 07:48:24.024 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitDimmer' received command 20
2019-08-27 07:48:24.468 [vent.ItemStateChangedEvent] - PLC_KuchniaSufitDimmer changed from 47 to 20

Siri switch OFF the light:

2019-08-27 07:48:34.162 [ome.event.ItemCommandEvent] - Item 'KuchniaSufitDimmer' received command OFF
2019-08-27 07:48:34.169 [vent.ItemStateChangedEvent] - KuchniaSufitDimmer changed from 20 to 0
2019-08-27 07:48:34.172 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitTaster' received command ON
2019-08-27 07:48:34.176 [ome.event.ItemCommandEvent] - Item 'PLC_KuchniaSufitTaster' received command OFF
2019-08-27 07:48:35.743 [vent.ItemStateChangedEvent] - PLC_KuchniaSufitSwiatlo changed from ON to OFF
grzegorz914 commented 4 years ago

Hi, after some test Your code not fixed #43, fixed only #13. With Your code send value 100 or 0/OFF, with my code send ON/100 and 0/OFF always. If U look on my rule the state ON enabled light and set to the latest state of brightness(value 100 is ignored), with Your code the light is enabled with 100% of brightness always, because You do not send ON command if value is 100.

Check this code please:

        this._log.debug(`Committing light state with vectors (B,H,S,B): ${binary},${hue},${saturation},${brightness}`);
        if(binary === undefined && hue === undefined && saturation === undefined && brightness === undefined) {
            command = new Error("Unable to commit state, since necessary information are missing");
        } else if (hue === undefined && saturation === undefined && brightness === undefined) {
            command = binary ? "ON" : "OFF";
        } else if (hue === undefined && saturation === undefined) {
            if(binary && (brightness === 0 || brightness === 100)) { // For some reaason when invoking Siri to turn on a light brightness is sometimes set to '0'
                command = "ON";
            } else {
                command = binary ? `${brightness}` : "OFF";
            }
        } else {
            if(binary && (brightness === 0 || brightness === 100)) { // For some reaason when invoking Siri to turn on a light brightness is sometimes set to '0'
                command = "ON";
            } else {
                command = binary ? `${hue},${saturation},${brightness}` : "OFF";
            }
        }
    }
    return command;