M8-R01: add configurable MQTT client identity
This commit is contained in:
@@ -454,6 +454,7 @@ def test_get_config_includes_mqtt_section(client: TestClient) -> None:
|
||||
assert "MQTT_USERNAME" in env_names
|
||||
assert "MQTT_PASSWORD" in env_names
|
||||
assert "MQTT_TLS_ENABLED" in env_names
|
||||
assert "MQTT_CLIENT_ID" in env_names
|
||||
|
||||
|
||||
def test_get_config_includes_ha_discovery_section(client: TestClient) -> None:
|
||||
@@ -578,6 +579,18 @@ def test_put_config_invalid_mqtt_port_returns_422_and_does_not_write(
|
||||
assert rows.get("MQTT_BROKER_PORT") != "not-a-number"
|
||||
|
||||
|
||||
def test_put_config_invalid_mqtt_client_id_returns_422(client: TestClient) -> None:
|
||||
_login(client)
|
||||
|
||||
response = client.put(
|
||||
"/api/config",
|
||||
json={"updates": _full_config_payload({"MQTT_CLIENT_ID": "invalid id"})},
|
||||
headers={"X-CSRF-Token": "token"},
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# M5-polish2 Area B: bool fields have input_type="checkbox"
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -722,6 +735,32 @@ def test_put_config_mqtt_reconnect_uses_db_merged_settings(
|
||||
)
|
||||
|
||||
|
||||
def test_put_config_mqtt_client_id_reconnects_and_trims_value(
|
||||
client: TestClient, test_database_urls
|
||||
) -> None:
|
||||
_login(client)
|
||||
mock_mgr = MagicMock()
|
||||
|
||||
with patch("app.api.routes.api.config.mqtt_manager", mock_mgr):
|
||||
response = client.put(
|
||||
"/api/config",
|
||||
json={"updates": _full_config_payload({"MQTT_CLIENT_ID": " home-automation-dev "})},
|
||||
headers={"X-CSRF-Token": "token"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
reconnect_settings = mock_mgr.reconnect.call_args.args[0]
|
||||
assert reconnect_settings.mqtt_client_id == "home-automation-dev"
|
||||
conn = sqlite3.connect(test_database_urls["app_path"])
|
||||
try:
|
||||
stored_value = conn.execute(
|
||||
"SELECT value FROM app_config WHERE key = 'MQTT_CLIENT_ID'"
|
||||
).fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
assert stored_value == ("home-automation-dev",)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# M6-T02: DSMR + Tibber CONFIG_FIELDS
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user