mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Add type hints for MQTT common helper and fixtures (#87065)
This commit is contained in:
@@ -379,16 +379,34 @@ def async_mock_intent(hass, intent_typ):
|
||||
|
||||
|
||||
@callback
|
||||
def async_fire_mqtt_message(hass, topic, payload, qos=0, retain=False):
|
||||
def async_fire_mqtt_message(
|
||||
hass: HomeAssistant,
|
||||
topic: str,
|
||||
payload: bytes | str,
|
||||
qos: int = 0,
|
||||
retain: bool = False,
|
||||
) -> None:
|
||||
"""Fire the MQTT message."""
|
||||
# Local import to avoid processing MQTT modules when running a testcase
|
||||
# which does not use MQTT.
|
||||
from homeassistant.components.mqtt.models import ReceiveMessage
|
||||
|
||||
# pylint: disable-next=import-outside-toplevel
|
||||
from paho.mqtt.client import MQTTMessage
|
||||
|
||||
# pylint: disable-next=import-outside-toplevel
|
||||
from homeassistant.components.mqtt.models import MqttData
|
||||
|
||||
if isinstance(payload, str):
|
||||
payload = payload.encode("utf-8")
|
||||
msg = ReceiveMessage(topic, payload, qos, retain)
|
||||
hass.data["mqtt"].client._mqtt_handle_message(msg)
|
||||
|
||||
msg = MQTTMessage(topic=topic.encode("utf-8"))
|
||||
msg.payload = payload
|
||||
msg.qos = qos
|
||||
msg.retain = retain
|
||||
|
||||
mqtt_data: MqttData = hass.data["mqtt"]
|
||||
assert mqtt_data.client
|
||||
mqtt_data.client._mqtt_handle_message(msg)
|
||||
|
||||
|
||||
fire_mqtt_message = threadsafe_callback_factory(async_fire_mqtt_message)
|
||||
|
||||
Reference in New Issue
Block a user