From 69353d271944050790afd93e5ba9c16f279bd5bf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 28 May 2024 11:10:07 -1000 Subject: [PATCH] Speed up mqtt debug info on message callback (#118303) --- homeassistant/components/mqtt/models.py | 5 ++- tests/components/mqtt/test_init.py | 60 ------------------------- 2 files changed, 4 insertions(+), 61 deletions(-) diff --git a/homeassistant/components/mqtt/models.py b/homeassistant/components/mqtt/models.py index 83248d85135..f26ed196663 100644 --- a/homeassistant/components/mqtt/models.py +++ b/homeassistant/components/mqtt/models.py @@ -58,7 +58,10 @@ class PublishMessage: retain: bool -@dataclass(slots=True, frozen=True) +# eq=False so we use the id() of the object for comparison +# since client will only generate one instance of this object +# per messages/subscribed_topic. +@dataclass(slots=True, frozen=True, eq=False) class ReceiveMessage: """MQTT Message received.""" diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 13130329296..3e40594b230 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -3527,66 +3527,6 @@ async def test_debug_info_wildcard( } in debug_info_data["entities"][0]["subscriptions"] -async def test_debug_info_filter_same( - hass: HomeAssistant, - device_registry: dr.DeviceRegistry, - mqtt_mock_entry: MqttMockHAClientGenerator, - freezer: FrozenDateTimeFactory, -) -> None: - """Test debug info removes messages with same timestamp.""" - await mqtt_mock_entry() - config = { - "device": {"identifiers": ["helloworld"]}, - "name": "test", - "state_topic": "sensor/#", - "unique_id": "veryunique", - } - - data = json.dumps(config) - async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data) - await hass.async_block_till_done() - - device = device_registry.async_get_device(identifiers={("mqtt", "helloworld")}) - assert device is not None - - debug_info_data = debug_info.info_for_device(hass, device.id) - assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1 - assert {"topic": "sensor/#", "messages": []} in debug_info_data["entities"][0][ - "subscriptions" - ] - - dt1 = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC) - dt2 = datetime(2019, 1, 1, 0, 0, 1, tzinfo=dt_util.UTC) - freezer.move_to(dt1) - async_fire_mqtt_message(hass, "sensor/abc", "123") - async_fire_mqtt_message(hass, "sensor/abc", "123") - freezer.move_to(dt2) - async_fire_mqtt_message(hass, "sensor/abc", "123") - - debug_info_data = debug_info.info_for_device(hass, device.id) - assert len(debug_info_data["entities"][0]["subscriptions"]) == 1 - assert len(debug_info_data["entities"][0]["subscriptions"][0]["messages"]) == 2 - assert { - "topic": "sensor/#", - "messages": [ - { - "payload": "123", - "qos": 0, - "retain": False, - "time": dt1, - "topic": "sensor/abc", - }, - { - "payload": "123", - "qos": 0, - "retain": False, - "time": dt2, - "topic": "sensor/abc", - }, - ], - } == debug_info_data["entities"][0]["subscriptions"][0] - - async def test_debug_info_same_topic( hass: HomeAssistant, device_registry: dr.DeviceRegistry,