From e5523ef6b6102923f85773e1b1542d5d0139be33 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 10 Mar 2022 10:32:49 +0100 Subject: [PATCH] Correct local import of paho-mqtt (#67944) * Correct local import of paho-mqtt * Remove MqttClientSetup.mqtt class attribute * Remove reference to MqttClientSetup.mqtt --- homeassistant/components/mqtt/__init__.py | 23 ++++++++++++-------- homeassistant/components/mqtt/config_flow.py | 6 ++++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 8ef62ae8bcd..96074acab37 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -13,7 +13,7 @@ import logging from operator import attrgetter import ssl import time -from typing import Any, Union, cast +from typing import TYPE_CHECKING, Any, Union, cast import uuid import attr @@ -108,6 +108,11 @@ from .models import ( ) from .util import _VALID_QOS_SCHEMA, valid_publish_topic, valid_subscribe_topic +if TYPE_CHECKING: + # Only import for paho-mqtt type checking here, imports are done locally + # because integrations should be able to optionally rely on MQTT. + import paho.mqtt.client as mqtt # pylint: disable=import-outside-toplevel + _LOGGER = logging.getLogger(__name__) _SENTINEL = object() @@ -754,23 +759,23 @@ class Subscription: class MqttClientSetup: """Helper class to setup the paho mqtt client from config.""" - # We don't import on the top because some integrations - # should be able to optionally rely on MQTT. - import paho.mqtt.client as mqtt # pylint: disable=import-outside-toplevel - def __init__(self, config: ConfigType) -> None: """Initialize the MQTT client setup helper.""" + # We don't import on the top because some integrations + # should be able to optionally rely on MQTT. + import paho.mqtt.client as mqtt # pylint: disable=import-outside-toplevel + if config[CONF_PROTOCOL] == PROTOCOL_31: - proto = self.mqtt.MQTTv31 + proto = mqtt.MQTTv31 else: - proto = self.mqtt.MQTTv311 + proto = mqtt.MQTTv311 if (client_id := config.get(CONF_CLIENT_ID)) is None: # PAHO MQTT relies on the MQTT server to generate random client IDs. # However, that feature is not mandatory so we generate our own. - client_id = self.mqtt.base62(uuid.uuid4().int, padding=22) - self._client = self.mqtt.Client(client_id, protocol=proto) + client_id = mqtt.base62(uuid.uuid4().int, padding=22) + self._client = mqtt.Client(client_id, protocol=proto) # Enable logging self._client.enable_logger() diff --git a/homeassistant/components/mqtt/config_flow.py b/homeassistant/components/mqtt/config_flow.py index 99e7e9718d0..6697b17dfdc 100644 --- a/homeassistant/components/mqtt/config_flow.py +++ b/homeassistant/components/mqtt/config_flow.py @@ -319,6 +319,10 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow): def try_connection(hass, broker, port, username, password, protocol="3.1"): """Test if we can connect to an MQTT broker.""" + # We don't import on the top because some integrations + # should be able to optionally rely on MQTT. + import paho.mqtt.client as mqtt # pylint: disable=import-outside-toplevel + # Get the config from configuration.yaml yaml_config = hass.data.get(DATA_MQTT_CONFIG, {}) entry_config = { @@ -334,7 +338,7 @@ def try_connection(hass, broker, port, username, password, protocol="3.1"): def on_connect(client_, userdata, flags, result_code): """Handle connection result.""" - result.put(result_code == MqttClientSetup.mqtt.CONNACK_ACCEPTED) + result.put(result_code == mqtt.CONNACK_ACCEPTED) client.on_connect = on_connect