Skip to content

Import mqtt in the import executor when @mqtt_trigger starts - #930

Merged
craigbarratt merged 1 commit into
custom-components:masterfrom
dmamelin:fix-blocking-mqtt-import
Nov 26, 2026
Merged

craigbarratt merged 1 commit into
custom-components:masterfrom
dmamelin:fix-blocking-mqtt-import

Conversation

@dmamelin

Copy link
Copy Markdown
Contributor

Small follow-up to #927 that fixes a regression: a blocking-call warning at startup when MQTT isn't set up.

Detected blocking call to open ... inside the event loop by integration 'mqtt' at homeassistant/components/mqtt/models.py, line 12: from paho.mqtt.client import MQTTMessage

Before #927, the legacy mqtt.py imported homeassistant.components.mqtt (and paho) while Home Assistant was loading pyscript in its import executor. Now only decorators/mqtt.py imports it, and the decorators are loaded by DecoratorRegistry.init() from async_setup_entry(), which runs in the event loop.

@mqtt_trigger now imports mqtt with async_import_module() when it starts, so it's loaded in the import executor, and only when the decorator is used. When MQTT is set up, Home Assistant has already loaded it (after_dependencies), so this just returns the loaded module.

Since the legacy mqtt.py was removed, homeassistant.components.mqtt (and
paho) was imported only by decorators/mqtt.py, which DecoratorRegistry.init()
loads in the event loop, so Home Assistant logged a blocking call when MQTT
isn't set up. MQTTTriggerDecorator.start() now imports it with
async_import_module(), which runs the import in the import executor.

@craigbarratt craigbarratt left a comment

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.

Thanks @dmamelin, good catch. I checked that loading pyscript and its decorators no longer imports homeassistant.components.mqtt or paho (on master it imports both), and that the existing @mqtt_trigger test goes through the new import in start().

@craigbarratt
craigbarratt merged commit d56368d into custom-components:master Nov 26, 2026
6 checks passed
@craigbarratt craigbarratt mentioned this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants