mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix cleanup of MQTT debug info (#66104)
This commit is contained in:
parent
07b3d23835
commit
55d8314093
@ -596,6 +596,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
debug_info.initialize(hass)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,6 +20,11 @@ DATA_MQTT_DEBUG_INFO = "mqtt_debug_info"
|
|||||||
STORED_MESSAGES = 10
|
STORED_MESSAGES = 10
|
||||||
|
|
||||||
|
|
||||||
|
def initialize(hass: HomeAssistant):
|
||||||
|
"""Initialize MQTT debug info."""
|
||||||
|
hass.data[DATA_MQTT_DEBUG_INFO] = {"entities": {}, "triggers": {}}
|
||||||
|
|
||||||
|
|
||||||
def log_messages(
|
def log_messages(
|
||||||
hass: HomeAssistant, entity_id: str
|
hass: HomeAssistant, entity_id: str
|
||||||
) -> Callable[[MessageCallbackType], MessageCallbackType]:
|
) -> Callable[[MessageCallbackType], MessageCallbackType]:
|
||||||
@ -67,9 +72,7 @@ def log_message(
|
|||||||
retain: bool,
|
retain: bool,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Log an outgoing MQTT message."""
|
"""Log an outgoing MQTT message."""
|
||||||
debug_info = hass.data.setdefault(
|
debug_info = hass.data[DATA_MQTT_DEBUG_INFO]
|
||||||
DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}}
|
|
||||||
)
|
|
||||||
entity_info = debug_info["entities"].setdefault(
|
entity_info = debug_info["entities"].setdefault(
|
||||||
entity_id, {"subscriptions": {}, "discovery_data": {}, "transmitted": {}}
|
entity_id, {"subscriptions": {}, "discovery_data": {}, "transmitted": {}}
|
||||||
)
|
)
|
||||||
@ -86,9 +89,7 @@ def log_message(
|
|||||||
def add_subscription(hass, message_callback, subscription):
|
def add_subscription(hass, message_callback, subscription):
|
||||||
"""Prepare debug data for subscription."""
|
"""Prepare debug data for subscription."""
|
||||||
if entity_id := getattr(message_callback, "__entity_id", None):
|
if entity_id := getattr(message_callback, "__entity_id", None):
|
||||||
debug_info = hass.data.setdefault(
|
debug_info = hass.data[DATA_MQTT_DEBUG_INFO]
|
||||||
DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}}
|
|
||||||
)
|
|
||||||
entity_info = debug_info["entities"].setdefault(
|
entity_info = debug_info["entities"].setdefault(
|
||||||
entity_id, {"subscriptions": {}, "discovery_data": {}, "transmitted": {}}
|
entity_id, {"subscriptions": {}, "discovery_data": {}, "transmitted": {}}
|
||||||
)
|
)
|
||||||
@ -117,9 +118,7 @@ def remove_subscription(hass, message_callback, subscription):
|
|||||||
|
|
||||||
def add_entity_discovery_data(hass, discovery_data, entity_id):
|
def add_entity_discovery_data(hass, discovery_data, entity_id):
|
||||||
"""Add discovery data."""
|
"""Add discovery data."""
|
||||||
debug_info = hass.data.setdefault(
|
debug_info = hass.data[DATA_MQTT_DEBUG_INFO]
|
||||||
DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}}
|
|
||||||
)
|
|
||||||
entity_info = debug_info["entities"].setdefault(
|
entity_info = debug_info["entities"].setdefault(
|
||||||
entity_id, {"subscriptions": {}, "discovery_data": {}, "transmitted": {}}
|
entity_id, {"subscriptions": {}, "discovery_data": {}, "transmitted": {}}
|
||||||
)
|
)
|
||||||
@ -134,14 +133,13 @@ def update_entity_discovery_data(hass, discovery_payload, entity_id):
|
|||||||
|
|
||||||
def remove_entity_data(hass, entity_id):
|
def remove_entity_data(hass, entity_id):
|
||||||
"""Remove discovery data."""
|
"""Remove discovery data."""
|
||||||
|
if entity_id in hass.data[DATA_MQTT_DEBUG_INFO]["entities"]:
|
||||||
hass.data[DATA_MQTT_DEBUG_INFO]["entities"].pop(entity_id)
|
hass.data[DATA_MQTT_DEBUG_INFO]["entities"].pop(entity_id)
|
||||||
|
|
||||||
|
|
||||||
def add_trigger_discovery_data(hass, discovery_hash, discovery_data, device_id):
|
def add_trigger_discovery_data(hass, discovery_hash, discovery_data, device_id):
|
||||||
"""Add discovery data."""
|
"""Add discovery data."""
|
||||||
debug_info = hass.data.setdefault(
|
debug_info = hass.data[DATA_MQTT_DEBUG_INFO]
|
||||||
DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}}
|
|
||||||
)
|
|
||||||
debug_info["triggers"][discovery_hash] = {
|
debug_info["triggers"][discovery_hash] = {
|
||||||
"device_id": device_id,
|
"device_id": device_id,
|
||||||
"discovery_data": discovery_data,
|
"discovery_data": discovery_data,
|
||||||
@ -167,9 +165,7 @@ def info_for_device(hass, device_id):
|
|||||||
entries = hass.helpers.entity_registry.async_entries_for_device(
|
entries = hass.helpers.entity_registry.async_entries_for_device(
|
||||||
entity_registry, device_id, include_disabled_entities=True
|
entity_registry, device_id, include_disabled_entities=True
|
||||||
)
|
)
|
||||||
mqtt_debug_info = hass.data.setdefault(
|
mqtt_debug_info = hass.data[DATA_MQTT_DEBUG_INFO]
|
||||||
DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}}
|
|
||||||
)
|
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
if entry.entity_id not in mqtt_debug_info["entities"]:
|
if entry.entity_id not in mqtt_debug_info["entities"]:
|
||||||
continue
|
continue
|
||||||
|
@ -573,7 +573,6 @@ class MqttDiscoveryUpdate(Entity):
|
|||||||
def _cleanup_discovery_on_remove(self) -> None:
|
def _cleanup_discovery_on_remove(self) -> None:
|
||||||
"""Stop listening to signal and cleanup discovery data."""
|
"""Stop listening to signal and cleanup discovery data."""
|
||||||
if self._discovery_data and not self._removed_from_hass:
|
if self._discovery_data and not self._removed_from_hass:
|
||||||
debug_info.remove_entity_data(self.hass, self.entity_id)
|
|
||||||
clear_discovery_hash(self.hass, self._discovery_data[ATTR_DISCOVERY_HASH])
|
clear_discovery_hash(self.hass, self._discovery_data[ATTR_DISCOVERY_HASH])
|
||||||
self._removed_from_hass = True
|
self._removed_from_hass = True
|
||||||
|
|
||||||
@ -709,6 +708,7 @@ class MqttEntity(
|
|||||||
await MqttAttributes.async_will_remove_from_hass(self)
|
await MqttAttributes.async_will_remove_from_hass(self)
|
||||||
await MqttAvailability.async_will_remove_from_hass(self)
|
await MqttAvailability.async_will_remove_from_hass(self)
|
||||||
await MqttDiscoveryUpdate.async_will_remove_from_hass(self)
|
await MqttDiscoveryUpdate.async_will_remove_from_hass(self)
|
||||||
|
debug_info.remove_entity_data(self.hass, self.entity_id)
|
||||||
|
|
||||||
async def async_publish(
|
async def async_publish(
|
||||||
self,
|
self,
|
||||||
|
@ -1766,7 +1766,7 @@ async def test_debug_info_multiple_entities_triggers(hass, mqtt_mock):
|
|||||||
} in discovery_data
|
} in discovery_data
|
||||||
|
|
||||||
|
|
||||||
async def test_debug_info_non_mqtt(hass, device_reg, entity_reg):
|
async def test_debug_info_non_mqtt(hass, device_reg, entity_reg, mqtt_mock):
|
||||||
"""Test we get empty debug_info for a device with non MQTT entities."""
|
"""Test we get empty debug_info for a device with non MQTT entities."""
|
||||||
DOMAIN = "sensor"
|
DOMAIN = "sensor"
|
||||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user