mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 18:58:04 +02:00
Split mqtt subscribe and unsubscribe calls to smaller chunks (#118035)
This commit is contained in:
@ -44,7 +44,7 @@ from homeassistant.const import (
|
||||
UnitOfTemperature,
|
||||
)
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.core import CoreState, HomeAssistant, callback
|
||||
from homeassistant.core import CALLBACK_TYPE, CoreState, HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er, template
|
||||
from homeassistant.helpers.entity import Entity
|
||||
@ -2816,6 +2816,59 @@ async def test_mqtt_subscribes_in_single_call(
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"mqtt_config_entry_data",
|
||||
[
|
||||
{
|
||||
mqtt.CONF_BROKER: "mock-broker",
|
||||
mqtt.CONF_BIRTH_MESSAGE: {},
|
||||
mqtt.CONF_DISCOVERY: False,
|
||||
}
|
||||
],
|
||||
)
|
||||
@patch("homeassistant.components.mqtt.client.MAX_SUBSCRIBES_PER_CALL", 2)
|
||||
@patch("homeassistant.components.mqtt.client.MAX_UNSUBSCRIBES_PER_CALL", 2)
|
||||
@patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0)
|
||||
@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
|
||||
async def test_mqtt_subscribes_and_unsubscribes_in_chunks(
|
||||
hass: HomeAssistant,
|
||||
mqtt_client_mock: MqttMockPahoClient,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
record_calls: MessageCallbackType,
|
||||
) -> None:
|
||||
"""Test chunked client subscriptions."""
|
||||
mqtt_mock = await mqtt_mock_entry()
|
||||
# Fake that the client is connected
|
||||
mqtt_mock().connected = True
|
||||
|
||||
mqtt_client_mock.subscribe.reset_mock()
|
||||
unsub_tasks: list[CALLBACK_TYPE] = []
|
||||
unsub_tasks.append(await mqtt.async_subscribe(hass, "topic/test1", record_calls))
|
||||
unsub_tasks.append(await mqtt.async_subscribe(hass, "home/sensor1", record_calls))
|
||||
unsub_tasks.append(await mqtt.async_subscribe(hass, "topic/test2", record_calls))
|
||||
unsub_tasks.append(await mqtt.async_subscribe(hass, "home/sensor2", record_calls))
|
||||
await hass.async_block_till_done()
|
||||
# Make sure the debouncer finishes
|
||||
await asyncio.sleep(0.2)
|
||||
|
||||
assert mqtt_client_mock.subscribe.call_count == 2
|
||||
# Assert we have a 2 subscription calls with both 2 subscriptions
|
||||
assert len(mqtt_client_mock.subscribe.mock_calls[0][1][0]) == 2
|
||||
assert len(mqtt_client_mock.subscribe.mock_calls[1][1][0]) == 2
|
||||
|
||||
# Unsubscribe all topics
|
||||
for task in unsub_tasks:
|
||||
task()
|
||||
await hass.async_block_till_done()
|
||||
# Make sure the debouncer finishes
|
||||
await asyncio.sleep(0.2)
|
||||
|
||||
assert mqtt_client_mock.unsubscribe.call_count == 2
|
||||
# Assert we have a 2 unsubscribe calls with both 2 topic
|
||||
assert len(mqtt_client_mock.unsubscribe.mock_calls[0][1][0]) == 2
|
||||
assert len(mqtt_client_mock.unsubscribe.mock_calls[1][1][0]) == 2
|
||||
|
||||
|
||||
async def test_default_entry_setting_are_applied(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
|
Reference in New Issue
Block a user