Repository: zpieslak/mobilus-client

Issue:
Move client_id generation out of Config class
Generate unique client_id on each mqtt client initialization, instead of doing it in Config class.
This is first step needed to App class to be more flexible and allow multiple mqtt connections.

Discussion:

Patch:
diff --git a/mobilus_client/app.py b/mobilus_client/app.py
index 0eae7e0..210eb08 100644
--- a/mobilus_client/app.py
+++ b/mobilus_client/app.py
@@ -1,4 +1,5 @@
 import logging
+import secrets
 import socket
 
 from mobilus_client.config import Config
@@ -16,7 +17,7 @@ class App:
         self.message_registry = MessageRegistry()
         self.key_registry = KeyRegistry(config.user_key)
         self.client = MqttClient(
-            client_id=config.client_id,
+            client_id=secrets.token_hex(6).upper(),
             transport=config.gateway_protocol,
             userdata={
                 "config": config,
diff --git a/mobilus_client/config.py b/mobilus_client/config.py
index 572ca03..1918598 100644
--- a/mobilus_client/config.py
+++ b/mobilus_client/config.py
@@ -1,5 +1,4 @@
-import secrets
-from dataclasses import dataclass, field
+from dataclasses import dataclass
 
 from mobilus_client.utils.encryption import create_key
 
@@ -11,7 +10,6 @@ class Config:
     user_password: str
 
     auth_timeout_period: float = 30
-    client_id: str = field(default_factory=lambda: secrets.token_hex(6).upper())
     gateway_port: int = 8884
     gateway_protocol: str = "websockets"
     timeout_period: float = 30
diff --git a/mobilus_client/mqtt_client.py b/mobilus_client/mqtt_client.py
index d935d37..731b62a 100644
--- a/mobilus_client/mqtt_client.py
+++ b/mobilus_client/mqtt_client.py
@@ -17,6 +17,7 @@ logger = logging.getLogger(__name__)
 
 
 class MqttClient(mqtt.Client):
+    _client_id: bytes
     _userdata: dict[str, Any]
 
     def __init__(self, **kwargs: Any) -> None: # noqa: ANN401
@@ -43,7 +44,7 @@ class MqttClient(mqtt.Client):
 
         encrypted_message = MessageEncryptor.encrypt(
             cast(MessageRequest, message),
-            self._userdata["config"].client_id,
+            self._client_id.decode(),
             self._userdata["key_registry"],
         )
 
@@ -52,9 +53,9 @@ class MqttClient(mqtt.Client):
     def on_disconnect(self, _client: mqtt.Client, _userdata: dict[str, Any], reason_code: int) -> None:  # type: ignore[override]
         logger.info("Disconnected with result code - %s", reason_code)
 
-    def on_connect(self, client: mqtt.Client, userdata: dict[str, Any], *_args: Any) -> None:  # type: ignore[override] # noqa: ANN401
+    def on_connect(self, client: mqtt.Client, _userdata: dict[str, Any], *_args: Any) -> None:  # type: ignore[override] # noqa: ANN401
         client.subscribe([
-            (userdata["config"].client_id, 0),
+            (self._client_id.decode(), 0),
             ("clients", 0),
         ])
 


Test patch:
diff --git a/tests/test_app.py b/tests/test_app.py
index 36744b1..66e0caa 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -15,7 +15,6 @@ from tests.factories import (
 class TestApp(unittest.TestCase):
     def setUp(self) -> None:
         self.config = Config(
-            client_id="0123456789ABCDEF",
             gateway_host="host",
             user_login="login",
             user_password="password",
diff --git a/tests/test_config.py b/tests/test_config.py
index 3d6f7a8..6f792b2 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -15,9 +15,6 @@ class TestConfig(unittest.TestCase):
     def test_auth_timeout_period(self) -> None:
         self.assertEqual(self.config.auth_timeout_period, 30)
 
-    def test_client_id(self) -> None:
-        self.assertEqual(len(self.config.client_id), 12)
-
     def test_gateway_host(self) -> None:
         self.assertEqual(self.config.gateway_host, "host")
 
diff --git a/tests/test_mqtt_client.py b/tests/test_mqtt_client.py
index 66d06d8..83dbd8d 100644
--- a/tests/test_mqtt_client.py
+++ b/tests/test_mqtt_client.py
@@ -22,8 +22,8 @@ from tests.helpers import encrypt_message
 
 class TestMQTTClient(unittest.TestCase):
     def setUp(self) -> None:
+        self.client_id = "0123456789ABCDEF"
         self.config = Config(
-            client_id="0123456789ABCDEF",
             gateway_host="host",
             user_login="login",
             user_password="password",
@@ -33,7 +33,7 @@ class TestMQTTClient(unittest.TestCase):
         self.message_registry = MessageRegistry()
         self.key_registry = KeyRegistry(self.config.user_key)
         self.client = MqttClient(
-            client_id=self.config.client_id,
+            client_id=self.client_id,
             transport=self.config.gateway_protocol,
             userdata={
                 "config": self.config,
@@ -147,7 +147,7 @@ class TestMQTTClient(unittest.TestCase):
         mock_subscribe.assert_called_once_with(
             self.client,
             [
-                (self.config.client_id, 0),
+                (self.client_id, 0),
                 ("clients", 0),
             ],
         )