Skip to content

Let PAHO MQTT client handle connection to MQTT server#35983

Merged
emontnemery merged 3 commits into
home-assistant:devfrom
emontnemery:fix_mqtt_startup
May 26, 2020
Merged

Let PAHO MQTT client handle connection to MQTT server#35983
emontnemery merged 3 commits into
home-assistant:devfrom
emontnemery:fix_mqtt_startup

Conversation

@emontnemery
Copy link
Copy Markdown
Contributor

@emontnemery emontnemery commented May 22, 2020

Proposed change

Let PAHO MQTT client handle connection to MQTT server by itself, instead of doing checks when loading the config entry.
The checks prevents the config entry from loading until connection with the MQTT server is established, which breaks MQTT components as reported in #35858 as well as in other issues.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

@probot-home-assistant
Copy link
Copy Markdown

Hey there @home-assistant/core, mind taking a look at this pull request as its been labeled with a integration (mqtt) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)

@MartinHjelmare
Copy link
Copy Markdown
Member

Why doesn't the current retry logic work and a restart is required?

@emontnemery
Copy link
Copy Markdown
Contributor Author

emontnemery commented May 22, 2020

@MartinHjelmare The retry logic has already been removed, done in #34556
Edit: Or you mean why doesn't the retry logic for loading the config entry work?

@MartinHjelmare
Copy link
Copy Markdown
Member

Yeah, the latter.

@emontnemery
Copy link
Copy Markdown
Contributor Author

emontnemery commented May 22, 2020

When the config entry is not loaded, calls to subscribe don't work so the subscriptions are not done once the connection to the MQTT server is established.
This breaks MQTT components, automations, etc. that setup subscriptions before the connection is established.

Since we don't unload the MQTT config entry if the connection is severed, I think it's OK to not block it from loading either?

@MartinHjelmare
Copy link
Copy Markdown
Member

Why doesn't the subscription store work when the entry isn't setup?

@emontnemery
Copy link
Copy Markdown
Contributor Author

emontnemery commented May 22, 2020

The subscription store is reset every time loading the MQTT entry is retried because async_setup_entry will create a new MQTT instance:

hass.data[DATA_MQTT] = MQTT(

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just a small comment.

Comment thread homeassistant/components/mqtt/__init__.py Outdated
Comment thread homeassistant/components/mqtt/__init__.py Outdated
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@frenck
Copy link
Copy Markdown
Member

frenck commented May 23, 2020

/AzurePipelines run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

with patch("paho.mqtt.client.Client") as mock_client:
mock_client().connect = lambda *args: 1
assert not await mqtt.async_setup_entry(hass, entry)
assert await mqtt.async_setup_entry(hass, entry)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use the core interface, hass.config_entries.async_setup.

@emontnemery emontnemery added this to the 0.110.4 milestone May 26, 2020
@emontnemery emontnemery merged commit 8de863e into home-assistant:dev May 26, 2020
balloob pushed a commit that referenced this pull request May 28, 2020
* Let PAHO client handle connection to MQTT server
@balloob balloob mentioned this pull request May 28, 2020
@emontnemery emontnemery deleted the fix_mqtt_startup branch June 10, 2020 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MQTT devices fail to update if MQTT broker isn't ready on boot

5 participants