mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Fix client_id
not generated when connecting to the MQTT broker (#140264)
Fix client_id not generated when connecting to the MQTT broker
This commit is contained in:
parent
25f15c1149
commit
6284a83a34
@ -15,6 +15,7 @@ import socket
|
|||||||
import ssl
|
import ssl
|
||||||
import time
|
import time
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
import certifi
|
import certifi
|
||||||
|
|
||||||
@ -292,7 +293,7 @@ class MqttClientSetup:
|
|||||||
"""
|
"""
|
||||||
# We don't import on the top because some integrations
|
# We don't import on the top because some integrations
|
||||||
# should be able to optionally rely on MQTT.
|
# should be able to optionally rely on MQTT.
|
||||||
import paho.mqtt.client as mqtt # pylint: disable=import-outside-toplevel
|
from paho.mqtt import client as mqtt # pylint: disable=import-outside-toplevel
|
||||||
|
|
||||||
# pylint: disable-next=import-outside-toplevel
|
# pylint: disable-next=import-outside-toplevel
|
||||||
from .async_client import AsyncMQTTClient
|
from .async_client import AsyncMQTTClient
|
||||||
@ -309,9 +310,10 @@ class MqttClientSetup:
|
|||||||
clean_session = True
|
clean_session = True
|
||||||
|
|
||||||
if (client_id := config.get(CONF_CLIENT_ID)) is None:
|
if (client_id := config.get(CONF_CLIENT_ID)) is None:
|
||||||
# PAHO MQTT relies on the MQTT server to generate random client IDs.
|
# PAHO MQTT relies on the MQTT server to generate random client ID
|
||||||
# However, that feature is not mandatory so we generate our own.
|
# for protocol version 3.1, however, that feature is not mandatory
|
||||||
client_id = None
|
# so we generate our own.
|
||||||
|
client_id = mqtt._base62(uuid4().int, padding=22) # noqa: SLF001
|
||||||
transport: str = config.get(CONF_TRANSPORT, DEFAULT_TRANSPORT)
|
transport: str = config.get(CONF_TRANSPORT, DEFAULT_TRANSPORT)
|
||||||
self._client = AsyncMQTTClient(
|
self._client = AsyncMQTTClient(
|
||||||
callback_api_version=mqtt.CallbackAPIVersion.VERSION2,
|
callback_api_version=mqtt.CallbackAPIVersion.VERSION2,
|
||||||
|
@ -1556,6 +1556,42 @@ async def test_setup_uses_certificate_on_certificate_set_to_auto_and_insecure(
|
|||||||
assert insecure_check["insecure"] == insecure_param
|
assert insecure_check["insecure"] == insecure_param
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("mqtt_config_entry_data", "client_id"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
{
|
||||||
|
mqtt.CONF_BROKER: "mock-broker",
|
||||||
|
"client_id": "random01234random0124",
|
||||||
|
},
|
||||||
|
"random01234random0124",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
{
|
||||||
|
mqtt.CONF_BROKER: "mock-broker",
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_client_id_is_set(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||||
|
client_id: str | None,
|
||||||
|
) -> None:
|
||||||
|
"""Test setup defaults for tls."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.mqtt.async_client.AsyncMQTTClient"
|
||||||
|
) as async_client_mock:
|
||||||
|
await mqtt_mock_entry()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert async_client_mock.call_count == 1
|
||||||
|
call_params: dict[str, Any] = async_client_mock.call_args[1]
|
||||||
|
assert "client_id" in call_params
|
||||||
|
assert client_id is None or client_id == call_params["client_id"]
|
||||||
|
assert call_params["client_id"] is not None
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"mqtt_config_entry_data",
|
"mqtt_config_entry_data",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user