-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
is_connected raises MMQTTException when not connected, rather that returning False. #109
Comments
I too find this annoying. I'm writing a library that needs to set the will/testament before making a connection (without knowing the prior state of connection). The fact that try:
if self.client.is_connected():
self.client.disconnect()
except MMQTTException:
pass # this means the client was/is disconnected
self.client.will_set(topic, msg)
self.client.connect(**mqtt_settings) I could write this without try:
self.client.disconnect()
except MMQTTException:
pass # this means the client was/is disconnected
self.client.will_set(topic, msg)
self.client.connect(**mqtt_settings) But at that point, what is the actual utility of |
git blames 41bc399 which seems to prefer internally using the method as an assertion rather than a getter. |
I would like to have this return a boolean instead of raising an error so that I can use it in a loop. Since there is already a breaking change pull request in for making <init> key word only, perhaps this is a good opportunity to change this behaviour. |
The
The code calls In if self._is_connected:
raise MMQTTException("Last Will should only be called before connect().") From reading through the code, I conclude that the Because the method is named Therefore I think that changing Hope this helps! |
Being that the intent of this library is to have a minimal footprint ("mini" is in the name), I think it's fine to let As you say, users can call While I value your observation, the purpose of this thread is to allow using |
I agree with you. Perhaps also renaming |
Well I'm not in favor of renaming the function in the public API because that would be even more breaking than limiting the return value to a bool. That suggestion was mainly for the private API. The name Personally, I think you had it right the first time with #125. But you did what most of us coders do, and you thought (or over-thought) about it too much. |
Guilty as charged. I was looking at the Paho source code when you replied. I'll reopen the pull request with 'is_connected Thanks! |
Remember that you can use the |
@brentru Could you weigh in on this? |
@dhalbert @BiffoBear I agree with @2bndy5 - this library's function calls should be similar to Paho MQTT Python's API. Let's go with #125 |
I would expect is_connected to return True or False, rather than raise an exception in the False case:
To recreate:
The text was updated successfully, but these errors were encountered: