Inline MQTT paho imports (#29177)

* Inline MQTT paho imports

* Address comments

* Fix patch paths

* Move other imports inline

* Fix test
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-12-02 11:32:02 -08:00
1 parent 1804c6edc5
commit ffaa0e572a
4 files changed
+29 -17

No files matched your search

+17 -2
View File
@@ -16,8 +16,6 @@ from typing import Any, Callable, List, Optional, Union
import attr
import requests.certs
import voluptuous as vol
import paho.mqtt.client as mqtt
from paho.mqtt.matcher import MQTTMatcher
from homeassistant import config_entries
from homeassistant.components import websocket_api
@@ -725,6 +723,11 @@ class MQTT:
tls_version: Optional[int],
) -> None:
"""Initialize Home Assistant MQTT client."""
# We don't import them on the top because some integrations
# should be able to optionally rely on MQTT.
# pylint: disable=import-outside-toplevel
import paho.mqtt.client as mqtt
self.hass = hass
self.broker = broker
self.port = port
@@ -786,6 +789,9 @@ class MQTT:
This method is a coroutine.
"""
# pylint: disable=import-outside-toplevel
import paho.mqtt.client as mqtt
result: int = None
try:
result = await self.hass.async_add_job(
@@ -877,6 +883,9 @@ class MQTT:
Resubscribe to all topics we were subscribed to and publish birth
message.
"""
# pylint: disable=import-outside-toplevel
import paho.mqtt.client as mqtt
if result_code != mqtt.CONNACK_ACCEPTED:
_LOGGER.error(
"Unable to connect to the MQTT broker: %s",
@@ -968,6 +977,9 @@ class MQTT:
def _raise_on_error(result_code: int) -> None:
"""Raise error if error result."""
# pylint: disable=import-outside-toplevel
import paho.mqtt.client as mqtt
if result_code != 0:
raise HomeAssistantError(
"Error talking to MQTT: {}".format(mqtt.error_string(result_code))
@@ -976,6 +988,9 @@ def _raise_on_error(result_code: int) -> None:
def _match_topic(subscription: str, topic: str) -> bool:
"""Test if topic matches subscription."""
# pylint: disable=import-outside-toplevel
from paho.mqtt.matcher import MQTTMatcher
matcher = MQTTMatcher()
matcher[subscription] = True
try: