Move imports in mqtt component (#27835)
* move imports to top-level in mqtt server * move imports to top-level in mqtt configflow * move imports to top-level in mqtt init * move imports to top-level in mqtt vacuum * move imports to top-level in mqtt light
This commit is contained in:
1 parent
7637ceb880
commit
1a5b4c105a
19 files changed
+130
-144
No files matched your search
@@ -1,5 +1,6 @@
|
||||
"""Support for MQTT message handling."""
|
||||
import asyncio
|
||||
import sys
|
||||
from functools import partial, wraps
|
||||
import inspect
|
||||
from itertools import groupby
|
||||
@@ -15,6 +16,8 @@ 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
|
||||
@@ -36,6 +39,7 @@ from homeassistant.exceptions import (
|
||||
ConfigEntryNotReady,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv, template
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType, ServiceDataType
|
||||
from homeassistant.loader import bind_hass
|
||||
@@ -50,7 +54,12 @@ from .const import (
|
||||
DEFAULT_DISCOVERY,
|
||||
CONF_STATE_TOPIC,
|
||||
ATTR_DISCOVERY_HASH,
|
||||
PROTOCOL_311,
|
||||
DEFAULT_QOS,
|
||||
)
|
||||
from .discovery import MQTT_DISCOVERY_UPDATED, clear_discovery_hash
|
||||
from .models import PublishPayloadType, Message, MessageCallbackType
|
||||
from .subscription import async_subscribe_topics, async_unsubscribe_topics
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -95,11 +104,9 @@ CONF_VIA_DEVICE = "via_device"
|
||||
CONF_DEPRECATED_VIA_HUB = "via_hub"
|
||||
|
||||
PROTOCOL_31 = "3.1"
|
||||
PROTOCOL_311 = "3.1.1"
|
||||
|
||||
DEFAULT_PORT = 1883
|
||||
DEFAULT_KEEPALIVE = 60
|
||||
DEFAULT_QOS = 0
|
||||
DEFAULT_RETAIN = False
|
||||
DEFAULT_PROTOCOL = PROTOCOL_311
|
||||
DEFAULT_DISCOVERY_PREFIX = "homeassistant"
|
||||
@@ -329,23 +336,9 @@ MQTT_PUBLISH_SCHEMA = vol.Schema(
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
PublishPayloadType = Union[str, bytes, int, float, None]
|
||||
SubscribePayloadType = Union[str, bytes] # Only bytes if encoding is None
|
||||
|
||||
|
||||
@attr.s(slots=True, frozen=True)
|
||||
class Message:
|
||||
"""MQTT Message."""
|
||||
|
||||
topic = attr.ib(type=str)
|
||||
payload = attr.ib(type=PublishPayloadType)
|
||||
qos = attr.ib(type=int)
|
||||
retain = attr.ib(type=bool)
|
||||
|
||||
|
||||
MessageCallbackType = Callable[[Message], None]
|
||||
|
||||
|
||||
def _build_publish_data(topic: Any, qos: int, retain: bool) -> ServiceDataType:
|
||||
"""Build the arguments for the publish service without the payload."""
|
||||
data = {ATTR_TOPIC: topic}
|
||||
@@ -629,8 +622,6 @@ async def async_setup_entry(hass, entry):
|
||||
elif conf_tls_version == "1.0":
|
||||
tls_version = ssl.PROTOCOL_TLSv1
|
||||
else:
|
||||
import sys
|
||||
|
||||
# Python3.6 supports automatic negotiation of highest TLS version
|
||||
if sys.hexversion >= 0x03060000:
|
||||
tls_version = ssl.PROTOCOL_TLS # pylint: disable=no-member
|
||||
@@ -735,8 +726,6 @@ class MQTT:
|
||||
tls_version: Optional[int],
|
||||
) -> None:
|
||||
"""Initialize Home Assistant MQTT client."""
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
self.hass = hass
|
||||
self.broker = broker
|
||||
self.port = port
|
||||
@@ -808,8 +797,6 @@ class MQTT:
|
||||
return CONNECTION_FAILED_RECOVERABLE
|
||||
|
||||
if result != 0:
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
_LOGGER.error("Failed to connect: %s", mqtt.error_string(result))
|
||||
return CONNECTION_FAILED
|
||||
|
||||
@@ -891,8 +878,6 @@ class MQTT:
|
||||
Resubscribe to all topics we were subscribed to and publish birth
|
||||
message.
|
||||
"""
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
if result_code != mqtt.CONNACK_ACCEPTED:
|
||||
_LOGGER.error(
|
||||
"Unable to connect to the MQTT broker: %s",
|
||||
@@ -984,8 +969,6 @@ class MQTT:
|
||||
def _raise_on_error(result_code: int) -> None:
|
||||
"""Raise error if error result."""
|
||||
if result_code != 0:
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
raise HomeAssistantError(
|
||||
"Error talking to MQTT: {}".format(mqtt.error_string(result_code))
|
||||
)
|
||||
@@ -993,8 +976,6 @@ def _raise_on_error(result_code: int) -> None:
|
||||
|
||||
def _match_topic(subscription: str, topic: str) -> bool:
|
||||
"""Test if topic matches subscription."""
|
||||
from paho.mqtt.matcher import MQTTMatcher
|
||||
|
||||
matcher = MQTTMatcher()
|
||||
matcher[subscription] = True
|
||||
try:
|
||||
@@ -1028,8 +1009,6 @@ class MqttAttributes(Entity):
|
||||
|
||||
async def _attributes_subscribe_topics(self):
|
||||
"""(Re)Subscribe to topics."""
|
||||
from .subscription import async_subscribe_topics
|
||||
|
||||
attr_tpl = self._attributes_config.get(CONF_JSON_ATTRS_TEMPLATE)
|
||||
if attr_tpl is not None:
|
||||
attr_tpl.hass = self.hass
|
||||
@@ -1065,8 +1044,6 @@ class MqttAttributes(Entity):
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Unsubscribe when removed."""
|
||||
from .subscription import async_unsubscribe_topics
|
||||
|
||||
self._attributes_sub_state = await async_unsubscribe_topics(
|
||||
self.hass, self._attributes_sub_state
|
||||
)
|
||||
@@ -1102,7 +1079,6 @@ class MqttAvailability(Entity):
|
||||
|
||||
async def _availability_subscribe_topics(self):
|
||||
"""(Re)Subscribe to topics."""
|
||||
from .subscription import async_subscribe_topics
|
||||
|
||||
@callback
|
||||
def availability_message_received(msg: Message) -> None:
|
||||
@@ -1128,8 +1104,6 @@ class MqttAvailability(Entity):
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Unsubscribe when removed."""
|
||||
from .subscription import async_unsubscribe_topics
|
||||
|
||||
self._availability_sub_state = await async_unsubscribe_topics(
|
||||
self.hass, self._availability_sub_state
|
||||
)
|
||||
@@ -1154,9 +1128,6 @@ class MqttDiscoveryUpdate(Entity):
|
||||
"""Subscribe to discovery updates."""
|
||||
await super().async_added_to_hass()
|
||||
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from .discovery import MQTT_DISCOVERY_UPDATED, clear_discovery_hash
|
||||
|
||||
@callback
|
||||
def discovery_callback(payload):
|
||||
"""Handle discovery update."""
|
||||
|
||||
Reference in new issue
Block a user