mirror of
https://github.com/elisspace/core.git
synced 2026-09-29 05:49:53 +00:00
Improve MQTT debug info for subscriptions with wildcard (#33752)
This commit is contained in:
@@ -21,8 +21,10 @@ def log_messages(hass: HomeAssistantType, entity_id: str) -> MessageCallbackType
|
||||
def _log_message(msg):
|
||||
"""Log message."""
|
||||
debug_info = hass.data[DATA_MQTT_DEBUG_INFO]
|
||||
messages = debug_info["entities"][entity_id]["topics"][msg.subscribed_topic]
|
||||
messages.append(msg.payload)
|
||||
messages = debug_info["entities"][entity_id]["subscriptions"][
|
||||
msg.subscribed_topic
|
||||
]
|
||||
messages.append((msg.payload, msg.topic))
|
||||
|
||||
def _decorator(msg_callback: MessageCallbackType):
|
||||
@wraps(msg_callback)
|
||||
@@ -37,24 +39,26 @@ def log_messages(hass: HomeAssistantType, entity_id: str) -> MessageCallbackType
|
||||
return _decorator
|
||||
|
||||
|
||||
def add_topic(hass, message_callback, topic):
|
||||
"""Prepare debug data for topic."""
|
||||
def add_subscription(hass, message_callback, subscription):
|
||||
"""Prepare debug data for subscription."""
|
||||
entity_id = getattr(message_callback, "__entity_id", None)
|
||||
if entity_id:
|
||||
debug_info = hass.data.setdefault(
|
||||
DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}}
|
||||
)
|
||||
entity_info = debug_info["entities"].setdefault(
|
||||
entity_id, {"topics": {}, "discovery_data": {}}
|
||||
entity_id, {"subscriptions": {}, "discovery_data": {}}
|
||||
)
|
||||
entity_info["topics"][topic] = deque([], STORED_MESSAGES)
|
||||
entity_info["subscriptions"][subscription] = deque([], STORED_MESSAGES)
|
||||
|
||||
|
||||
def remove_topic(hass, message_callback, topic):
|
||||
"""Remove debug data for topic."""
|
||||
def remove_subscription(hass, message_callback, subscription):
|
||||
"""Remove debug data for subscription."""
|
||||
entity_id = getattr(message_callback, "__entity_id", None)
|
||||
if entity_id and entity_id in hass.data[DATA_MQTT_DEBUG_INFO]["entities"]:
|
||||
hass.data[DATA_MQTT_DEBUG_INFO]["entities"][entity_id]["topics"].pop(topic)
|
||||
hass.data[DATA_MQTT_DEBUG_INFO]["entities"][entity_id]["subscriptions"].pop(
|
||||
subscription
|
||||
)
|
||||
|
||||
|
||||
def add_entity_discovery_data(hass, discovery_data, entity_id):
|
||||
@@ -63,7 +67,7 @@ def add_entity_discovery_data(hass, discovery_data, entity_id):
|
||||
DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}}
|
||||
)
|
||||
entity_info = debug_info["entities"].setdefault(
|
||||
entity_id, {"topics": {}, "discovery_data": {}}
|
||||
entity_id, {"subscriptions": {}, "discovery_data": {}}
|
||||
)
|
||||
entity_info["discovery_data"] = discovery_data
|
||||
|
||||
@@ -117,9 +121,14 @@ async def info_for_device(hass, device_id):
|
||||
continue
|
||||
|
||||
entity_info = mqtt_debug_info["entities"][entry.entity_id]
|
||||
topics = [
|
||||
{"topic": topic, "messages": list(messages)}
|
||||
for topic, messages in entity_info["topics"].items()
|
||||
subscriptions = [
|
||||
{
|
||||
"topic": topic,
|
||||
"messages": [
|
||||
{"payload": msg[0], "topic": msg[1]} for msg in list(messages)
|
||||
],
|
||||
}
|
||||
for topic, messages in entity_info["subscriptions"].items()
|
||||
]
|
||||
discovery_data = {
|
||||
"topic": entity_info["discovery_data"].get(ATTR_DISCOVERY_TOPIC, ""),
|
||||
@@ -128,7 +137,7 @@ async def info_for_device(hass, device_id):
|
||||
mqtt_info["entities"].append(
|
||||
{
|
||||
"entity_id": entry.entity_id,
|
||||
"topics": topics,
|
||||
"subscriptions": subscriptions,
|
||||
"discovery_data": discovery_data,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -34,14 +34,16 @@ class EntitySubscription:
|
||||
if other is not None and other.unsubscribe_callback is not None:
|
||||
other.unsubscribe_callback()
|
||||
# Clear debug data if it exists
|
||||
debug_info.remove_topic(self.hass, other.message_callback, other.topic)
|
||||
debug_info.remove_subscription(
|
||||
self.hass, other.message_callback, other.topic
|
||||
)
|
||||
|
||||
if self.topic is None:
|
||||
# We were asked to remove the subscription or not to create it
|
||||
return
|
||||
|
||||
# Prepare debug data
|
||||
debug_info.add_topic(self.hass, self.message_callback, self.topic)
|
||||
debug_info.add_subscription(self.hass, self.message_callback, self.topic)
|
||||
|
||||
self.unsubscribe_callback = await mqtt.async_subscribe(
|
||||
hass, self.topic, self.message_callback, self.qos, self.encoding
|
||||
@@ -96,7 +98,9 @@ async def async_subscribe_topics(
|
||||
if remaining.unsubscribe_callback is not None:
|
||||
remaining.unsubscribe_callback()
|
||||
# Clear debug data if it exists
|
||||
debug_info.remove_topic(hass, remaining.message_callback, remaining.topic)
|
||||
debug_info.remove_subscription(
|
||||
hass, remaining.message_callback, remaining.topic
|
||||
)
|
||||
|
||||
return new_state
|
||||
|
||||
|
||||
Reference in New Issue
Block a user