Simplify imports in mqtt (#125749)

This commit is contained in:
epenet 2024-09-11 18:14:00 +02:00 committed by GitHub
parent e4347e5520
commit ba9dae10c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 9 deletions

View File

@ -26,8 +26,8 @@ from homeassistant.loader import async_get_mqtt
from homeassistant.util.json import json_loads_object from homeassistant.util.json import json_loads_object
from homeassistant.util.signal_type import SignalTypeFormat from homeassistant.util.signal_type import SignalTypeFormat
from .. import mqtt
from .abbreviations import ABBREVIATIONS, DEVICE_ABBREVIATIONS, ORIGIN_ABBREVIATIONS from .abbreviations import ABBREVIATIONS, DEVICE_ABBREVIATIONS, ORIGIN_ABBREVIATIONS
from .client import async_subscribe_internal
from .const import ( from .const import (
ATTR_DISCOVERY_HASH, ATTR_DISCOVERY_HASH,
ATTR_DISCOVERY_PAYLOAD, ATTR_DISCOVERY_PAYLOAD,
@ -341,7 +341,7 @@ async def async_start( # noqa: C901
) )
mqtt_data.discovery_unsubscribe = [ mqtt_data.discovery_unsubscribe = [
mqtt.async_subscribe_internal( async_subscribe_internal(
hass, hass,
topic, topic,
async_discovery_message_received, async_discovery_message_received,
@ -400,7 +400,7 @@ async def async_start( # noqa: C901
integration_unsubscribe.update( integration_unsubscribe.update(
{ {
f"{integration}_{topic}": mqtt.async_subscribe_internal( f"{integration}_{topic}": async_subscribe_internal(
hass, hass,
topic, topic,
functools.partial(async_integration_message_received, integration), functools.partial(async_integration_message_received, integration),

View File

@ -24,8 +24,15 @@ from homeassistant.helpers.trigger import TriggerActionType, TriggerData, Trigge
from homeassistant.helpers.typing import ConfigType, TemplateVarsType from homeassistant.helpers.typing import ConfigType, TemplateVarsType
from homeassistant.util.json import json_loads from homeassistant.util.json import json_loads
from .. import mqtt from .client import async_subscribe_internal
from .const import CONF_ENCODING, CONF_QOS, CONF_TOPIC, DEFAULT_ENCODING, DEFAULT_QOS from .const import (
CONF_ENCODING,
CONF_QOS,
CONF_TOPIC,
DEFAULT_ENCODING,
DEFAULT_QOS,
DOMAIN,
)
from .models import ( from .models import (
MqttCommandTemplate, MqttCommandTemplate,
MqttValueTemplate, MqttValueTemplate,
@ -33,11 +40,12 @@ from .models import (
PublishPayloadType, PublishPayloadType,
ReceiveMessage, ReceiveMessage,
) )
from .util import valid_subscribe_topic, valid_subscribe_topic_template
TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
{ {
vol.Required(CONF_PLATFORM): mqtt.DOMAIN, vol.Required(CONF_PLATFORM): DOMAIN,
vol.Required(CONF_TOPIC): mqtt.util.valid_subscribe_topic_template, vol.Required(CONF_TOPIC): valid_subscribe_topic_template,
vol.Optional(CONF_PAYLOAD): cv.template, vol.Optional(CONF_PAYLOAD): cv.template,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template, vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): cv.string, vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): cv.string,
@ -76,7 +84,7 @@ async def async_attach_trigger(
topic_template: Template = config[CONF_TOPIC] topic_template: Template = config[CONF_TOPIC]
topic = topic_template.async_render(variables, limited=True, parse_result=False) topic = topic_template.async_render(variables, limited=True, parse_result=False)
mqtt.util.valid_subscribe_topic(topic) valid_subscribe_topic(topic)
@callback @callback
def mqtt_automation_listener(mqttmsg: ReceiveMessage) -> None: def mqtt_automation_listener(mqttmsg: ReceiveMessage) -> None:
@ -104,7 +112,7 @@ async def async_attach_trigger(
"Attaching MQTT trigger for topic: '%s', payload: '%s'", topic, wanted_payload "Attaching MQTT trigger for topic: '%s', payload: '%s'", topic, wanted_payload
) )
return mqtt.async_subscribe_internal( return async_subscribe_internal(
hass, hass,
topic, topic,
mqtt_automation_listener, mqtt_automation_listener,