ParadoxAlarmInterface / pai

Paradox Magellan, Spectra and EVO, with MQTT, Signal, Pushbullet, Pushover and others
https://gitter.im/paradox-alarm-interface
Eclipse Public License 2.0
351 stars 94 forks source link

MQTT Interface topic not updating to "Offline' state #55

Closed cdk222 closed 5 years ago

cdk222 commented 5 years ago

System Paradox MG5050 v4.133 IP150 firmware 1.39 Using Dev branch

Describe the bug The state/value in the 'paradox/interface/MQTTInterface' topic does not change to 'offline' when you disconnect/stop the pai script. I also tested this by entering in the wrong user/pass MQTT credentials in the pai.conf thus causing the script to not connect MQTT on startup. In each case the state is always "Online". The topic does seem to update the "Online" state though without issue.

To Reproduce Steps to reproduce the behavior (2 methods): METHOD 1:

  1. Start script
  2. Control C to disconnect/stop the script
  3. View the 'paradox/interface/MQTTInterface' topic value in MQTT client

METHOD 2:

  1. Modify MQTT username or password credentials in pai.conf so that they are incorrect
  2. Start script
  3. View the 'paradox/interface/MQTTInterface' topic value in MQTT client
jpbarraca commented 5 years ago

Method 2 should not change the value as it will not connect correctly.

Method 1 should work as we are setting a last will.

What broker are you using?

cdk222 commented 5 years ago

I am using Mosquitto MQTT on a pi - same pi and the paradox scripts.

True on Method 2

cdk222 commented 5 years ago

Hi - thanks for working on this. I have loaded the changes and the issue is still present. The MQTTInterface remains in Online state. I have also noted the below error appearing (from my log - in DEBUG mode) at the disconnect

2019-03-14 18:35:12,285 - INFO - PAI.paradox.paradox - Disconnecting from the Alarm Panel 2019-03-14 18:35:12,454 - ERROR - PAI.paradox.paradox - Got ERROR Message: Requested command did not work 2019-03-14 18:35:12,456 - DEBUG - PAI.paradox.interfaces.interface_manager - Stopping all interfaces 2019-03-14 18:35:12,457 - DEBUG - PAI.paradox.interfaces.interface_manager - mqtt 2019-03-14 18:35:12,459 - DEBUG - PAI.paradox.interfaces.mqtt_interface - Stopping MQTT Interface 2019-03-14 18:35:12,461 - INFO - PAI.paradox.interfaces.mqtt_interface - MQTT Broker Disconnected 2019-03-14 18:35:13,469 - DEBUG - PAI.paradox.interfaces.interface_manager - All Interfaces stopped 2019-03-14 18:35:13,470 - INFO - PAI - Good bye!

cdk222 commented 5 years ago

The reason I would like to get this working is that I have an availability sensor attached to the topic:

paradox/interface/MQTTInterface

so that it will report when the paradox mqtt connection has disconnected for an unknown reason

jpbarraca commented 5 years ago

Your scenario is perfectly valid. I tried to replicate the issue, but after the patch it seems to work properly, and I cannot replicate it...

paradox/interface/MQTTInterface online  # started
paradox/interface/MQTTInterface offline  # Stopped with CTRL-C
paradox/interface/MQTTInterface online  # started
paradox/interface/MQTTInterface offline  # Stopped with Kill
paradox/interface/MQTTInterface online  # Started with systemctl
paradox/interface/MQTTInterface offline  # Stopped with systemctl
cdk222 commented 5 years ago

Any ideas on what causes the following message:

2019-03-14 18:35:12,454 - ERROR - PAI.paradox.paradox - Got ERROR Message: Requested command did not work

It appears just after the disconnection message

jpbarraca commented 5 years ago

We sent a command to the panel and there was some error. I will need a full debug with messages and packets (use email, keybase, gitter)

cdk222 commented 5 years ago

Ok so after a lot of trial and error I was able to rectify this as the above fix did not work for me. I relocated the 'offline' state change in the _mqttinterface.py file so that it was called in the 'stop' function and not _'handledisconnect'. It appeared that the MQTT connection was already disconnected by the time the topic change to offline was called for. I also changed to '_self.mqtt.willset' to 'self.publish'. Below is the change:

`

def stop(self):
    """ Stops the MQTT Interface Thread"""
    self.publish('{}/{}/{}'.format(cfg.MQTT_BASE_TOPIC,
                                   cfg.MQTT_INTERFACE_TOPIC,
                                   self.__class__.__name__),
                 'offline', 0, cfg.MQTT_RETAIN)
    self.mqtt.disconnect()
    self.logger.debug("Stopping MQTT Interface")
    self.queue.put_nowait(SortableTuple((0, 'command', 'stop')))
    self.mqtt.loop_stop()
    self.join()

    `

Tested in my client and state now changes correctly

cdk222 commented 5 years ago

Hi there

Just messaged you on Gitter about the Paradox asyncio code and the error it gave me when trying to connect to the panel. Attached is my log file.

cheers

Chad

On Thu, Mar 14, 2019 at 10:33 PM João Paulo Barraca < notifications@github.com> wrote:

We sent a command to the panel and there was some error. I will need a full debug with messages and packets (use email, keybase, gitter)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jpbarraca/pai/issues/55#issuecomment-472887302, or mute the thread https://github.com/notifications/unsubscribe-auth/ArsFvfiaais-KjceeflxFHwjnIEn4sGEks5vWl2kgaJpZM4brMS1 .

yozik04 commented 5 years ago

asyncio is work in progress... Do not use it yet.

yozik04 commented 5 years ago

I think this one is fixed in dev branch. Non-retained version is also in master.

Reopen if not.

yellowonblack commented 5 years ago

Still not working for me https://github.com/jpbarraca/pai/issues/57#issuecomment-479912846

yozik04 commented 5 years ago

@yellowonblack and you are on latest dev branch?

Can you send MQTT log? To email if sensitive.

mosquitto_sub -u <user> -P <password> -t paradox/# -v

yellowonblack commented 5 years ago

Just sent you, both PAI and MQTT logs for dev branch commit 60249ec