1
0
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:
Jan Bouwhuis
2023-02-03 19:27:46 +01:00
committed by GitHub
parent 71200baa8f
commit 3edfd10f2d
2 changed files with 65 additions and 26 deletions

View File

@@ -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)