Remove legacy mqtt debug_info implementation (#118212)

This commit is contained in:
J. Nick Koston 2024-05-26 21:55:54 -10:00 committed by GitHub
parent cfc2cadb77
commit 3680d1f8c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 32 deletions

View file

@ -14,7 +14,7 @@ from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from .const import ATTR_DISCOVERY_PAYLOAD, ATTR_DISCOVERY_TOPIC from .const import ATTR_DISCOVERY_PAYLOAD, ATTR_DISCOVERY_TOPIC
from .models import DATA_MQTT, MessageCallbackType, PublishPayloadType from .models import DATA_MQTT, PublishPayloadType
STORED_MESSAGES = 10 STORED_MESSAGES = 10
@ -53,41 +53,33 @@ def log_message(
def add_subscription( def add_subscription(
hass: HomeAssistant, hass: HomeAssistant, subscription: str, entity_id: str | None
message_callback: MessageCallbackType,
subscription: str,
entity_id: str | None = None,
) -> None: ) -> None:
"""Prepare debug data for subscription.""" """Prepare debug data for subscription."""
if not entity_id:
entity_id = getattr(message_callback, "__entity_id", None)
if entity_id: if entity_id:
entity_info = hass.data[DATA_MQTT].debug_info_entities.setdefault( entity_info = hass.data[DATA_MQTT].debug_info_entities.setdefault(
entity_id, {"subscriptions": {}, "discovery_data": {}, "transmitted": {}} entity_id, {"subscriptions": {}, "discovery_data": {}, "transmitted": {}}
) )
if subscription not in entity_info["subscriptions"]: if subscription not in entity_info["subscriptions"]:
entity_info["subscriptions"][subscription] = { entity_info["subscriptions"][subscription] = {
"count": 0, "count": 1,
"messages": deque([], STORED_MESSAGES), "messages": deque([], STORED_MESSAGES),
} }
entity_info["subscriptions"][subscription]["count"] += 1 else:
entity_info["subscriptions"][subscription]["count"] += 1
def remove_subscription( def remove_subscription(
hass: HomeAssistant, hass: HomeAssistant, subscription: str, entity_id: str | None
message_callback: MessageCallbackType,
subscription: str,
entity_id: str | None = None,
) -> None: ) -> None:
"""Remove debug data for subscription if it exists.""" """Remove debug data for subscription if it exists."""
if not entity_id:
entity_id = getattr(message_callback, "__entity_id", None)
if entity_id and entity_id in ( if entity_id and entity_id in (
debug_info_entities := hass.data[DATA_MQTT].debug_info_entities debug_info_entities := hass.data[DATA_MQTT].debug_info_entities
): ):
debug_info_entities[entity_id]["subscriptions"][subscription]["count"] -= 1 subscriptions = debug_info_entities[entity_id]["subscriptions"]
if not debug_info_entities[entity_id]["subscriptions"][subscription]["count"]: subscriptions[subscription]["count"] -= 1
debug_info_entities[entity_id]["subscriptions"].pop(subscription) if not subscriptions[subscription]["count"]:
subscriptions.pop(subscription)
def add_entity_discovery_data( def add_entity_discovery_data(

View file

@ -15,7 +15,7 @@ from .const import DEFAULT_QOS
from .models import MessageCallbackType from .models import MessageCallbackType
@dataclass(slots=True) @dataclass(slots=True, kw_only=True)
class EntitySubscription: class EntitySubscription:
"""Class to hold data about an active entity topic subscription.""" """Class to hold data about an active entity topic subscription."""
@ -26,8 +26,8 @@ class EntitySubscription:
unsubscribe_callback: Callable[[], None] | None unsubscribe_callback: Callable[[], None] | None
qos: int = 0 qos: int = 0
encoding: str = "utf-8" encoding: str = "utf-8"
entity_id: str | None = None entity_id: str | None
job_type: HassJobType | None = None job_type: HassJobType | None
def resubscribe_if_necessary( def resubscribe_if_necessary(
self, hass: HomeAssistant, other: EntitySubscription | None self, hass: HomeAssistant, other: EntitySubscription | None
@ -42,18 +42,14 @@ class EntitySubscription:
if other is not None and other.unsubscribe_callback is not None: if other is not None and other.unsubscribe_callback is not None:
other.unsubscribe_callback() other.unsubscribe_callback()
# Clear debug data if it exists # Clear debug data if it exists
debug_info.remove_subscription( debug_info.remove_subscription(self.hass, str(other.topic), other.entity_id)
self.hass, other.message_callback, str(other.topic), other.entity_id
)
if self.topic is None: if self.topic is None:
# We were asked to remove the subscription or not to create it # We were asked to remove the subscription or not to create it
return return
# Prepare debug data # Prepare debug data
debug_info.add_subscription( debug_info.add_subscription(self.hass, self.topic, self.entity_id)
self.hass, self.message_callback, self.topic, self.entity_id
)
self.should_subscribe = True self.should_subscribe = True
@ -110,15 +106,15 @@ def async_prepare_subscribe_topics(
for key, value in topics.items(): for key, value in topics.items():
# Extract the new requested subscription # Extract the new requested subscription
requested = EntitySubscription( requested = EntitySubscription(
topic=value.get("topic", None), topic=value.get("topic"),
message_callback=value.get("msg_callback", None), message_callback=value["msg_callback"],
unsubscribe_callback=None, unsubscribe_callback=None,
qos=value.get("qos", DEFAULT_QOS), qos=value.get("qos", DEFAULT_QOS),
encoding=value.get("encoding", "utf-8"), encoding=value.get("encoding", "utf-8"),
hass=hass, hass=hass,
should_subscribe=None, should_subscribe=None,
entity_id=value.get("entity_id", None), entity_id=value.get("entity_id"),
job_type=value.get("job_type", None), job_type=value.get("job_type"),
) )
# Get the current subscription state # Get the current subscription state
current = current_subscriptions.pop(key, None) current = current_subscriptions.pop(key, None)
@ -132,7 +128,6 @@ def async_prepare_subscribe_topics(
# Clear debug data if it exists # Clear debug data if it exists
debug_info.remove_subscription( debug_info.remove_subscription(
hass, hass,
remaining.message_callback,
str(remaining.topic), str(remaining.topic),
remaining.entity_id, remaining.entity_id,
) )